[英]Accessing ComboBox.Items with Contains method for DataBound ComboBox
我想對照在運行時分配了數據源的ComboBox中的項目列表檢查當前ComboBox.text值。 如果文本與列表中的項目不匹配,則它將選擇列表中的第一項。
' This Load function is just here to give an example of how I bound the ComboBox '
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.myComboBox.DisplayMember = "something"
Me.myComboBox.ValueMember = "otherthing"
Me.myComboBox.DataSource = Me.myDataTable
End Sub
' This function is copy/pasted directly out of my code '
Private Sub myComboBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles myComboBox.Validating
If Not DirectCast(sender, ComboBox).Items.Contains(DirectCast(sender, ComboBox).Text) Then
DirectCast(sender, ComboBox).SelectedValue = -1
End If
End Sub
但是上述代碼無法按預期方式工作,因為在運行時檢查myComboBox.Items
屬性時,它實際上是System.Data.DataRowView
對象的集合。 因此,基本上它將String
與DataRowView.ToString()
進行比較,除非myComboBox.Text
值實際上是“ System.Data.DataRowView”,否則它始終為false。
“嘿,讓我們做一個擴展方法來搜索DataViewRow
所有項目,看看我請求的值是否在里面!”但是它並沒有按計划進行...
<Extension()>
Private Function Contains_databound(ByVal items As ComboBox.ObjectCollection, ByVal value As Object) As Boolean
Dim Success As Boolean = False
For Each itm In items
Dim item As DataRowView = Nothing
Try
item = DirectCast(itm, DataRowView)
Catch ex As Exception
Throw New Exception("Attempted to use a Contains_databound method on a non databound object", New Exception(ex.Message))
End Try
If Not IsNothing(item) Then
For Each rowItem In item.Row.ItemArray
Dim v1 As String = TryCast(rowItem, String)
Dim v2 As String = TryCast(value, String)
If Not IsNothing(v1) And Not IsNothing(v2) Then
If v1.Equals(v2) Then
Success = True
End If
End If
Next
End If
Next
Return Success
End Function
我知道這看起來很奇怪,但是如果未分隔<Extension()>
部分,則整理代碼將無法正常工作。 當我嘗試在我的主代碼中使用擴展方法時,我得到一個關於我的擴展方法不是ComboBox.ObjectCollection
成員的錯誤,這對我來說似乎是虛假的,因為在擴展方法中,我具體是說第一個參數是ComboBox.ObjectCollection
。
Private Sub myComboBox_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles myComboBox.Validating
If DirectCast(sender, ComboBox).Items.Contains_databound(DirectCast(sender, ComboBox).Text) Then
DirectCast(sender, ComboBox).SelectedValue = -1
End If
End Sub
如何確定用戶輸入到數據綁定的ComboBox中的文本在DataSource的項目列表中?
旁注:C#答案很好,只要它們具有VB.NET副本即可
您實際上要確定的是任何項目的顯示文本是否與其他值匹配。 要獲取項目的顯示文本,請調用GetItemText
。 我將舉一個例子。
這是可以完成這項工作的擴展方法:
Imports System.Runtime.CompilerServices
Public Module ComboBoxExtensions
<Extension>
Public Function ContainsItemText(source As ComboBox, itemText As String) As Boolean
Return source.Items.Cast(Of Object).Any(Function(item) source.GetItemText(item) = itemText)
End Function
End Module
我認為以下代碼代表了您所描述的內容。 基本上看btn.Clicked事件代碼。
public class FormAB : Form {
ComboBox combo = new ComboBox { DropDownStyle = ComboBoxStyle.DropDown, Dock = DockStyle.Top };
Button btn = new Button { Text = "Button", Dock = DockStyle.Top };
public FormAB() {
Controls.Add(btn);
Controls.Add(combo);
DataTable table = new DataTable();
table.Columns.Add("FirstName");
table.Columns.Add("BirthDate", typeof(DateTime));
table.Rows.Add("Alan", DateTime.Today.AddYears(-20));
table.Rows.Add("Bob", DateTime.Today.AddYears(-35));
combo.DataSource = table;
combo.DisplayMember = "FirstName";
combo.ValueMember = "FirstName";
btn.Click += delegate {
Object rv = combo.Items.Cast<Object>().Where(r => combo.GetItemText(r) == "Bob").FirstOrDefault();
// either/or
//Object rv = combo.Items.Cast<DataRowView>().Where(r => (String) r[combo.DisplayMember] == "Bob").FirstOrDefault();
if (rv == null && combo.Items.Count > 0)
rv = combo.Items[0];
combo.SelectedItem = rv;
};
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.