2

HI

I'm trying to put a textbox to search in a listBox. I have a TextBox: SearchText with this code:

 private void SearchText_TextChanged(object sender, EventArgs e)
 {
    int i = listBox3.FindString(SearchText.Text);
    listBox3.SelectedIndex = i;
 }

and a ListBox On the Load I have this code

List<string> str = GetListOfFiles(@"D:\\Music\\massive attack - collected");        
listBox3.DataSource = str;
listBox3.DisplayMember = "str";

and on selectedIndexChanged :

private void listBox3_SelectedIndexChanged(object sender, EventArgs e)
{
    player1.URL = listBox3.SelectedItem.ToString(); // HERE APPEAR THE ERROR "Object reference not set to an instance of an object."

    //  provaTxt.Text = listBox3.SelectedValue.ToString();
}

When I write down in the SeachText to find a songs I receive an error ("Object reference not set to an instance of an object.") in the line selectedIndexChanged of the ListBox. Do you know one more way to find in a listBox as my case? Thanks for your share. Nice Regards

Pieniadz
  • 653
  • 3
  • 9
  • 22
JayJay
  • 1,038
  • 3
  • 25
  • 50

1 Answers1

2

It sounds like the item wasn't found, so SelectedItem was null; try using:

player1.URL = Convert.ToString(listBox3.SelectedItem);

I believe this handles the null case (altenatively, test for null first).

I'd also be tempted to look in the underlying list:

List<string> items = (List<string>)listbox3.DataSource;
listbox3.SelectedIndex = items.FindIndex(s => s.StartsWith(searchFor));

For example:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
class MyForm : Form
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new MyForm());
    }

    ListBox listbox;
    TextBox textbox;
    CheckBox multi;
    public MyForm()
    {
        textbox = new TextBox { Dock = DockStyle.Top };
        List<string> strings = new List<string> { "abc", "abd", "abed", "ab" };
        listbox = new ListBox { Dock = DockStyle.Fill, DataSource = strings };
        textbox.KeyDown += textbox_KeyDown;
        Controls.Add(listbox);
        Controls.Add(textbox);
        listbox.SelectedIndexChanged += listbox_SelectedIndexChanged;
        listbox.SelectionMode = SelectionMode.MultiExtended;
        multi = new CheckBox { Text = "select multiple", Dock = DockStyle.Bottom };
        Controls.Add(multi);
    }

    void listbox_SelectedIndexChanged(object sender, EventArgs e)
    {
        Text = Convert.ToString(listbox.SelectedItem);
    }

    void textbox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Return)
        {
            string searchFor = textbox.Text;
            List<string> strings = (List<string>)listbox.DataSource;
            if (multi.Checked)
            {
                for (int i = 0; i < strings.Count; i++)
                {
                    listbox.SetSelected(i, strings[i].Contains(searchFor));
                }
            }
            else
            {
                listbox.ClearSelected();
                listbox.SelectedIndex = strings.FindIndex(
                    s => s.Contains(searchFor));
            }
        }
    }
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • You can check for null or empty strings using if (!String.IsNullOrEmpty(someString)) – DOK Mar 29 '09 at 20:19
  • @DOK: Yes, but SelectedItem is object, not string. – Marc Gravell Mar 29 '09 at 20:36
  • Hi Marc, first thanks for your support , i changed as you advice me when i write down on the SearchText the error don't appear more but the last one can not search nothing .i changed also StartWith with Contains but nothing.How i can work out that?Thanks yet – JayJay Mar 29 '09 at 20:46
  • Ah i forgot, if it is possible to search in multi_selected records with more result: – JayJay Mar 29 '09 at 20:52
  • Hi Marc ,It's right but there is a trouble when i search a song and click to listen that's right but after the Media PLayer play always the same song...I'm trying the trouble if you have an advice i'm happy as your kind support . Thanks – JayJay Mar 30 '09 at 03:42
  • When the Media PLayer change media item the ListBox din not change items so it fall down in a loop.I have not work out it yet but i continue to find out a solution: – JayJay Mar 30 '09 at 09:06