如何使用单选按钮从列表框中获取信息到文本框

时间:2014-10-29 01:01:46

标签: vb.net textbox listbox radio-button

我一直想弄清楚我做错了什么。我有两个单选按钮,一个用于新用户,另一个用于现有用户。新用户单选按钮将输入的信息输入文本框并添加到列表框中。现有的用户按钮应该采用列表框中选择的行并将其分解,然后将其放回我的文本框中。我已经为单选按钮编写了代码,但它们无法正常工作。如何才能正确处理信息并显示在文本框中?我尝试过分裂和功能,但这是不成功的。有人可以帮忙吗?这就是我对单选按钮所用的内容,到目前为止,如果你需要,我可以发布剩下的代码。

Private Sub rbtnNew_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtnNew.CheckedChanged
    lstCustomer.Items.Add(RevName(txtName.Text.ToUpper) & " , " & (txtAddress.Text.ToUpper) & " , " & (txtCity.Text.ToUpper))

End Sub

Private Sub rbtnExisting_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtnExisting.CheckedChanged
    Dim name1 As String
    Dim address2 As String
    Dim city2 As String
    Dim output() As String

    name1 = txtName.Text
    address2 = txtAddress.Text
    city2 = txtCity.Text
    output = Split(lstCustomer.SelectedItem.ToString(), ",")

    txtName.Text = output(0)
    txtAddress.Text = output(1)
    txtCity.Text = output(2)



    lstResults.Items.Clear()
    txtChairs.Clear()
    txtSofas.Clear()

End Sub

结束班

2 个答案:

答案 0 :(得分:1)

无论是选中还是未选中,都可以触发radiobutton checkedchanged事件。确保在事件函数中检查radiobutton的值/属性。

编辑:对于列表框根本没有项目的情况,您是否有错误检查代码?由于列表框中没有选择任何项目,因此会导致ListBox.SelectedItem返回null或者Split()函数无法处理的内容。

http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selecteditem%28v=vs.110%29.aspx

另外,使用系统提供的字符串操作函数,例如join(),concat()。

http://msdn.microsoft.com/en-us/library/aa903372%28v=vs.71%29.aspx

从split()中检查结果字符串数组的大小/长度。

答案 1 :(得分:1)

在这种情况下,当AutoCheck属性设置为True时,无线电按钮非常难以管理。无论如何,我给你几个可能的解决方案:

解决方案#1(AutoCheck属性设置为True):

Private Sub rbtnNew_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbtnNew.CheckedChanged

    Static wasInvoked As Boolean

    If Not wasInvoked Then

        If rbtnNew.Checked Then
            lstCustomer.Items.Add(RevName(txtName.Text.ToUpper) & " , " & (txtAddress.Text.ToUpper) & " , " & (txtCity.Text.ToUpper))
        Else
            If lstCustomer.SelectedItem Is Nothing Then

                wasInvoked = True
                rbtnNew.Checked = True
                wasInvoked = False

                MessageBox.Show("Please select a customer from the list.")
            End If
        End If
    End If
End Sub


Private Sub rbtnExisting_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbtnExisting.CheckedChanged

    If rbtnExisting.Checked Then

        Dim name1 As String
        Dim address2 As String
        Dim city2 As String
        Dim output() As String

        name1 = txtName.Text
        address2 = txtAddress.Text
        city2 = txtCity.Text
        output = Split(lstCustomer.SelectedItem.ToString(), ",")

        txtName.Text = output(0)
        txtAddress.Text = output(1)
        txtCity.Text = output(2)


        lstResults.Items.Clear()
        txtChairs.Clear()
        txtSofas.Clear()
    End If
End Sub

解决方案#2(RadioButtons的AutoCheck属性设置为False):

Private Sub rbtnNew_CheckedChanged(sender As Object, e As System.EventArgs) Handles rbtnNew.CheckedChanged
    rbtnExisting.Checked = Not rbtnNew.Checked
End Sub

Private Sub rbtnExisting_CheckedChanged(sender As Object, e As System.EventArgs) Handles rbtnExisting.CheckedChanged
    rbtnNew.Checked = Not rbtnExisting.Checked
End Sub

Private Sub rbtnNew_Click(sender As Object, e As System.EventArgs) Handles rbtnNew.Click

    If Not rbtnNew.Checked Then
        rbtnNew.Checked = True
        lstCustomer.Items.Add(RevName(txtName.Text.ToUpper) & " , " & (txtAddress.Text.ToUpper) & " , " & (txtCity.Text.ToUpper))
    End If
End Sub

Private Sub rbtnExisting_Click(sender As Object, e As System.EventArgs) Handles rbtnExisting.Click

    If Not rbtnExisting.Checked Then

        If lstCustomer.SelectedItem Is Nothing Then

            MessageBox.Show("Please select a customer from the list.")
        Else
            rbtnExisting.Checked = True

            Dim name1 As String
            Dim address2 As String
            Dim city2 As String
            Dim output() As String

            name1 = txtName.Text
            address2 = txtAddress.Text
            city2 = txtCity.Text
            output = Split(lstCustomer.SelectedItem.ToString(), ",")

            txtName.Text = output(0)
            txtAddress.Text = output(1)
            txtCity.Text = output(2)


            lstResults.Items.Clear()
            txtChairs.Clear()
            txtSofas.Clear()
        End If
    End If