0

Hey there.

i have a listbox thats databound. Now i want to select in that massive list, lets say a sting "dog", i want it to hightligh that word in there. But i cant seem to get it to work with this databound listbox.

Coz i know how to do it if it wasnt databound.

its this

txtCity.Text = listBox1.SelectedItem.ToString();

but this code doesnt seem to work on a databound listbox...

What can i do to fix this??:?:

Thank you.

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Mitja Bonca
Featured Replies
  • This should do it: [CODE] public partial class Form1 : Form { public Form1() { InitializeComponent(); string[] array ={"A", "B", "C", "D", "E"}; listBox1.DataSource = array; listBox1.SelectedIndex = -1; listBox1.SelectedIndexChanged+=new EventHandler(listBox1_SelectedIndexChanged); } private void button1_Click(object sender, EventArgs e) { //1. automatic selection - insert the number of the index into … Read More

1

This should do it:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string[] array ={"A", "B", "C", "D", "E"};
            listBox1.DataSource = array;
            listBox1.SelectedIndex = -1;
            listBox1.SelectedIndexChanged+=new EventHandler(listBox1_SelectedIndexChanged);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //1. automatic selection - insert the number of the index into textBox
            try
            {
                listBox1.SelectedIndex = int.Parse(textBox1.Text);
            }
            catch { }
            
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //2. when you select a row in the listBox, the value get written into textBox:
            textBox2.Text = listBox1.GetItemText(listBox1.SelectedItem);
        }
    }
Comments
Easy and Simple.
0

This should do it:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string[] array ={"A", "B", "C", "D", "E"};
            listBox1.DataSource = array;
            listBox1.SelectedIndex = -1;
            listBox1.SelectedIndexChanged+=new EventHandler(listBox1_SelectedIndexChanged);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //1. automatic selection - insert the number of the index into textBox
            try
            {
                listBox1.SelectedIndex = int.Parse(textBox1.Text);
            }
            catch { }
            
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //2. when you select a row in the listBox, the value get written into textBox:
            textBox2.Text = listBox1.GetItemText(listBox1.SelectedItem);
        }
    }

This worked great.. =) Thank you Mitja.
You always help out in a time of need =)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.