Results 1 to 6 of 6

Thread: [RESOLVED] Get RadioButton Selection from another form

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    Resolved [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

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    Re: Get RadioButton Selection from another form

    Thanks dday9... any thoughts on how you would get a radio button selection from another form?

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2021
    Posts
    191

    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
  •  



Click Here to Expand Forum to Full Width