Click here to Skip to main content
15,844,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one CheckListBox and one TextBox....items of checkListBox is bound to the

"user_name" column of the table in database and there is one more column "email_id".......my

question is that when i check multiple items form the checklist box it will show the their

respected value(email_id) into the TextBox separating with "," (comma operator)

C#
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 listBox1_SelectedIndexChanged(object sender, EventArgs e)
{


 

}
 
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 *(name,id) from address_book where name ='" + checkedListBox1.Text + "'", con);
SqlDataReader reader;
reader = cmd.ExecuteReader();

}
 

}
}
Posted
Updated 5-Aug-14 21:07pm
v3
Comments
Gaurav Manusmare 21-Feb-14 12:31pm    
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 listBox1_SelectedIndexChanged(object sender, EventArgs e)
{




}

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 *(name,id) from address_book where name ='" + checkedListBox1.Text + "'", con);
SqlDataReader reader;
reader = cmd.ExecuteReader();

}


}
}

Add user name as text & email as value.

C#
while (drd.Read()){


 checkedListBox1.Items.Add(new ListItem(drd.GetString(0).ToString(), drd.GetString(1).ToString()));

//assuming 0th column username and 1st column Email
 
}


C#
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string str= String.Empty;
 for (int i = 0; i < checkedListBox1.Items.Count; i++)
        {
          if (checkedListBox1.Items[i].Selected)
          {
          str = String.IsNullOrEmpty(str)? checkedListBox1.Items[i].Value: str+ ","+ checkedListBox1.Items[i].Value;  
          }
        }
 TextBox1.Text = str;
}
 
Share this answer
 
v4
Just handle the checklist click event and in that event use for loop to get checked boxes which value will be stored in string. bind this string to the text box.
 
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