ListBox Event Firing

J

Jared Baszler

I just wanted to write and ask why the below behavior is occuring. I've
simply put a list box on a form and populated it with two items. I'm
handling both the SelectedValueChanged event and the Click event. The
reason being because sometime I change the selected item programmatically
and sometimes the user selects the item.

I'm wondering why selectedID, which is being set in SelectedValueChanged, is
being displayed differently in the two messgeboxes in the Click Event. I'm
certain the SelectedValueChanged event occurs first so I don't know why this
behavior is occuring. I need to know how to get the two messageboxes to
display the actual item selected and not the previous item selected.

What is really baffling is when I immediately select the 2nd item (with no
selection present at the start of the form) the first messagebox shows 0
instead of -1?

Please explain this behavior if you could. Thanks in advance.

Jared Baszler

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

namespace MWSSVirtualCard

{

/// <summary>

/// Summary description for Temp.

/// </summary>

public class Temp : System.Windows.Forms.Form

{

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

private ListBox lstBoxSpecies;

int selectedID;

public Temp()

{



InitializeComponent();

}



private void InitializeComponent()

{

// Create and position controls

lstBoxSpecies = new ListBox();

lstBoxSpecies.Height = 135;

lstBoxSpecies.Location = new Point(100,100);

lstBoxSpecies.Items.Add("ITEM 1");

lstBoxSpecies.Items.Add("ITEM 2");

lstBoxSpecies.Parent = this;

lstBoxSpecies.SelectedValueChanged +=

new EventHandler(lstBoxSpecies_SelectedValueChanged);

lstBoxSpecies.Click += new EventHandler(lstBoxSpecies_Click);

this.components = new System.ComponentModel.Container();

this.Size = new System.Drawing.Size(300,300);

this.Text = "Temp";

}

private void lstBoxSpecies_SelectedValueChanged(object sender, EventArgs e)

{

selectedID = lstBoxSpecies.SelectedIndex;

} // private void lstBoxSpecies_SelectedValueChanged(object sender,
EventArgs e)

private void lstBoxSpecies_Click(object sender, EventArgs e)

{

// Why here when the first messagebox pops up it list the previously

// selected item and the 2nd messagebox shows the actual selected item

MessageBox.Show("In click: " + selectedID.ToString());

MessageBox.Show("In click: " + selectedID.ToString());

} // private void lstBoxSpecies_Click(object sender, EventArgs e)

}

}
 
C

Cor Ligthert

Jared,

I did not check all your code, because mostly this question is because
people forget that selecting in a listbox means as well first deselecting
(automaticly) and than that event is fired in the selectedindex
(selectedindices) change event.

Can it be that?

Cor
 
J

Jared Baszler

Well, as it turns out the click event is actually running before the
SelectedValueChanged event from what I can tell. Instead of using
messageboxes I tried inserting records into a database table essentially
keeping a log of events. I made sure I order them with a counter and it
turns out the Click event was inserting before the SelectedValueChanged
event. Also, the Click event was holding on to the old value and the
SelectedValueChanged event was inserting the new value. So I figured out
how to solve my problem. Thanks or the response. I just wanted to reply to
explain how I got around the problem I was having.

Jared Baszler
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top