Click here to Skip to main content
14,241,167 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,

I have read oops concept many times from different-2 articles and concept is clear to me. I am working as software developer since last 7 years using c# dot net technology but still could not know how to manipulate the custom class with windows form controls to retrieve and save data to database as windows form controls are not accessible from custom class, either this generates run time error or gives unexpected result.

So here i want to know, how can i use windows controls with custom classes. I mean, I want to define some functions like ADO.NET/Sql command to get and save the data to database and other required functions in custom class and to manipulate these functions with windows forms controls like TEXT BOX, COMBO BOX, BUTTON, DATAGRIDVIEW, LIST VIEW, RADIONBUTTON etc.

Please suggest any solution.

Thanks

What I have tried:

I tried to do this by Self Practice but could not get solution.
Posted
Updated 22-Jun-18 0:13am
Comments
F-ES Sitecore 22-Jun-18 5:17am
   
It sounds like your question is "how do I write a program". Get a book on forms development and go through it to get a grasp on the basics, you can't learn something as broad and complex from forum posts.
K N Sharma 23-Jun-18 6:58am
   
I think you did not understand my question. My issue is accessing a window form control like text box in custom class method to retrieve and save the record to database.
#realJSOP 22-Jun-18 5:30am
   
If you've been doing this for seven years and still don't understand it, maybe you should consider another line of work.
K N Sharma 23-Jun-18 6:58am
   
I think you did not understand my question. My issue is accessing a window form control like text box in custom class method to retrieve and save the record to database.
Richard MacCutchan 22-Jun-18 6:10am
   
7 years as a C# developer and you still do not understand the basics? If you create a custom form based on the System.Windows.Form class you have access to everything.
K N Sharma 23-Jun-18 6:58am
   
I think you did not understand my question. My issue is accessing a window form control like text box in custom class method to retrieve and save the record to database.
Eric Lynch 22-Jun-18 9:29am
   
No magic involved. When you are designing your form, simply subscribe to one of the events exposed by the controls. With WinForm designer, this generally involves double-clicking the control...at which point, a stub method is added to your form to handle that event. For example, Button has a click event. Then, within that handler method, put whatever logic you want to call methods in your custom classes.

That said, like the other respondents, I'm somewhat amazed that you haven't already done this in 7 years as a C# developer. Almost all UIs are event driven...the exact interface and designer change between approaches, but conceptually its all about the same.
K N Sharma 23-Jun-18 6:58am
   
I think you did not understand my question. My issue is accessing a window form control like text box in custom class method to retrieve and save the record to database.
Richard MacCutchan 23-Jun-18 7:43am
   
We do understand your question, but it lacks much useful detail. As it stands we have no idea what your custom class is based on or how it relates to your Form. If your custom class is based on a Windows.Form then there is absolutely no problem. If it is based on something else then you need to tell us what it is, and what the difficulty is in accessing the properties of the Form.

As I said earlier, after 7 years as a C# developer this should be easy.
Eric Lynch 23-Jun-18 14:50pm
   
For an event driven application, it sounds a bit backwards for a custom class to manipulate a control (unless perhaps the custom class is derived from a control). As many respondents have suggested, I think perhaps its less a misunderstanding of the few details in the question, than a general lack of detail in the question itself.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

In order to work with Windows controls and to be displayed, your class must be a Control itself - i.e. it must derive from Control in order to get a windows and a message loop. The usual way this is done is to create a UserControl class, and drop your form controls onto that. UserControl Class (System.Windows.Forms)[^]
If you are not trying to create a control that can be displayed, then you would treat it as a normal class: you get and set data via properties, and cause it to do things via methods. We can;t explain all that to you - if you have been a developer for 7 years and you don't understand such very, very basic things then you really need to get a book and read it from cover to cover, because it's clear you have missed out far, far too much stuff somewhere...
   
Comments
K N Sharma 23-Jun-18 7:14am
   
Thank to reply. Yes this what i want to know. I knew this that i can do this using properties concept to pass the values but i also wanted to confirm that is there any way to access the window form controls like text box in custom class method or not.

Thanks
OriginalGriff 23-Jun-18 7:44am
   
If you want to contain text boxes, you need to be using a Control - so start by checking what your class is derived from.

Once you have a UserControl that contains TextBoxes etc., you don't access them directly from outside - that's a bad idea as it "locks" the design; you can't make changes without thinking about how the outside world might have used it - instead you use properties of your class to access the data from the child controls.

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




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