473,385 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Basic Question - Working with forms

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
4 1326
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
"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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Basil Fawlty | last post by:
Hi everyone, I could use some help in converting a simple Liberty BASIC program toa VB.NET 2003 SE program. I have built the simple form with a label, text box and button, Under the button I...
13
by: usenet | last post by:
How and where can one find out about the basics of VB/Access2003 syntax? I am a died in the wool C/C++/Java Linux/Unix programmer and I am finding it difficult to understand the program format...
4
by: MikeB | last post by:
I've been all over the net with this question, I hope I've finally found a group where I can ask about Visual Basic 2005. I'm at uni and we're working with Visual Basic 2005. I have some books, ...
7
by: garyusenet | last post by:
This is the first time i've worked with openfile dialog. I'm getting a couple of errors with my very basic code. Can someone point out the errors in what i've done please....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.