REALLY simple question im sure

  • Thread starter Thread starter S Moran
  • Start date Start date
S

S Moran

just trying to grasp the insane differences between vb6 and vs2005...

2 buttons on a form. 1 button creates a new instance of a second form, and
shows the form. why cant i access this form from the second button?
 
S Moran said:
just trying to grasp the insane differences between vb6 and vs2005...

2 buttons on a form. 1 button creates a new instance of a second form, and
shows the form. why cant i access this form from the second button?

You can. Where are you having problems? Is the variable that references
the second form in a scope that the second button can access?
 
thats where my confusion is...
if i say: Form secondform = new Form2();
inside of the click event for button1, then button2 cant access the form,
correct?

and if i declare it outside of the buttons click event at the class level,
then both buttons can access it.

my questions are... couldnt i say: public Form secondform = new Form2();
in the click event of button1?

or does the buttons click event need to be made public instead of private?

just trying to grasp it all....
 
Hallo!
my questions are... couldnt i say: public Form secondform = new Form2();
in the click event of button1?

or does the buttons click event need to be made public instead of private?

you cannot declare a public field inside a clickevent.
You need to do it this way (better: this is one way, doing it):

public Form secondform; //simple declare the public field outside of any
other members, but inside the class of course
private void button1_Click(object sender, EventArgs e)
{
secondform = new form2();
}
private void button2_Click(object sender, EventArgs e)
{
secondform.show // or whatever
}

greets
Daniel
 
thank you... now...

if i declare a new form in the class, and button one does "form2.show" and
button2 does "form2.close", clicking form1.show again causes an error
because the form doesnt exist anymore. how can i deal with this?
 
Hi !
if i declare a new form in the class, and button one does "form2.show" and
button2 does "form2.close", clicking form1.show again causes an error
because the form doesnt exist anymore. how can i deal with this?

you need to know: a form has a lifecircle. creating, showing, closing,
destucting.

after closing a form, you cannot open it again. You would need to create a
new one by 'form2=new secondform();' inside the clickevent. But I guess,
closing the form was not your real intention. You can set
form2.visible=false; instead closing the form. In this case you will be able
to reshow the form.

greets
Daniel
 
S said:
if i declare a new form in the class, and button one does
"form2.show" and button2 does "form2.close", clicking form1.show
again causes an error because the form doesnt exist anymore. how can
i deal with this?

Two suggestions:

1. Get Button2 to hide the form rather than close it. You can hide and
re-show a form as many times as you like. It will of course be the same form
instance each time, so all values that were present in the form when you
hide it will still be there when you re-show it.

2. Alternatively, in Button2, after you close the form, set its reference to
Nothing. Then in Button1 you can check for this and create a new form if
required:


\\\
Public secondForm As Form

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

If secondForm Is Nothing Then
secondForm = New Form2
End If

secondForm.Show()

End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

If secondForm IsNot Nothing Then
secondForm.Close()
secondForm = Nothing
End If

End Sub
///

HTH,
 
Back
Top