Doonze Doonze - 4 months ago 17
Vb.net Question

How to mark a radio button as checked with no action performed?

I have a form with

CheckBox
and
RadioButton
controls. When people click on the buttons/boxes it saves the results to my DB. Later, I might want someone else to retrieve the form and make changes or continue using it.

When I pull the info back from the database into a new "search" form (that looks exactly like the form used to submit the data), I want the radio buttons to reflect the state they were in when the form was saved. So if a box was TRUE (checked) when saved, I want it to show TRUE (checked) on the new form.

HOWEVER, when I use

radiobutton.checked = True


It DOES mark my button as checked.... it ALSO acts like the button was clicked again. So I have duplicate results in my database. It will keep doing this every time the form is opened.

So, I want a way to mark the box as checked (the state it was in when saved) but NOT CAUSE THE EVENT AGAIN.

Here's an example:

str = "SELECT * from OpenEvents WHERE EventID = " & eventId & ""
Dim cmd6 As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd6.ExecuteReader()
While dr.Read()
contactType = dr("ContactType").ToString
abend = dr("Abend").ToString
paged = dr("Paged").ToString
shortSummary = dr("ShortSummary").ToString
eventNotes = dr("EventNotes").ToString
impacting = dr("Impacting").ToString
L1Engaged = dr("L1Engaged").ToString
L2Engaged = dr("L2Engaged").ToString
managerEngaged = dr("ManagerEngaged")
IncTicket = dr("IncTicket").ToString
End While
myConnection.Close()

If contactType = "Call" Then
CallRadioButton.Checked = True
ElseIf contactType = "IM" Then
IMRadioButton.Checked = True
ElseIf contactType = "Other" Then
OtherRadioButton.Checked = True
ElseIf contactType = "Alert" Then
AlertRadioButton.Checked = True
Else
End If

If abend = "True" Then AbendCheckBox.Checked = True
If paged = "True" Then PagedYes.Checked = True
ShortSummaryTextBox.Text = shortSummary
DetailsTextBox.Text = eventNotes
If impacting = "True" Then ImpactingYesRadioButton.Checked = True
If L1Engaged = "True" Then L1YesRadioButton.Checked = True
If L2Engaged = "True" Then L2YesRadioButton.Checked = True
If managerEngaged = "True" Then ManagerYesRadioButton.Checked = True
IncTicketTextBox.Text = IncTicket

Answer

You could use Click event instead of CheckedChanged event.

Doing so, if you want to raise the event linked to the RadioButton you can use PerformClick instead of Checked = True.

If you want to change checked status without raising the event you can use Checked = True.

Here a little example:

Private Sub RadioButton1_Click(sender As Object, e As EventArgs) Handles RadioButton1.Click, RadioButton2.Click, RadioButton3.Click
    Select Case sender.name
        Case Is = "RadioButton1"
            Me.Label1.BackgroundColor = Color.Yellow
            Me.Label2.BackgroundColor = Color.Gray
            Me.Label3.BackgroundColor = Color.Gray
        Case Is = "RadioButton2"
            Me.Label1.BackgroundColor = Color.Gray
            Me.Label2.BackgroundColor = Color.Yellow
            Me.Label3.BackgroundColor = Color.Gray
        Case Is = "RadioButton3"
            Me.Label1.BackgroundColor = Color.Gray
            Me.Label2.BackgroundColor = Color.Gray
            Me.Label3.BackgroundColor = Color.Yellow
    End Select
End Sub

Private Sub Btn_ChangeSelected_Click(sender As Object, e As EventArgs) Handles Btn_ChangeSelected.Click
    Me.RadioButton1.Checked = True
End Sub

Private Sub Btn_PerformClick_Click(sender As Object, e As EventArgs) Handles Btn_PerformClick.Click
    Me.RadioButton1.PerformClick()
End Sub

If you click Btn_ChangeSelected RadioButton1 will be checked but no event will be raised.

If you click Btn_PerformClick RadioButton1 will be checked and RadioButton1.click event will be raised.

Comments