Going to the previous form creates problems
Hi,
I have 2 Forms. I am doing this
when in form1 > menuform2 click - I go to Form2.
when in Form2 > menuForm1_Click - I go to Form1.
when in Form2 > menuForm3_Click - I go to Form 3
BUT when I go back to Form1 from Form2, a combobox does not get populated.
Also a problem when I go from Form3 to Form2 where the comboboxes get populated more than one time.
How can I make Form2 and Form3 become sub-forms of Form1 having all the functions included.
Hope I have made myself clear. Please guide me how to clear this.
Thanks for helping.
Re: Going to the previous form creates problems
Are the combobox population subroutines located/called from the Form Load event for the respective forms? Do you close the forms when opening the other one?
Re: Going to the previous form creates problems
I am closing the forms but not all have sub routines, some are populated on page load cos I have many conditions associated with them..
Does the Form_Activated helps if I go about it that way ??
Re: Going to the previous form creates problems
Yes, maybe.
If the form is closed before going to another form, you would need to re-load the form to show it again, in which case, using the Load event is almost certainly better. If the form is only hidden when moving to the next form, then using the Activated event would be correct, since the Load event will not fire again when a form is simply un-hidden.
I strongly prefer to have a form set itself up in the Load event, though part of this was a holdover from VB6, where there was a potentially nasty bug when using the Activate event. In VB6, it was possible to cause the Activate event not to fire, or to have the Activate event for the wrong form fire, during debugging, depending on where breakpoints were located. This made debugging occasionally difficult. I don't know that this still remains in VB.NET. In any event, setting up the form in the load event can be a good idea.
There is potentially a problem with the chain of forms you describe. If Form1 shows Form2 modally in response to some button (or menu) click, then Form2 shows Form1 modally, it would be possible to keep going from 1->2->1->2....etc. Since each button click is a sub, and execution of the sub halts until the ShowDialog call returns, a look at the call stack would show an increasing series of calls. Eventually, you will run out of stack space, and the program will crash. This would take a LOOOOOOOONG time on most modern computers, but it is a design consideration.
One thing I do to get around this is this:
1) have a global integer variable.
2) Have a function that consists of a Select Case statement that displays a different form in response to the global variable.
3) Set the variable to the first form to be shown, then call the function in an endless loop.
4) Have a value for the variable that breaks the loop.
VB Code:
do while theState > 0
'Call the function in #2
loop
Then, when you want to switch from form1 to form2, set the global to the right setting for form2, then close form1.
Well, that's a fair amount of off topic chatter. Just an idea.