By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,041 Members | 1,018 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,041 IT Pros & Developers. It's quick & easy.

Basic Question - Working with forms

P: n/a
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
Mar 30 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
"Chris Strug" <so********@hotmail.com> wrote in message
news:uO******************@TK2MSFTNGP09.phx.gbl...
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

Mar 30 '06 #2

P: n/a
"Martin" <x@y.com> wrote in message
news:ej****************@TK2MSFTNGP12.phx.gbl...
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.
Mar 30 '06 #3

P: n/a
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" <so********@hotmail.com> wrote in message
news:uO******************@TK2MSFTNGP09.phx.gbl...
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

Mar 30 '06 #4

P: n/a

"Claes Bergefall" <lo*****@nospam.nospam> wrote in message
news:Os**************@tk2msftngp13.phx.gbl...
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" <so********@hotmail.com> wrote in message


Claes,

Many thanks for your advuce - its greatly appreciated.

Regards

Chris.
Mar 31 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.