Hi Experts,
I have developing a windows application with CheckedListBox and Textbox.
Checkboxlist contains 100 values.
Textbox- filter the Checkboxlist value based on the textbox text
Also I can retain the checked values from full list and filtered list
Attached sourced code for quick reference
How Can i do this?
please help me out on this.
Thanks,
Murugavel S
Loading
Murugavel SadagopanPosted Sep 10, 2013, 12:16 AM
Form1.cs
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 CheckedList
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
test();
textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string filtertxt = textBox1.Text.Trim() as string;
if (string.IsNullOrEmpty(filtertxt))
{
test();
}
else
{
checkedListBox1.Items.Clear();
test1(filtertxt);
}
}
public void test()
{
SqlConnection con = new SqlConnection("Persist Security Info=True;Initial Catalog=xxxx;Integrated Security=SSPI;Connect Timeout=5;Data Source=dbxxxxx");
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("SELECT distinct column_name FROM information_schema.columns where table_name IN ('tablename1', 'tablename2')", con);
sda.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
//bool Check = Convert.ToBoolean(dr["IsSold"]);
checkedListBox1.Items.Add(dr["column_name"].ToString());
}
}
public void test1(string txt)
{
SqlConnection con = new SqlConnection("Persist Security Info=True;Initial Catalog=xxxx;Integrated Security=SSPI;Connect Timeout=5;Data Source=dbxxxxx");
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter("SELECT distinct column_name FROM information_schema.columns where table_name IN ('tablename1', 'tablename2') and column_name like '%" + txt + "%' ", con);
sda.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
//bool Check = Convert.ToBoolean(dr["IsSold"]);
checkedListBox1.Items.Add(dr["column_name"].ToString());
}
}
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
List
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i))
{
// If the item is selected, add the value to the list.
YrStrList.Add(checkedListBox1.Items[i].ToString());
}
else
{
// Item is not selected, do something else.
}
}
// Join the string together using the ; delimiter.
String YrStr = String.Join(";", YrStrList.ToArray());
MessageBox.Show(YrStr);
// Write to the page the value.
//myString = "101,102,103,104,105,106,107,108,109";
foreach (string value in YrStr.Split(';'))
{
// checkedListBox2.Items.Add(value);
}
}
}
}
Designer File.....
namespace CheckedList
{
partial class Form1
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
/// Clean up any resources being used.
///
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(106, 35);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(175, 20);
this.textBox1.TabIndex = 0;
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// checkedListBox1
//
this.checkedListBox1.CheckOnClick = true;
this.checkedListBox1.FormattingEnabled = true;
this.checkedListBox1.Location = new System.Drawing.Point(106, 62);
this.checkedListBox1.Name = "checkedListBox1";
this.checkedListBox1.ScrollAlwaysVisible = true;
this.checkedListBox1.Size = new System.Drawing.Size(175, 184);
this.checkedListBox1.TabIndex = 1;
this.checkedListBox1.SelectedIndexChanged += new System.EventHandler(this.checkedListBox1_SelectedIndexChanged);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(587, 275);
this.Controls.Add(this.checkedListBox1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.CheckedListBox checkedListBox1;
}
}
Javeed M ShaikhPosted Sep 6, 2013, 10:19 AM