Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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");
        }

       

      
    }
}
Posted

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());
 
Share this answer
 
v2
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(',');
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900