Question

I am creating radioButton based on the email count when form will open and user will checked the radio Button then that emailId i am storing in config file dynamically.This functionality I am implementing in outlook add in

when outlook will closed and again it will open then i am getting that last selected mail ID from Config file and storing in string but i am not able to checked that particular emailID those i get from config because radiobutton i have created dynamically.when every time form will open i want to checked last selected radioButton even if i closed the outlook also and again i open i need to check last selected Radio Button.

This is my dynamically created radio buttons

    For Each email As String In Arremailslist
        Dim rb As New RadioButton
        rb.Name = email
        rb.Text = email
        rb.AutoSize = True

        dynamictablelayout.SetColumn(rb, 0)
        dynamictablelayout.SetRow(rb, i)
        dynamictablelayout.Controls.Add(rb)
        i = i + 1
    Next

and this method i am calling in Form_Load Event

This is my Button click event code for checking which email ID checked and updating in config file

Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
    selectedmailID = Nothing
    For Each c As Control In dynamictablelayout.Controls
        If c.GetType Is GetType(RadioButton) Then
            Dim rb As RadioButton = DirectCast(c, RadioButton)
            If rb.Checked Then
                selectedmailID = rb.Text
                updateconfige(selectedmailID)
                Exit For
            End If
        End If
    Next
    Me.Close()
End Sub
Was it helpful?

Solution

Assuming that you have the updated e-mail id string in a variable, say selectedEmailIdFromConfig

Then your code snippet in Form_Load below should look like this

   For Each email As String In Arremailslist
            Dim rb As New RadioButton
            rb.Name = email
            rb.Text = email
            rb.AutoSize = True

            'Check the radiobutton by string equality check
            If selectedEmailIdFromConfig = email Then
                rb.Checked = True
            End If


            dynamictablelayout.SetColumn(rb, 0)
            dynamictablelayout.SetRow(rb, i)
            dynamictablelayout.Controls.Add(rb)
            i = i + 1
        Next

I hope this helped.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top