❔ How to access a button from a different class?

Hello. Recently, I was developing a project and decided to start splitting my code into separate files instead of keeping everything in form1.cs. I'm a beginner, and this may be a very basic question, but I really don't know how to access a button or another designer element from a different class other than form1.cs. I've searched everywhere, but I couldn't find a tutorial that taught me this.
10 Replies
Anchy
Anchy17mo ago
need a little bit more details, but it sounds like your class has a dependency on the UI which means it should stay in your code-behind (form1.cs file) unless the code can be re-written to be self reliant you would need to share some examples of this code
IsNotNull
IsNotNull17mo ago
@carecalisinha Anchy is correct that we would need more context to help (some code or an explanation of a specific thing you are trying to do). I would say that offhand it sounds like you might be thinking about this the wrong way. You usually wouldn't move code into another file just to have more files...usually you would move code for each type of thing into its own file. Its not required and devs will occasionally violate this, but the most common convention is to have one class or interface definition per file. If you are using WinForms, then each form usually has its own class (and designer/resource files if you are using the designer). If your form1.cs is growing quite large and contains all of your code, it might indicate that you need to add some types to represent the 'things' that you are working with in form1.cs. For example, if I have a form that contains a list of products, I might make a Product.cs file that contains a Product class could contain a name, a price, a description. In the form, when the form is opening I might create the list of products to show in the form, or load them from a file/database, or whatever it is I'm trying to accomplish. Most of the distinct 'things' that you work with should have their own type. If you know this all already, then I appologize for being wordy. A simplistic approach used by most beginners would have the form own and with work instances of other types and those types would not know about the details of the form. Having class files that represent data and know about the details of a form would generally be considered a bad approach. I would never have a Product know about a button on the form that shows products, for example. It would be fine for the form to handle a button click, then call a public method on a Product though.
Denis
Denis17mo ago
The Form.cs is responsible for handlig the UI controls - buttons, images, tables, etc. If you wish to split your code into different files, you need to let us know what you are trying to do exactly, and what code you have written. In general, there are patterns for separating code from the Form (a.k.a. View). Those are MVC and MVVM. I believe you are on WinForms, since you've mentioned Form.cs, this GUI platform is more tailored towards MVC. Check it out, it is very likely that is exactly what you are looking for. M - model, classes that represent your data, e.g., book. V - view, what the user sees. Handles the UI controls, their events C - controller, handles non-ui specific logic. E g., loads books from the database, saves files, authenticates the user, does a calculation. The controller provides models (data) to the view. The view calls methods from the controller for handling data, as the view is responsible for handling the UI, while the controller is responsible for the data - basic separation of cocnerns. Another note, if you are trying to access the button from a different class, then it is very likely you are doing something wrong
Careca_Lisinha_
Careca_Lisinha_OP17mo ago
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.
Denis
Denis17mo ago
Labels and buttons are called controls And again, what you are trying to do is incorrect. Why are you trying to access UI controls from other classes? UI controls of a given form should only be accessible by the given form
IsNotNull
IsNotNull17mo ago
@carecalisinha This of course can be done, but there is almost universal consensus among developers that this is an inferior and hard to maintain design. When you make instances of a type defined in another file, you could pass a reference to that types constructor method to the form or label, or assign them onto a property of one of those types. In the example I gave earlier, your Product type could have a constructor that takes a parameter of the type of your Form1's class. That would let you interact with it from another file. Its still the wrong thing to do.
Careca_Lisinha_
Careca_Lisinha_OP17mo ago
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.
Denis
Denis17mo ago
MVC is what you need to look into
Careca_Lisinha_
Careca_Lisinha_OP17mo ago
I will look. Thank you
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server