Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Initialising controls on a form

Status
Not open for further replies.

huggyboy

Programmer
Feb 7, 2003
108
0
0
GB
I want to return some (or all) of the controls on a form to their initial state (as defined on the properties of the design screen) on the click of a radio button on that form.

Obviously i can hardcode the the changes for the controls but i was looking for a more generic way of doing it.

I dont want to call the form load event routine because it does all sorts of background data setting up i dont want done again.

I found the automatically generated subroutine InitializeComponent(which is presumably called in the background on or just before the load event) however calling it causes an infinite loop because the radio button event is fired(i think).

The other solutions coming to mind are either
1) suppress all events call initializecomponenet then switch events back on (vaguely remember acommand in excel BA that does that) or
2) cycle around the controls collection and restore some kind of default properties back to the control - ignoring the ones causing the problem.

Neither of the solutions i've managed to figure out how to do.

I have just started using vb.net 2008 after using VBA/VB6 for a couple of years I would appreciate some pointers.

Many thanks in anticipation and thanks to all the people posting on this forum for help in the past.
 

I got lost on "return some (or all) of the controls on a form to their initial state "

Do you want your btnExit with the text "Exit" to change back to Button5 with text "Button5"?


Have fun.

---- Andy
 
Sorry I wasnt clearer - thanks for the quick reply

OK - this is my first ever post - I will have another go.

If you set a label up with "original" in the text and a button which changed it to text of "changed". Run the app and click the button which obviously changes the text.
I am looking for an easy way to set up another button, when clicked, sets the label back to text of "original" ie detect the 'design time' settings of the label automatically.

Obvioosly there are programming ways to do do it (on form load store the value then put it back on the 2nd button click) but i wondered if there was a generic way so all the 'design time' settings on the controls on the form can be restored.

Thanks again.
 
I don't think you can use initialiseComponent (which is how the IDE sets everything up), because that function assumes that your objects have not been instantiated...

I'm not sure if there is a quick way of doing this, bar writing a general routine called from Form_Load that uses reflection to note every control on the form together with it's properties...
 
I would suggest letting us know what the end result you are looking for is. An instanced form is the first thing I think of, but it really depends on what you are doing if another suggestion might be better.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
mmilan - you are absolutely correct - calling initializeComponent doesnt work - i tried it in a simpler test project -havent done anything with reflection as yet - will look into it - thanks

Sorwen -
The system I am writing has several user role types and what appears on the 2nd screen (after logon screen) depends on the user role - which is controlled with the code on the Load event making the appropriate fields visible or invisible - all well and good.
However some users can have multiple user roles which they swap between by clicking radio buttons on the second screen.
Calling the subroutines that have run in the form load does the job however the code assumes the 2nd form is in the 'design type' state before it runs so a field that is invisible at design time is changed to visible by user role 1 but never set to invisible when changing to user role 2 (which never changes it to invisible because it assumes it is)
All this is cured at a stroke if the design-time values are refreshed and a trick i can use in the future

Cheers both for the suggestions.
 
Just my first thought would be create a class that inherits from panel or create a windows control library. Then when you need to reload those controls just delete the current panel/control and make a new one.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
ok - as i said i'm new to vb.net from vb6 so I'll have to think about that - i will have a crack at at in my test project. Presumably you mean create a panel class (inherited from the general one) containing the controls i want to change the properties of then delete & create the panel class inside the form when the buttons are clicked.

Sounds feasible to me - thanks for the help - will post the result if i have any joy

Hugh
 
ok - as i said i'm new to vb.net from vb6 so I'll have to think about that - i will have a crack at at in my test project. Presumably you mean create a panel class (inherited from the general one) containing the controls i want to change the properties of then delete & create the panel class inside the form when the buttons are clicked.
Yes, that is the best way I can think of for what you said. It also leaves open a lot of oppertunity for growth.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Thanks - will try out when i have some time - working on a different aspect of the same project this week
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top