Winfrom ComboBox無法設置Text,利用Text屬性設置默認值問題

問題

使用comboBox時,想用Text屬性爲其設置默認值,但是無論在哪裏設置都無法爲其設置默認值

原因

不能夠直接對 combobox.text 進行設置,需要先設置combobox.datasource 的值,然後再對 combobox.text 進行設置。
例如:

// 此時 combobox.datasource = null; "test" 不包含於 combobox.datasource
// 所以此時仍然顯示爲空值
combobox.text = "test";

正確示例:

List<string> list = new List<string>();
list.add("test1");
list.add("test2");

combobox.datasource = list;
combobox.text = "test2";
// "test2" 包含於 combobox.datasource({"test1","test2"})
// 此時將顯示默認值test2

注意
dataSource中的各項類型必須爲字符串類型,否則也將無法設置,因爲在dataSource中無法找到該項。

示例:

List<int> list = new List<int>();
list.Add(1000);
list.Add(2000);
list.Add(5000);
list.Add(8000);
this.comRowsPerPage.DataSource = list;
this.comRowsPerPage.Text = "8000";
// 因爲與DataSorce中的類型不一致,將顯示空

正確示例:

List<string> list = new List<string>();
list.Add("1000");
list.Add("2000");
list.Add("5000");
list.Add("8000");
this.comRowsPerPage.DataSource = list;
this.comRowsPerPage.Text = "5000";

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章