Basic Question - Working with forms

C

Chris Strug

Hi there,

Possibly something of a simple question but I was wondering is anyone could
provide a few pointers or links.

Given that I have two forms, a main (frmMain) and a child (frmChild). From
frmMain, I create an instance of frmChild and show() it. The child form
contains a property called "getString" which I want to return the contents
of a textbox from the child form.

I have something that I think works, however it falls over if the user exits
the child form (such as by using the corner cross) instead of clicking
cmdReturnData.

I was just wondering if I am on the right lines - creating properties and so
on for the child form seem pretty intuititive but actually calling them from
the main form trouble free seems to be a pain.

Apologies if this is a little vague - I'm working on my .net having moved
from vb6 so I'm still trying to get used to the way that .net works...

Thanks

Chris.


************************

Public Class FrmMain

Dim frmChild As New frmChild

Private Sub mainFrm_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

End Sub

Private Sub cmdShowChild_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdShowChild.Click
frmChild.ShowDialog()

Me.txtRetrievedData.Text = frmChild.getString

frmChild.Dispose()
End Sub
End Class


****************
Public Class frmChild

Public Property getString()
Get
Return Me.txtChildTextvalue.Text
End Get
Set(ByVal value)

End Set
End Property

Private Sub auxFrm_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

End Sub

Private Sub cmdReturnData_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles cmdReturnData.Click
Me.Hide()
End Sub
End Class
 
M

Martin

Hi!

The problem is that the childform looses the value of its property after it
has been closed. When your main asks the property's value I think it even
recreates an instance of the childform. At least it was that way in VB6.
What I would do is working the other way around. Create a procedure in the
main 'SetValue(Byval MyValue as string)' and let the childform do a callback
to this procedure. This callback you code in the FormClosing event. The
button can then simply do a Me.Close. Then there is no functional difference
between closing the form with the [X] and the command button.

Hth,
Martin
 
C

Chris Strug

Martin said:
Hi!

The problem is that the childform looses the value of its property after
it has been closed. When your main asks the property's value I think it
even recreates an instance of the childform. At least it was that way in
VB6. What I would do is working the other way around. Create a procedure
in the main 'SetValue(Byval MyValue as string)' and let the childform do a
callback to this procedure. This callback you code in the FormClosing
event. The button can then simply do a Me.Close. Then there is no
functional difference between closing the form with the [X] and the
command button.

Hth,
Martin

Martin,

Yes, that sounds like an excellent solution.

Many thanks for your help.

Cheers

Chris.
 
C

Claes Bergefall

Your code works for me. It does however, suffer from a a few (design and
runtime) problems

1. Open frmChild in the designer and set its AcceptButton property to
cmdReturnData (your button)
2. Next select the cmdReturnData button and set its DialogResult property to
OK
3. Remove the auxFrm_Load and cmdReturnData methods from frmChild
4. Remove the line "Dim frmChild As New frmChild" from FrmMain
5. Change cmdShowChild_Click in FrmMain to the following:
Private Sub cmdShowChild_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdShowChild.Click
Dim frmChild As New frmChild
frmChild.ShowDialog()
Me.txtRetrievedData.Text = frmChild.getString
frmChild.Dispose()
End Sub

You should consider adding a cancel button to your frmChild form. If so set
its DialogResult proeprty to Cancel and set the CancelButton property on
frmChild to that button. After that you can check the return value from
ShowDialog to determine which button the user pressed (the X in the corner
would generate the sama result as the cancel button)

/claes
 
C

Chris Strug

Claes Bergefall said:
Your code works for me. It does however, suffer from a a few (design and
runtime) problems

1. Open frmChild in the designer and set its AcceptButton property to
cmdReturnData (your button)
2. Next select the cmdReturnData button and set its DialogResult property
to OK
3. Remove the auxFrm_Load and cmdReturnData methods from frmChild
4. Remove the line "Dim frmChild As New frmChild" from FrmMain
5. Change cmdShowChild_Click in FrmMain to the following:
Private Sub cmdShowChild_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdShowChild.Click
Dim frmChild As New frmChild
frmChild.ShowDialog()
Me.txtRetrievedData.Text = frmChild.getString
frmChild.Dispose()
End Sub

You should consider adding a cancel button to your frmChild form. If so
set its DialogResult proeprty to Cancel and set the CancelButton property
on frmChild to that button. After that you can check the return value from
ShowDialog to determine which button the user pressed (the X in the corner
would generate the sama result as the cancel button)

/claes

"Chris Strug" <[email protected]> wrote in message

Claes,

Many thanks for your advuce - its greatly appreciated.

Regards

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top