【问题标题】:Having issue regarding to checklistbox有关于复选框的问题
【发布时间】:2014-04-01 04:00:10
【问题描述】:

我有一个checkedListBox 和一个TextBox...当我检查checkedListBox 中的项目时,它会显示TextBox 中各个项目的值...当我检查checkedListBox 中的多个项目时,它会显示各个项目的值在以 {,}"逗号"

分隔的 TextBox 中

现在我的问题是,当我取消选中文本框中的项目时,它必须从文本框中删除相应未选中项目的值......还请告诉我如何从末尾删除“逗号”{,}以编程方式显示文本框

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=email_client;Integrated Security=True");
        SqlCommand command = new SqlCommand("Select * FROM address_book ", connection);

        try
        {
            connection.Open();
            {
                SqlDataReader drd = command.ExecuteReader();

                while (drd.Read())
                {
                    this.checkedListBox1.Items.Add(drd.GetString(0).ToString());
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }

        connection.Close();
    }

    private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=email_client;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from address_book where name='" + checkedListBox1.Text + "'", con);
        SqlDataReader dr;
        dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            textBox1.Text += Convert.ToString(dr["id"] + ",");
        }
        dr.Close();
    }

    private void textBox1_Enter(object sender, EventArgs e)
    {
        ToolTip tt = new ToolTip();
        tt.SetToolTip(textBox1, "sorry");
    }
}

【问题讨论】:

  • 好的先生,我会遵循这个编辑规则.....但是现在你能给我一个完美的答案......@ pravprab

标签: c# sql-server winforms


【解决方案1】:

首先,如果您想显示“选中”项目,则不应使用SelectedIndexChanged 事件。请改用ItemCheck

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)

然后使用CheckedListBox.CheckedItems 检索所有被检查的项目:

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
    StringBuilder stringBuilder = new StringBuilder();

    foreach (var item in checkedListBox1.CheckedItems)
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=email_client;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand(string.Format("select * from address_book where name='{0}'", item), con);
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            stringBuilder.Append(Convert.ToString(dr["id"] + ","));
        }
        dr.Close();
    }

    textBox1.Text = stringBuilder.ToString().TrimEnd(',');
}

记得使用string.TrimEnd() 修剪最后一个逗号。

嗯,这不是最有效的方法,因为当其中一项发生更改时,您需要检查每个选中的项目。您可以使用Dictionary 来维护名称-ID 对——至少当您取消选中某个项目时不需要执行SQL 查询,对吧? :)

【讨论】:

  • 谢谢先生......你给了我一个完美的代码............但是使用 SelectedIndexChanged 而不是 ItemCheck 给了我确切的解决方案......跨度>
  • SelectedIndexChanged 将在您选择一个项目时被调用,也就是说,当您专注于该项目而不检查它时(单击它的内容,而不是复选框)。并且 ItemCheck 将仅在选中或未选中项目时调用。在您的问题中,据说“当我 Checked 项目在 checkedListBox 中时,它会显示 TextBox 中相应项目的值”。我认为SelectedIndexChanged 是你需要的,而不是SelectedIndexChanged
  • 希望我没有误解您的要求。
【解决方案2】:

要删除,

textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);

但理想情况下,我会将我的 dr["id"] 保存在一个列表中,并创建一个小函数来遍历列表并返回逗号分隔的字符串。添加和删​​除项目应在该集合中进行。

【讨论】:

  • 先生,实际上我不清楚解决方案,您能否为上述问题提供某种编码
猜你喜欢
  • 2011-07-19
  • 1970-01-01
  • 2011-09-06
  • 2019-11-13
  • 2013-11-05
  • 2014-12-10
  • 2011-02-19
  • 2016-11-28
  • 2022-09-25
相关资源
最近更新 更多