Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#5
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 20-Mar-11 1:38am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
SAKryukov at 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
AlbinAbel at 20-Mar-11 23:13pm
   
Alternative thought. My 5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
SAKryukov at 20-Mar-11 21:30pm
   
Agree, my 5, see also my short Answer.
--SA
AlbinAbel at 20-Mar-11 23:11pm
   
Agreed. My 5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Comments
SAKryukov at 20-Mar-11 21:30pm
   
I agree, my 5. In my answer I insist trying to treat ports, etc. as controls is bad.
--SA
AlbinAbel at 20-Mar-11 23:11pm
   
Yes, right. My 5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
Comments
AlbinAbel at 20-Mar-11 23:15pm
   
Good direction. OP may need to have a winform if he needs an interactive UI. My 5
SAKryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 195
1 ProgramFOX 130
2 Maciej Los 105
3 Sergey Alexandrovich Kryukov 105
4 Afzaal Ahmad Zeeshan 82
0 OriginalGriff 6,564
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 21 Mar 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100