Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
Say I have a form Main.cs, and it has a series of buttons and controls on it. Now generally it is good practice to create a class file for everything that is being handled, for example I have a serial port Icon, when I click in the main form, it should start the serial port communications. In this button click, it should call the Serial port Class (which I should mention is a file in its own), and I will have a series of these classes which are on their own separated from Main.cs (which will just be in general empty).

So what I would like to know is there a way to access the controls in the Main form like buttons and Text boxes and the likes from any of the class files that I will create????
Posted

Basically controls are just instances of classes, so if you create them at runtime and store them to a variable, collection etc you can later use the same control via the variable. If the variable is defined in your main window it's accessible in the main window in any method as long as the main window is running. Of course, if you add the newly created control to your windows controls collection, it's accessible via that collection also. One good way to understand this more thoroughly is to have a look at the generated Main.Designer.cs.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Mar-11 21:32pm    
The idea of looking at generated code is not but, but your answer does not address OP's misconception other Answers do. (I did not vote for this one.)
--SA
Albin Abel 20-Mar-11 23:13pm    
Alternative thought. My 5
My suggestion to you would be to take a step back and look at your classes. Your thought processes are close to being correct here. You absolutely want to have separate classes for things like your Serial Port class. What you do NOT want to do is have code in those classes manipulating UI. Proper separation of your domain and UI logic is imperative to functional code. In your Main.cs code you already kick off some process in the the Serial Port class. What you may wish to do is start as you have, but when the process in Serial port class is complete, update your UI from within Main.cs by checking the appropriate values in the Serial Port class after the run is complete.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Mar-11 21:30pm    
Agree, my 5, see also my short Answer.
--SA
Albin Abel 20-Mar-11 23:11pm    
Agreed. My 5
What you should do is create a set of custom events that the form can subscribe to, make the other classes post events, and then the form can subscribe to them. That way, your classes don't need to know anything about the UI component that uses them. All it has to do is post events, and if there are any subscribers, those subscribers will do what they need to do.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Mar-11 21:30pm    
I agree, my 5. In my answer I insist trying to treat ports, etc. as controls is bad.
--SA
Albin Abel 20-Mar-11 23:11pm    
Yes, right. My 5
This is actually a very bad to treat non-visual non-UI objects like a port in the same way as Form controls.
You should do something directly opposite: thoroughly isolate UI from functionality. Further discussion of this would lead us too far. I only point out that by asking your Question you already push in wrong direction.

—SA
 
Share this answer
 
Comments
Albin Abel 20-Mar-11 23:15pm    
Good direction. OP may need to have a winform if he needs an interactive UI. My 5
Sergey Alexandrovich Kryukov 20-Mar-11 23:22pm    
This is unfortunately has something to do with Microsoft marketing. Development tools are getting better though. In near past there was a lot more fashion and advertisement of offering non-visual stuff as controls.
Thank you, Albin.
--SA
I like this subscribing idea, I shall google search in the mean time about these things. Essentially in my serial port class all it does is retrieve data, in the program that I am trying to sort out, EVERYTHING was in Main.cs(very bad i knw---silly electronic engineers), and all it did was post filtered data to a textbox.

So for the serial port class thing I intend on calling it like this Textbox.Appendtext(call Serial Port Class), I have created an event for the Serial Port occucrence happen....

Anyway will look further into this events and subscribing thing, will post back here as soon as I understand it and to make sure ask a few questions....Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900