problems trying to write back to a form

T

The One

Have created a form to pop up with 2 option I then wish to write the text that
is in the option button chose back to the original form using the code below but
it gives me an exception error so could any please help me.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim frm As Form1

Dim text As String

If RadioButton1.Checked = True Then

text = RadioButton1.Text

frm.lblName4.Text = text

frm.lblSkill4.Text = "50%"

ElseIf RadioButton2.Checked = True Then

text = RadioButton2.Text

frm.lblName4.Text = text

frm.lblSkill4.Text = "40%"

End If

Close()

End Sub

thanks in advance.
 
M

Michal Sampson [MSFT]

The line:

Dim frm as Form1

doesn't give you access to the main form. What you want to do is have the
code in your main form that pops up that dialog handle filling in the
values. Your dialog box class should implement public properties to expose
the data that the main form needs access to. Here is a short example. Lets
say your popup dialog has a textbox named TextBox1 and a submit button
named Button1. When the user hits Button1, you want the form to close and
the main form to show the data from that textbox in its own textbox called
MainTextBox.

Class PopupDialog
Private data as String
' Winforms code here
Private Sub Button1_Click(ByVal sender as Object, ByVal e as
ClickEventArgs) Handles Button1.Click
' We'll save the data the user has entered into a string
data = TextBox1.Text
Me.DialogResult = DialogResult.Ok
End Sub
Public ReadOnly Property DialogText as String
Get
Return data
End Get
End Property
End Class

Class MainForm
' Winforms stuff here
Private Sub ShowPopupDialog()
Dim popup as New PopupDialog
If popup.ShowDialog = DialogResult.Ok Then
MainTextBox.Text = popup.DialogText
End If
End Sub
End Class

This is just a little sample but you get the idea (I hope :)). Setting the
dialog result in the popup will cause it to close and return to the main
form since we used ShowDialog to display the dialog.

If you need more help, don't be afraid to ask.

--
Mike Sampson VB .Net Developer in Deployment
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
--------------------
 
T

The One

Cool thanks for that I'll give it a try.
I think I'm going to have to go back and learn some more on dot not.
Thanks again for that if get back if I have any problems with it.
 
T

The One

Thanks for that will give that I try.

Michal Sampson said:
The line:

Dim frm as Form1

doesn't give you access to the main form. What you want to do is have the
code in your main form that pops up that dialog handle filling in the
values. Your dialog box class should implement public properties to expose
the data that the main form needs access to. Here is a short example. Lets
say your popup dialog has a textbox named TextBox1 and a submit button
named Button1. When the user hits Button1, you want the form to close and
the main form to show the data from that textbox in its own textbox called
MainTextBox.

Class PopupDialog
Private data as String
' Winforms code here
Private Sub Button1_Click(ByVal sender as Object, ByVal e as
ClickEventArgs) Handles Button1.Click
' We'll save the data the user has entered into a string
data = TextBox1.Text
Me.DialogResult = DialogResult.Ok
End Sub
Public ReadOnly Property DialogText as String
Get
Return data
End Get
End Property
End Class

Class MainForm
' Winforms stuff here
Private Sub ShowPopupDialog()
Dim popup as New PopupDialog
If popup.ShowDialog = DialogResult.Ok Then
MainTextBox.Text = popup.DialogText
End If
End Sub
End Class

This is just a little sample but you get the idea (I hope :)). Setting the
dialog result in the popup will cause it to close and return to the main
form since we used ShowDialog to display the dialog.

If you need more help, don't be afraid to ask.

--
Mike Sampson VB .Net Developer in Deployment
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 

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