Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

In my form application I use multiple textboxes and buttons. I want to reuse them but the application failed over and over. I thought it was something like:

window TestWindow = new window();

So, how can I reuse my textboxes in my other classes? (I am just starting with C#, so hopefully you can help me :-))
Posted
Comments
Sandeep Mewara 28-Mar-11 13:04pm    
Elaborate "reuse of textboxes in other classes".
Toli Cuturicu 28-Mar-11 16:42pm    
Not clear.

There is a "window" class in WPF, but since you are talking about WinForms, I assume that you don't want to use that!

It's not that clear what you are trying to do, but I think that:
1) You have created a form with multiple textboxes and buttons.
2) You want to show several copies of this form, so that the user can enter or view different information with each.

If so, then it is fairly simple: each time you want to display a new copy of your form, just do:
MyNewForm form = new MyNewForm();
// Set any properties the form needs.
form.Show();
I would also suggest that it would be a good idea to include a Closed event handler so that you can retrieve any new information if you need to.
MyNewForm form = new MyNewForm();
form.FormClosed += new FormClosedEventHandler(MyNewForm_FormClosed);
// Set any properties the form needs.
form.Show();


void MyNewForm_FormClosed(object sender, FormClosedEventArgs e)
   {
   MyNewForm form = sender as MyNewForm;
   if (form != null)
      {
      // Get any information the user has entered.
      }
   }
 
Share this answer
 
Comments
Member 7762422 29-Mar-11 3:24am    
Thanks for your information!!
If you have controls (like textboxes and radio buttons) that are used in the same format/arrangment in multiple forms, you can create a UserControl that contains those controls, and then include the user control in any form that needs those controls.

If you want to dynamically crerate controls in a form, you can do that too, but it gets a little tougher because you need to manage ALL of the creation and initializeation in code instead of relying on the designer to do it for you. In that case, I'd probably create a class library that handles all that stuff for me allowing the programmer to pass in parameters for things like position, initial settings, event handling, and stuff like that.

It's hard to answer your question because of the way it was stated.
 
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