我想先删除ComboBox中的所有内容。然后将文本添加到文本中,但仍保留一些旧文本。有没有办法重置或清除ComboBox?或者我怎样才能最好地实现这一目标?
public void GetBadgeName ()
{
try
{
int i = 0;
while (i < 200)
{
cmb_SelectBadge.RemoveText(i);
++i;
}
string connectionString = "URI=file:SIGN.sqlite";
IDbConnection dbcon;
dbcon = (IDbConnection) new SqliteConnection(connectionString);
dbcon.Open();
IDbCommand dbcmd = dbcon.CreateCommand();
string sql =
"SELECT BadgeName " +
"FROM Badge";
dbcmd.CommandText = sql;
IDataReader reader = dbcmd.ExecuteReader();
while(reader.Read()) {
string BadgeName = reader.GetString (0);
cmb_SelectBadge.PrependText(BadgeName);
}
reader.Close();
reader = null;
dbcmd.Dispose();
dbcmd = null;
dbcon.Close();
dbcon = null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
答案 0 :(得分:2)
我想出来了。只需创建一个新的空ListStore(我称之为ClearList),然后使combobox.Model等于ClearList。然后将文本正常添加或添加到组合框中。
ListStore ClearList = new ListStore(typeof(string),typeof(string));
cmb_SelectBadge.Model = ClearList;
// This example retrieves each BadgeName from the Badge table (see question)
while(reader.Read()) {
string BadgeName = reader.GetString (0);
cmb_SelectBadge.PrependText(BadgeName);
}
答案 1 :(得分:0)
是的,尝试使用combobox.Clear()。您可以在http://docs.go-mono.com/
找到Gtk#的文档