TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Resign Designs
NA
31
30k
How to add listbox selected item to textbox?
Jun 28 2015 2:21 AM
Hi,
I have below code and when I type something on textbox it will be selected from listbox.Like an autocomplete feature.Now I need to
get selected item from listbox to textbox? any idea how can I achieve it?
DataTable dt = new DataTable();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add(1, "Foo");
dt.Rows.Add(2, "Hoo");
dt.Rows.Add(3, "Too");
dt.Rows.Add(4, "Fii");
dt.Rows.Add(5, "Fee");
dt.Rows.Add(6, "Fuu");
dt.Rows.Add(7, "Noo");
listBox1.DataSource = dt;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "Id";
listBox1.SelectionMode = SelectionMode.MultiExtended;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
DataTable clone = dt.Clone();
foreach (DataRow dr in dt.Select("Name like '" + textBox1.Text + "%'"))
{
clone.ImportRow(dr);
}
listBox1.DataSource = clone;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "Id";
//or
for (int i = 0; i < listBox1.Items.Count; i++)
{
listBox1.SetSelected(i, false);
if (listBox1.GetItemText(listBox1.Items[i]).StartsWith(textBox1.Text))
{
listBox1.SetSelected(i, true);
}
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//This code is not working
/* for (int i = 0; i < listBox1.Items.Count; i++)
{
if ((i + 1) < listBox1.Items.Count)
textBox1.Text += listBox1.Items[i] + ", ";
else
textBox1.Text += listBox1.Items[i];
}*/
}
Reply
Answers (
10
)
XML into Object
login form