简体   繁体   中英

Textbox Appears According to Button Choice

I want a textbox to appear only when a radio-button is chosen. I tried using RadioButtonList but i couldn't get it to work, so I used separate radio buttons. However, it didn't work. Here is the code:

Protected Sub radItem_CheckedChanged(sender As Object, e As EventArgs) Handles radItem.CheckedChanged
    If radItem.Checked = True Then
        radClient.Checked = False
        radUser.Text = False
        btnSearch.Visible() = True
        txtSearch.Visible() = True
    End If
End Sub

Protected Sub radUser_CheckedChanged(sender As Object, e As EventArgs) Handles radUser.CheckedChanged
    If radUser.Checked = True Then
        radItem.Checked = False
        radClient.Text = False
        btnSearch.Visible() = True
        txtSearch.Visible() = True
    End If
End Sub

Protected Sub radClient_CheckedChanged(sender As Object, e As EventArgs) Handles radClient.CheckedChanged
    If radClient.Checked = True Then
        radItem.Checked = False
        radUser.Text = False
        btnSearch.Visible() = True
        txtSearch.Visible() = True
    End If
End Sub

I'm pretty sure there's a much efficient way of doing this anyway.

As tymeJV mentioned, remove () in the Visible()

Also, you do not need to disable other radioButtons, unless they were specifically grouped separately.

Since you are always making button/textbox visible regardless of which radiobutton was selected, you do not need to check for radio button value. Your event should look like this (change others to follow the suit)

Protected Sub radItem_CheckedChanged(sender As Object, e As EventArgs) Handles radItem.CheckedChanged
    btnSearch.Visible = True
    txtSearch.Visible = True
End Sub

You should use the Click Event and since you are handling the same multiple radio buttons

Protected Sub rad_Click(sender As Object, e As EventArgs) Handles radItem.Click, radUser.Click, radClient.Click
     If Ctype(sender, RadioButton).Checked = False Then Exit Sub
     Select Case Ctype(sender, RadioButton).Name
          Case "radItem"
               radUser.Text = False 'You are setting the Text value to False?
                                    'Are you sure about this?

          Case "radUser"
               radClient.Text = False

          Case Else 'radClient
               radUser.Text = False
     End Select

     'You are doing this regardless of which radio button is checked
     radItem.Checked = False 'This is weird. You wont be able to use radItem.

     'I dont think you need this two lines anymore. There is always 1 RadioButton that is checked
     btnSearch.Visible = True
     txtSearch.Visible = True
End Sub

Fares, after adding the 'GroupName' property to your radio buttons, you will not need to set the Text or Checked state of the other Radios, it will update automatically.

for Show/Hide Textboxes, you only need to set txtSearch.Visible() = True OR txtSearch.Visible() = False

Set AutoPostBack property to true in each radio buttons. then it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM