清单框中的多个项目选择,并在文本框中显示相应的值 [英] multiple selection of items in checklist Box and show respective value in textbox

查看:86
本文介绍了清单框中的多个项目选择,并在文本框中显示相应的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CheckListBox和一个TextBox .... checkListBox的项目绑定到数据库中表的



user_name列,并且有一个更多栏目email_id.......我的



问题是,当我从核对表框中检查多个项目时,它会显示他们的



尊重值(email_id)进入TextBox分隔,(逗号运算符)



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)

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.

while (drd.Read()){


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

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







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;
}


只需处理核对表单击事件,并在该事件中使用for循环来获取复选框,哪些值将存储在字符串中。将此字符串绑定到文本框。
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.


这篇关于清单框中的多个项目选择,并在文本框中显示相应的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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