473,378 Members | 1,518 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

ListBox Event Firing

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)

}

}
Nov 16 '05 #1
2 7276
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
Nov 16 '05 #2
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:uP****************@tk2msftngp13.phx.gbl...
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

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stubble | last post by:
I have code that selects records in a listbox called based on the value typed in a textbox called txtVendor like the windows help that narrows the possiblities as the user types. One of the...
13
by: Manuel Lopez | last post by:
I have a puzzling form timer problem that I didn't experience prior to Access 2003 (though I'm not sure access 2003 is to blame). Here's the situation: a computer has two access 2003 databases on...
3
by: Doug | last post by:
Hi, I wondering if anyone has noticed this and has a workaround. I've set up a delegate for the SelectedIndexChange event and noticed that it is firing twice - does nayone else have...
1
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What...
1
by: RedGST | last post by:
Hello, Im somewhat new to asp.net, so bear with me. I have created a form that has a Listbox along with 2 command buttons & a few other non-important controls. The command button is tied to...
18
by: Dave Sauny | last post by:
Ok, its a friday, I'm at work and I cant get this to work: I have 3 listboxes on one tab control page. when i select an item in listbox1 i want whatever is selected on the other 2 listboxes...
5
by: Alien2_51 | last post by:
I have a problem with a ListBox control that is on a TabControl, it seems to be forgetting which items are selected in the list when I tab off the current tab, here's my winform code... I even...
3
by: Alec MacLean | last post by:
Hi everyone, I have a ComboBox that when changed, calls a method to change the content of a ListBox. I'm also using the Listbox's SelectedIndexChanged event to change other control values on...
3
by: martin1 | last post by:
Hi, All, I want user select first item (called All) in listbox, then all other items are selected by SetSelected method, but in loop (see code below) whenever going to SetSelected(), the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.