Home All Groups Group Topic Archive Search About

Basic Question - Working with forms

Author
30 Mar 2006 11:24 AM
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

Author
30 Mar 2006 11:41 AM
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


Show quoteHide quote
"Chris Strug" <solace1***@hotmail.com> wrote in message
news:uOyW%23x%23UGHA.4952@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
>
Author
30 Mar 2006 12:36 PM
Chris Strug
Show quote Hide quote
"Martin" <x@y.com> wrote in message
news:ejMSc7%23UGHA.5148@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.
Author
30 Mar 2006 3:23 PM
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

Show quoteHide quote
"Chris Strug" <solace1***@hotmail.com> wrote in message
news:uOyW%23x%23UGHA.4952@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
>
Author
31 Mar 2006 8:21 AM
Chris Strug
Show quote Hide quote
"Claes Bergefall" <louplou@nospam.nospam> wrote in message
news:OssXi2AVGHA.4660@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" <solace1***@hotmail.com> wrote in message

Claes,

Many thanks for your advuce - its greatly appreciated.

Regards

Chris.