1

I'm trying to load a form with different information depending on which component of my listbox I double click. If I get which box was clicked(box 1, box2, etc.), that would be enough.

I've tried using the Doubleclick event, but it returns an object, and I'm not quite sure what to do with this object to get what I need.

Heres my code right now:

for (int i = 0; i <= (Program.Customers.Count) - 1; i++)
            {
                if (Program.Customers[i].Name == searchTerm)
                {
                    SearchIndex.Add(i);   
                    listBox1.Items.Add(((Program.Customers[i].ID + " - " + Program.Customers[i].Name)));
                }
            }
            listBox1.Show();

What would be the best way to get which box was clicked? I need the ID, but I can get that with box was clicked.

Thanks!

3
  • 1
    Try casting the sender: ListBox listBox = sender As ListBox;. Commented Oct 10, 2013 at 19:37
  • Im really new to C#, so I'm not too sure what to do with this code. It doesn't compile in my doubleclick method. Could you explain what this does, or show where/how to use it? Thanks! Commented Oct 10, 2013 at 19:39
  • It doesn't seem like you posted enough code to understand the problem. The signature method for a double click event for a ListBox should look like this: void listBox1_DoubleClick(object sender, EventArgs e). Commented Oct 10, 2013 at 19:45

1 Answer 1

2

In the simplest case, you can directly compare the sender argument with your ListBox control instances, for example:

if (sender == listBox1)
{
    // ...
}

To get more information out of sender you need to cast it to something more specific first. So if you know your double click handler was only attached to list boxes, you can do

var listbox = (ListBox)sender;

and then access any of the properties of ListBox (such as Tag, which I mention because it's there specifically for your custom needs).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.