problem regarding to the checklistbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GAURAV MANUSMAR
    New Member
    • Feb 2014
    • 8

    problem regarding to the checklistbox

    Hey there,

    I have a checkedListBox and a TextBox......Wh en I Checked item in the checkedListBox it shows the value of the respective item in the TextBox.....Whe n 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 programmaticall y

    Code:
    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");
            }
    
           
    
          
        }
    }
    Attached Files
  • GAURAV MANUSMAR
    New Member
    • Feb 2014
    • 8

    #2
    Code:
    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(',');
    }
    Last edited by Rabbit; Mar 1 '14, 05:34 AM. Reason: Please use [code] and [/code] tags when posting code or formatted data.

    Comment

    Working...