By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,899 Members | 1,230 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,899 IT Pros & Developers. It's quick & easy.

problem regarding to the checklistbox

P: 8
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

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.SqlClient;
  10. namespace listbox
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.  
  20.  
  21.         private void Form1_Load(object sender, EventArgs e)
  22.         {
  23.             SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=email_client;Integrated Security=True");
  24.             SqlCommand command = new SqlCommand("Select * FROM address_book ", connection);
  25.  
  26.             try
  27.             {
  28.                 connection.Open();
  29.                 {
  30.                     SqlDataReader drd = command.ExecuteReader();
  31.  
  32.                     while (drd.Read())
  33.                     {
  34.                         this.checkedListBox1.Items.Add(drd.GetString(0).ToString());
  35.  
  36.                     }
  37.                 }
  38.  
  39.             }
  40.             catch (Exception ex)
  41.             {
  42.  
  43.                 MessageBox.Show(ex.Message.ToString());
  44.  
  45.             }
  46.  
  47.             connection.Close();
  48.         }
  49.  
  50.         private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
  51.         {
  52.  
  53.                 SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=email_client;Integrated Security=True");
  54.                 con.Open();
  55.                 SqlCommand cmd = new SqlCommand("select * from address_book where name='" + checkedListBox1.Text + "'", con);
  56.                 SqlDataReader dr;
  57.                 dr = cmd.ExecuteReader();
  58.  
  59.  
  60.  
  61.                 while (dr.Read())
  62.                 {
  63.                     textBox1.Text += Convert.ToString(dr["id"] + ",");
  64.                 }
  65.  
  66.  
  67.                 dr.Close();
  68.  
  69.  
  70.         }
  71.  
  72.         private void textBox1_Enter(object sender, EventArgs e)
  73.         {
  74.             ToolTip tt = new ToolTip();
  75.             tt.SetToolTip(textBox1, "sorry");
  76.         }
  77.  
  78.  
  79.  
  80.  
  81.     }
  82. }
  83.  
Attached Images
File Type: jpg Screenshot (17).jpg (37.4 KB, 41 views)
Feb 28 '14 #1
Share this Question
Share on Google+
1 Reply


P: 8
Expand|Select|Wrap|Line Numbers
  1. private void checkedListBox1_SelectedIndexChanged(object sender, ItemCheckEventArgs e)
  2. {
  3. StringBuilder stringBuilder = new StringBuilder();
  4.  
  5. foreach (var item in checkedListBox1.CheckedItems)
  6. {
  7. SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=email_client;Integrated Security=True");
  8. con.Open();
  9. SqlCommand cmd = new SqlCommand(string.Format("select * from address_book where name='{0}'", item), con);
  10. SqlDataReader dr = cmd.ExecuteReader();
  11. while (dr.Read())
  12. {
  13. stringBuilder.Append(Convert.ToString(dr["id"] + ","));
  14. }
  15. dr.Close();
  16. }
  17.  
  18. textBox1.Text = stringBuilder.ToString().TrimEnd(',');
  19. }
Mar 1 '14 #2

Post your reply

Sign in to post your reply or Sign up for a free account.