|
-
Mar 10th, 2021, 01:23 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Get RadioButton Selection from another form
Hi All.. I have form1 as my main form, I have form2 as an alternate form that includes RadioButtons. To save my life, I cannot pass the radiobutton text to my Form1. This is how I have it set right now after many trials of different things. In Form1 the variable is always blank?
Form1: Click button on Form1 to open/show Form2 RadioButtons to make selection
Code:
Public Class Form1
Public rButtonSelect
Public Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
rButtonSelect = Form2.rButton
Msgbox(rButtonSelect)
End Sub
Form2:
Code:
Public Class Form2
Public Shared rButton As RadioButton
Public Sub btnSelect_Click(sender As Object, e As EventArgs) Handles btnSelect.Click
rButton = Panel1.Controls.OfType(Of RadioButton).FirstOrDefault(Function(r) r.Checked = True)
Me.Dispose()
End Sub
-
Mar 10th, 2021, 01:32 PM
#2
Re: Get RadioButton Selection from another form
Personally, I would not pass data to and from Forms like what you're doing.
However, I believe the immediate fix is to remove the Shared keyword.
-
Mar 10th, 2021, 01:42 PM
#3
Thread Starter
Addicted Member
Re: Get RadioButton Selection from another form
Thanks dday9... any thoughts on how you would get a radio button selection from another form?
-
Mar 10th, 2021, 01:52 PM
#4
Re: Get RadioButton Selection from another form
This is one approach. Note that it does NOT use the default instance of Form2.
Code:
Public Class Form1
Private Frm2Instance As Form2
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
ChkFrm2Instance()
End Sub
Private Sub ChkFrm2Instance()
If Frm2Instance Is Nothing OrElse Frm2Instance.IsDisposed Then
Frm2Instance = New Form2 'create an instance of Form2 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim s As String = Frm2Instance.RadioButton1.Text
Stop 'look at s
End Sub
End Class
-
Mar 10th, 2021, 02:13 PM
#5
Re: Get RadioButton Selection from another form
Usually what I do is setup a property on the Form you want to get the data from (in this case, Form2), add business logic to update the property, then when you need to get the property (in this case, on Form1) you would get the property.
Something along the lines of:
Code:
Public Class Form2
Public Property SelectedOption As String
Public Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each rb In Panel1.Controls.OfType(Of RadioButton)
AddHandler rb.CheckChanged, AddressOf Panel1RadioButton_Changed
Next
End Sub
Private Sub Panel1RadioButton_Changed(sender As Object, e As EventArgs)
Dim rb = DirectCast(sender, RadioButton)
If (rb.Checked) Then
SelectedOption.Text = rb.Text
End If
End Sub
End Class
Public Class Form1
Private frm2 As Form2
' frm2 = New Form2 somewhere else in the form
Public Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
MessageBox.Show(frm2?.SelectedOption)
End Sub
End Sub
-
Mar 10th, 2021, 04:37 PM
#6
Thread Starter
Addicted Member
Re: Get RadioButton Selection from another form
Thanks dday9 and dbasnett... I will give these a try. I appreciate your time and help
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|