有关清单箱的问题 [英] problem regarding to the checklistbox

查看:68
本文介绍了有关清单箱的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿那里,



我有一个checkedListBox和一个TextBox ......当我在checkedListBox中检查项目时,它显示了相应项目的值在TextBox中.....当我在checkedListBox中检查多个项目时,它显示TextBox中相应项目的值,分隔为{,}逗号



现在我的问题是,当我取消选中textBox中的项目时,它必须从textBox中删除相应未检查项目的值......也请告诉我如何从结尾处删除逗号{,}文本框以编程方式



Hey there,

I have a checkedListBox and a TextBox......When I Checked item in the checkedListBox it shows the value of the respective item in the TextBox.....When I Checked multiple items in the checkedListBox it shows the values of the respective items in the TextBox separating by {,}"Comma"

Now my question is that when I unchecked the item in the textBox it must remove the value of respective unchecked items from the textBox ......also plz tell me how do i remove "Comma"{,} from the end of the text box programmatically

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace listbox
{
    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");
        }

       

      
    }
}

推荐答案

删除最后一个逗号:

To remove the last comma you could:
textBox1.Text = textBox1.Text.TrimEnd(',');



但也许你应该在更改列表框中的项目时始终生成整个文本:


but maybe you should always generate the entire text when changing a item in the listbox:

checkedListBox1.ItemCheck += (s, e) => textBox1.Text = string.Join(", ", checkedListBox1.CheckedItems.Cast<string>().ToArray());


private void checkedListBox1_SelectedIndexChanged(对象发送者,ItemCheckEventArgs e)

{

StringBuilder stringBuilder = new StringBuilder();



foreach(var 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 = stringBuil der.ToString()。TrimEnd(',');

}
private void checkedListBox1_SelectedIndexChanged(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(',');
}


这篇关于有关清单箱的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆