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)
}
}
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)
}
}