Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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.

1 solution

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...
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900