组合框默认值 [英] combobox default value
问题描述
hi;
我有一个vb.net应用程序,并且有一个组合框,其中应该包含一个显示成员(文本)和一个值成员(整数).
问题是:
我想在加载表单时为该组合框设置默认值,但是在动态创建表单和组合框时没有加载事件.我已经使用了combobox.selectedvalue = 6,但是它不起作用.
注意:我不能使用selectedindex属性,因为有时该值来自数据库表主键,该键与组合框索引不同.
代码:
hi;
I have a vb.net application, and I have a combobox which should contains a display member (text) and a value member (integer).
the problem is:
I want to set a default value to that combobox when the form load but I don''t have a load event as I create the form and the combobox dynamically. i have used the combobox.selectedvalue =6 but it doesn''t work.
note: I can''t use the selectedindex property because sometimes the value comes from a DB table primary key which is not the same as the combobox index.
the code:
Private Sub fill_combo(ByRef combo As ComboBox, ByVal nodes As HashMap)
Dim comboData = New BindingList(Of KeyValuePair(Of Integer, String))
nodes.movefirst()
Do While Not nodes.eof
If check_atrbValue(nodes.key, nodes.value, "string", "other") Then
comboData.Add(New KeyValuePair(Of Integer, String)(nodes.key, nodes.value))
End If
nodes.movenext()
Loop
combo.DataSource = comboData
combo.ValueMember = "Key"
combo.DisplayMember = "Value"
combo.selectedvalue=2
End Sub
推荐答案
您需要在所选值中使用一个值,而不是键.
因此,请使用名称(来自DisplayMember)而不是键.
You need to use a value in the selected value rather than the key.
So use names (from DisplayMember) instead of keys.
在表单加载事件中尝试将此操作写为
combo.text =您的期望值"
Try This on form load event write it
combo.text="Your Desired value"
这篇关于组合框默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!