如何验证ComboBox [英] How to Validate ComboBox

查看:79
本文介绍了如何验证ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们



我写了一个函数来验证项目中的组合框。该功能适用​​于在设计时填充的组合框。但是当我在运行时通过数据库使用组合框时它不会起作用



我已经显示了组合框的功能和验证事件处理程序



功能:



Hi friends

I have written a function that validates comboboxes in my project. The function works fine with comboboxes that are populated at design time. But it will not work when I used it with comboboxes that via database at runtime

I have show the function and the validate event handler for comboboxes

The function:

 Public Function validateCombo(txt As ComboBox) As Boolean
 If Not txt.Items.Contains(txt.Text.ToString) Then
    Return False
 Else
    Return True
 End If
End Function





组合事件处理程序:





Combo event handler:

Private Sub cboFund_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles cboFund.Validating
       If Not bnformclosing Then
           If cboFund.Text <> String.Empty Then

               If validateCombo(cboFund) Then
                   e.Cancel = False
               Else
                   MessageBox.Show("The item: " & cboFund.Text & " is not a valid fund", "Invalid fund", MessageBoxButtons.OK, MessageBoxIcon.Information)
                   e.Cancel = True
               End If
           End If
       End If
   End Sub

推荐答案

ComboBox.Items.Contains()方法搜索对象集合绑定到ComboBox。添加字符串值时,可以通过提供字符串值作为参数来调用此方法。但是当绑定DataTable时,对象类型将是DaraRow。在这种情况下,您必须向ComboBox.Items.Contains()方法提供DataRow对象。



或者使用ComboBox.FindString()或ComboBox.FindStringExact ()。这是最简单的方法
ComboBox.Items.Contains() method search through the object collection which are bound to the ComboBox. When you add string values, this method can be called by providing a string value as parameter. But when you bind a DataTable, the object type will be DaraRow. You have to provide a DataRow object to ComboBox.Items.Contains() method in this scenario.

Or use ComboBox.FindString() or ComboBox.FindStringExact(). That is the easiest way


你可以使用Combobox下拉列表属性
You Can use the Combobox dropdown list property


这篇关于如何验证ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆