Careca_Lisinha_
❔ How to access a button from a different class?
Originally, my idea was to separate my methods into different files.
As IsNotNull mentioned, it is possible to do that, but it is an incorrect way. Therefore, until I have knowledge about another way to split my code, I will keep it in the main class.
15 replies
❔ How to access a button from a different class?
I will provide a clear example.
To begin, I have a project. Within the project, there is a form, code files, and other things. Inside the form, we have the designer. In the designer, I have a label called "label1".
Now let's move on to the code part. When you have a Windows Forms project, there is always a file called "Form1.cs" by default. If I wanted to modify the text of label1, I just need to write "label1.Text = 'hello world'" and I can change it. In other words, in this file, I can access these elements (I don't know the technical name for labels, buttons, and similar things) just by their name.
Now, imagine that I have a new class called "Class2". If I want to access label1 in the same way I did in "Form1.cs", I won't be able to do it, even if I change the modifier of label1 to public.
I am looking for a way to access these elements through other files, basically.
15 replies