当你把combox.properties.items.add(table.rows[i].itemarray[0]
+
"-"
+
table.rows[i].itemarray[1])这样的代码写完后,在在后面加上一句应该就可以了,this.combobox.selectedindex
=
0
这句话就表示默认选中的是第一项。如果你想默认选中第几项就把0改成那个
,注意:如果想默认显示第二项,那么把0改为1,以此类推
this.combobox.selectedindex
=n-1
n是第几项
需知comboBox里的项都是对象实例新建一个类
/// <summary>
/// 选择项类,用于ComboBox或者ListBox添加项
/// </summary>
public class ListItem:System.Object
{
private string id = string.Empty
public string Id
{
get { return id}
set { id = value}
}
private string name = string.Empty
public string Name
{
get { return name}
set { name = value}
}
public ListItem(string sid, string sname)
{
this.Id = sid
this.Name = sname
}
public override string ToString()
{
return this.Name
}
}
static void Main()
{
List<ListItem>items = new List<ListItem>()//添加项的集合
ListItem item = new ListItem("显示文字内容1","value值1")
ListItem item = new ListItem("显示文字内容2","value值2")
ListItem item = new ListItem("显示文字内容3","value值3")
items.Add(item)
comboBoxGroup.DisplayMember = "Name"
comboBoxGroup.ValueMember = "Id"
comboBoxGroup.DataSource = items
this.comboBoxGroup.SelectedIndex = 0
//下拉框事件
comboBoxGroup.SelectedIndexChanged += new EventHandler(comboBoxGroup_SelectedIndexChanged)
return 0
}
搞定~
}
VB使用AddItem 方法给ComboBox控件添加数据。
AddItem 方法,用于将项目添加到 ListBox 或 ComboBox 控件。不支持命名参数。
Private Sub Form_Click()Dim Entry, I, Msg ' 声明变量。
Msg = "点OK增加100项到ComboBox控件"
MsgBox Msg ' 显示信息。
For I = 1 To 100 ' 计数值从 1 到 100。
Entry = "Entry " & I ' 创建输入项。
Combo1.AddItem Entry ' 添加该输入项。
Next I
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)