 |
| VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1). |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

March 22nd, 2005, 12:12 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
Resetting a form
Hello all,
I have a form with a bunch of combo boxes and textboxes for data input. Once the user saves the entered info, I want to clear all the controls, basically set all the comboboxes to the original item, and clear all the textboxes. Is there a way to do this with out writing code, control by control? Possibly loop through a controls collection?
Thanks for the help.
Jim
|
|

March 22nd, 2005, 05:45 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
(âwithoutâ is 1 word...)
There was a method in MS Access to completely reset a form, but it does not appear that there is such a method in .NET.
Usually what I do is create a routine like 'Reset_frmMain()' which explicitly sets the values in all the controls.
It is more time consuming than a built in method, but it gives you complete control over what happens (you can skip some controls, set their values according to changing circumstances such as setting a time of day control, and so on).
Then, when necessary, just invoke that routine. (I know you know you can do this, as your question makes that clear. I'm not trying to imply you don't...)
But there certainly are times that a built in âclear âem all!â method would be nice.
I wonder if there is a way to get the form to reload as if it were new?
|
|

March 22nd, 2005, 05:52 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
Thanks for the reply and spelling lesson :)
I did actually write me own function and excluded controls as necessary. I was wondering if there was a method like that. I come from a Power Builder background and that functionality exists.
I have called events explicitly in ASP.NET, but it can get messy and probably not a good idea but it can be done.
|
|

March 22nd, 2005, 06:16 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
You can use the controls collection to loop through all of the controls on a form (or panel, or groupbox, anything that can hold controls). If it is a textbox, radiobutton, checkbox, etc., do a type conversion and reset it how you want.
It isn't that difficult (I think the one that I wrote only has about 10 lines of code. The problem that I ran into was comboboxes and resetting them to a default value. If there is one or they all hold the same information, it isn't a problem. But if more than one needs reset with different info, it is a problem. I haven't had time to look into a way around this.
J
|
|

March 22nd, 2005, 11:54 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
They only way I have found around the combobox issue is to create a different data source for each one. I created a dataset and then used the copy method to create a copy for each combox.
|
|

March 23rd, 2005, 12:54 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
I don't have a problem populating the comboboxes, just resetting the selected value of multiple boxes when I clear all of the controls. If all you want to do is reset the combobox to index 0, it isn't a problem.
If you want to set the value to a specific item, say "California" in a combobox that contains states and "Wisconsin" in a different combobox that contains states my function won't work as written. It is something I hope to fix in the future, but like I said I haven't gotten around to it yet.
J
|
|

March 23rd, 2005, 10:41 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
if you bind the comboboxes to the same data set you will have that problem. You either have to create a separate data set for each one, or loop through one dataset and add the items manually.
|
|

March 23rd, 2005, 11:06 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 540
Thanks: 0
Thanked 4 Times in 4 Posts
|
|
I steer clear of binding the comboboxes. I have had too many issues when using this method.
J
|
|

March 23rd, 2005, 11:37 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
I agree with you and have learned my lesson. The behavior is different in ASP.NET. I am getting used to Windows application programming now.
|
|
 |