问题解决--设置ComboBox缺省值
'seting the combobox default value
ComboBox1.Text = ComboBox1.Items.Item(1)
ComboBox2.Text = ComboBox2.Items.Item(4)
ComboBox3.Text = ComboBox3.Items.Item(0)
ComboBox4.Text = ComboBox4.Items.Item(3)
ComboBox5.Text = ComboBox5.Items.Item(0)
如何:在 Windows 窗体 ComboBox 控件、ListBox 控件或 CheckedListBox 控件中添加或移除项
可以以多种方式向 Windows 窗体组合框、列表框或选中列表框添加项,因为这些控件可以绑定到多种数据源。但本主题 介绍最简单的方法,该方法无需进行数据绑定。所显示的项通常为字符串;但是可使用任何对象。控件中显示的文本是对象的 ToString 方法所返回的值。
添加项
使用 ObjectCollection 类的 Add 方法向列表添加字符串或对象。使用 Items 属性引用集合:
Visual Basic 复制代码
ComboBox1.Items.Add("Tokyo")
C# 复制代码
comboBox1.Items.Add("Tokyo");
J# 复制代码
comboBox1.get_Items().Add("Tokyo");
C++ 复制代码
comboBox1->Items->Add("Tokyo");
- 或 -
使用 Insert 方法在列表中所需位置插入字符串或对象:
Visual Basic 复制代码
C# 复制代码
checkedListBox1.Items.Insert(0, "Copenhagen");
J# 复制代码
checkedListBox1.get_Items().Insert(0, "Copenhagen");
C++ 复制代码
checkedListBox1->Items->Insert(0, "Copenhagen");
- 或 -
向 Items 集合分配整个数组:
Visual Basic 复制代码
Dim ItemObject(9) As System.Object
Dim i As Integer
For i = 0 To 9
ItemObject(i) = "Item" & i
Next i
ListBox1.Items.AddRange(ItemObject)
C# 复制代码
System.Object[] ItemObject = new System.Object[10];
for (int i = 0; i <= 9; i++)
{
ItemObject[i] = "Item" + i;
}
listBox1.Items.AddRange(ItemObject);
J# 复制代码
System.Object[] ItemObject = new System.Object[10];
for(int i=0;i <= 9;i ++)
{
ItemObject .set_Item( i , "Item" + i );
}
listBox1.get_Items().AddRange(ItemObject);
C++ 复制代码
Array<System::Object^>^ ItemObject = gcnew Array<System::Object^>(10);
for (int i = 0; i <= 9; i++)
{
ItemObject[i] = String::Concat("Item", i.ToString());
}
listBox1->Items->AddRange(ItemObject);
移除项
调用 Remove 或 RemoveAt 方法来删除项。
Remove 有一个参数可指定要移除的项。 RemoveAt 移除具有指定的索引号的项。
Visual Basic 复制代码
' To remove item with index 0:
ComboBox1.Items.RemoveAt(0)
' To remove currently selected item:
ComboBox1.Items.Remove(ComboBox1.SelectedItem)
' To remove "Tokyo" item:
ComboBox1.Items.Remove("Tokyo")
C# 复制代码
// To remove item with index 0:
comboBox1.Items.RemoveAt(0);
// To remove currently selected item:
comboBox1.Items.Remove(comboBox1.SelectedItem);
// To remove "Tokyo" item:
comboBox1.Items.Remove("Tokyo");
J# 复制代码
// To remove item with index 0:
comboBox1.get_Items().RemoveAt(0);
// To remove currently selected item:
comboBox1.get_Items().Remove(comboBox1.get_SelectedItem());
// To remove "Tokyo" item:
comboBox1.get_Items().Remove("Tokyo");
C++ 复制代码
// To remove item with index 0:
comboBox1->Items->RemoveAt(0);
// To remove currently selected item:
comboBox1->Items->Remove(comboBox1->SelectedItem);
// To remove "Tokyo" item:
comboBox1->Items->Remove("Tokyo");
移除所有项
调用 Clear 方法从集合移除所有项:
Visual Basic 复制代码
ListBox1.Items.Clear()
C# 复制代码
listBox1.Items.Clear();
J# 复制代码
listBox1.get_Items().Clear();
C++ 复制代码
listBox1->Items->Clear();
请参见
任务
如何:对 Windows 窗体 ComboBox 控件、ListBox 控件或 CheckedListBox 控件的内容排序
参考
ComboBox
ListBox
CheckedListBox
概念
何时使用 Windows 窗体 ComboBox 而非 ListBox
其他资源
用于列出选项的 Windows 窗体控件
====================================================
====================================================
订阅免费电子杂志〈名人传记〉
每期发送电子图书,广读博览融汇贯通。阅读电子书时,请注意保护视力。╭○═○╮╭◎═◎╮╭●═●╮同步发行《名人传记简装版》简装版只包含附件联接地址,大小控制在80k以内。同时发送《热点》期刊
1 邮件订阅方式
向下面的邮件地址发一封信,邮件正文(不是标题)填写subscribe zhuanji
2.主页订阅
主页地址一
http://list.cn99.com/cgi-bin/getlsts?listname=zhuanji
主页地址二 http://yjxsuccess.blog.hexun.com/