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)
{
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];
}*/
{
if ((i + 1) < listBox1.Items.Count)
textBox1.Text += listBox1.Items[i] + ", ";
else
textBox1.Text += listBox1.Items[i];
}*/
}
Resign DesignsPosted Jun 29, 2015, 4:08 AM
Download
Saber ShaikhPosted Jun 29, 2015, 3:59 AM
Resign DesignsPosted Jun 29, 2015, 3:33 AM
Manoj BhoirPosted Jun 29, 2015, 2:41 AM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
if (textBox1.Text == "")
textBox1.Text += listBox1.SelectedItem.ToString();
else
textBox1.Text += "," + listBox1.SelectedItem.ToString();
}
}
}
}
Upendra Pratap ShahiPosted Jun 29, 2015, 1:53 AM
textbox1.text=listbox1.selecteditem.value
If you want to select multiple values then u may need for loop:
for(i=0;i< listbox1.items.count-1;i++){
if( listbox1.items(i).selected==true){
textbox1.text=listbox1.items(i).value
}}
Resign DesignsPosted Jun 29, 2015, 1:45 AM
Resign DesignsPosted Jun 28, 2015, 7:59 AM
Khan Abrar AhmedPosted Jun 28, 2015, 5:20 AM
{
listBox1.SelectedItem.Text;Manoj BhoirPosted Jun 28, 2015, 4:28 AM
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
Munesh SharmaPosted Jun 28, 2015, 3:50 AM