Question

I understand that there are Listbox Select index change questions floating around. However, this question focuses on a different matter. I have a list box, with some strings on the form. What I am trying to accomplish is to be able to scroll through the items in the list box (i.e using the arrow keys to navigate to a particular item). Once I navigate to the item I want, I want to either be able to press enter on the item and continue my application. So, the question is How to determine the Event type of that was raised on the List box in order to compare the event with either a Mouse Click event or a Keydown event, thus allowing me to decide which conditional statement to execute based of the result of the boolean expression......The reason I need to determine the type is because if the user presses ENter on the selectedIndexed Item a Dialogbox Appears, currently the dialogbox appears everytime a user HIGHLIGHTS a new item (you can see how that is a problem).

Psuedo Code

    if (Listbox_Selected_Event_EventType isEqualTo Mouse_Click)
    {
        // execute code
    } else if (Listbox_Selected_Event_EventType isEqualTo KeydownEvent)
          {
             // execute code
          }

Finished code thanks to Evan,

    private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (listBox1.SelectedIndex != -1)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                var file = Directory.GetFiles(urlHistoryFolder, listBox1.Text).FirstOrDefault();
                String line;
                try
                {
                    using (StreamReader sr = new StreamReader(file))
                    {
                        line = sr.ReadToEnd();
                    }

                    DialogResult result1 = MessageBox.Show("Are You sure you want to Load this WebService", "Important Question", MessageBoxButtons.YesNo);
                    if (result1 == DialogResult.Yes)
                    {
                        //MessageBox.Show("Loading WebService");
                        textEndPointUri.Text = line;
                        listBox1.Visible = false;
                        GetBtn_Click(sender, e);
                    }
                }
                catch (Exception exp)
                {
                    Console.WriteLine("File could not be read:");
                    Console.WriteLine(exp.Message);
                }
            }
        }
    }
Was it helpful?

Solution

The problem is you are looking at the wrong event. You should be handling the MouseClick event and the KeyUp or KeyDown event on the list box.

    private void listBox1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            //Get the selected item and do whatever you need to it
            //Open your dialog box

        }
    }

    private void listBox1_Click(object sender, MouseEventArgs e)
    {
        //Get the selected item and do whatever you need to it
        //Open your dialog box
    }

Then there is no need for a conditional as you have handled both the events individually. Make sure you remove your Dialog box code from the SelectedIndexChanged event.

EDIT: SelectedIndexChanged fires every time you select and item in the ListBox Object. The box still stores an index even if you don't handle that event. So you can reference or manipulate the PROPERTY of SelectedIndex anywhere. If you handle the two above events, any time a user clicks an item or presses enter you check if there is a selected item:

if (listBox1.SelectedIndex != -1)
{
    //Now we know you have an item selected
    //Do some stuff
}

OTHER TIPS

Add a Button to the Form and set the AcceptButton() Property of the FORM to that Button. Now when Enter is pressed the Button will fire. Display your dialog in the Button Click() handler. This has the added benefit that people can also click the Button instead of pressing Enter:

    private void button1_Click(object sender, EventArgs e)
    {
        if (ListBox.SelectedIndex != -1)
        {
            // ... display the dialog ...
            Console.WriteLine(ListBox.SelectedItem.ToString());
        }
    }

To identify if ENTER has been pressed:

private void listBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Return)
        // do something
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top