gpt4 book ai didi

.net - 尝试验证我的组合框时遇到问题

转载 作者:行者123 更新时间:2023-12-01 11:51:30 30 4
gpt4 key购买 nike

我决定在我的组合框中添加一些验证,我想要实现的是确保用户只能输入组合框中的字段,但我现在遇到的问题是,如果用户单击组合框,不输入任何内容并尝试离开组合框,消息框出现。

Private Sub Combobox1_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Combobox1.Validating
If Combobox1.Items.Contains(Combobox1.Text) = False Then
e.Cancel = True
End If
End Sub

Private Sub Combobox1_Leave(sender As Object, e As System.EventArgs) Handles Combobox1.Leave
If Combobox1.Items.Contains(Combobox1.Text) = False Then
Combobox1.Select()
MessageBox.Show("select item from combobox")
End If
End Sub

如前所述,编码确实有效,但我试图确保如果用户未在组合框中输入任何内容,则不会出现消息框。

最佳答案

根据您的评论,我认为您需要做的就是添加一个空字符串检查:

Private Sub ComboBox1_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles ComboBox1.Validating
If ComboBox1.Items.Contains(ComboBox1.Text) = False Then
e.Cancel = (ComboBox1.Text <> String.Empty)
End If
End Sub

Private Sub ComboBox1_Leave(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.Leave
If Not ComboBox1.Items.Contains(ComboBox1.Text) Then
If ComboBox1.Text <> String.Empty Then
ComboBox1.Select()
MessageBox.Show("select item from combobox")
End If
End If
End Sub

关于.net - 尝试验证我的组合框时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11058883/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com