MONO GTK#:尝试删除组合框中的文本,然后将新文本添加到组合框中,但仍保留一些旧文本

发布于 2024-12-11 03:15:09 字数 974 浏览 0 评论 0原文

我试图首先删除组合框中的所有内容。然后在其前面添加文本,但保留一些旧文本。有没有办法重置或清除组合框?或者我怎样才能最好地实现这一目标?

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);    

        }
    }

I am trying to first remove everything in the ComboBox. And then prepend text to it, but some of the old text remains. Is there a way to RESET or CLEAR the ComboBox? Or how can I best achieve this?

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);    

        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风无影 2024-12-18 03:15:09

我明白了这一点。只需创建一个新的空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);
        }

I figured this out. Simply create a new empty ListStore (I call it ClearList) and then make the combobox.Model equal to the ClearList. Then prepend or append the text as normal to the combobox.

 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);
        }
千秋岁 2024-12-18 03:15:09

是的,尝试组合框.Clear()。您可以在 http://docs.go-mono.com/ 找到 Gtk# 的文档

Yes, try combobox.Clear(). You can find docs for Gtk# at http://docs.go-mono.com/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文