D
D
Danil2019-10-22 09:22:00
C++ / C#
Danil, 2019-10-22 09:22:00

Display text in label on click c# windows forms?

Hello everyone, I'm trying to do something simple here and faced a problem:

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
            private void button1_Click(object sender, EventArgs e) 
            {
                label1.Text = "Test";
            }
        }
    }

There is a button button1 and a label label1
When the button is clicked, the label should take the value Tes, I understand that I'm doing something wrong, but what exactly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Foggy Finder, 2019-10-28
@flabot

In WinForms projects, the form designer hides the user interface details in the Form1.Designer.cs file (Form1 will be the name of the specific class). Therefore, when copying from other projects or examples, you must remember to transfer all subscriptions as well.
To check their presence (or absence), select the element in the form designer and go to view its properties (Properties).
By default, the control properties tab will open. To view information about events, you need to go to the next one (with a drawn lightning bolt):
In the list, find the required element. If nothing is assigned on the right, then the matter is in the missing subscription. Through the drop-down list (ComboBox), select the handler from the available ones:
Run, check.
If everything works, then the problem is found correctly.
It is not necessary to use the form designer to subscribe, you can create them yourself, for example, in the form designer:
But it is better to stick to one style, and the code will be easier to understand and there will be no unnecessary subscriptions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question