473,796 Members | 2,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 SelectedValueCh anged event and the Click event. The
reason being because sometime I change the selected item programmaticall y
and sometimes the user selects the item.

I'm wondering why selectedID, which is being set in SelectedValueCh anged, is
being displayed differently in the two messgeboxes in the Click Event. I'm
certain the SelectedValueCh anged 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.Collecti ons;

using System.Componen tModel;

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.Componen tModel.Containe r components = null;

private ListBox lstBoxSpecies;

int selectedID;

public Temp()

{

InitializeCompo nent();

}

private void InitializeCompo nent()

{

// Create and position controls

lstBoxSpecies = new ListBox();

lstBoxSpecies.H eight = 135;

lstBoxSpecies.L ocation = new Point(100,100);

lstBoxSpecies.I tems.Add("ITEM 1");

lstBoxSpecies.I tems.Add("ITEM 2");

lstBoxSpecies.P arent = this;

lstBoxSpecies.S electedValueCha nged +=

new EventHandler(ls tBoxSpecies_Sel ectedValueChang ed);

lstBoxSpecies.C lick += new EventHandler(ls tBoxSpecies_Cli ck);

this.components = new System.Componen tModel.Containe r();

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

this.Text = "Temp";

}

private void lstBoxSpecies_S electedValueCha nged(object sender, EventArgs e)

{

selectedID = lstBoxSpecies.S electedIndex;

} // private void lstBoxSpecies_S electedValueCha nged(object sender,
EventArgs e)

private void lstBoxSpecies_C lick(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.ToSt ring());

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

} // private void lstBoxSpecies_C lick(object sender, EventArgs e)

}

}
Nov 16 '05 #1
2 7291
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
(selectedindice s) change event.

Can it be that?

Cor
Nov 16 '05 #2
Well, as it turns out the click event is actually running before the
SelectedValueCh anged 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 SelectedValueCh anged
event. Also, the Click event was holding on to the old value and the
SelectedValueCh anged 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******** ********@tk2msf tngp13.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
(selectedindice s) 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
1078
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 specifications is that the first possible record should be selected. It worked fine until the client decided that when they single click on the listbox, they want that value that they just clicked on to be sent to populate the textbox.
13
7501
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 it, a frontend and a backend. Case 1: If vba code on the frontend updates many rows (360,000) on the backend, a form's timer event (from the frontend) will stop firing until the user gives the form focus. (Note that the update itself always...
3
3995
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 this"problem" and have a workaround for it? Thanks in advance... Doug
1
8424
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 I want to do is to allow the user to click on the row that corresponds to the correct address, and have the code behind populate the form's Address1, Address2 etc. controls with the relevant data items. I put the code for this into the...
1
5080
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 the OnClick Event. My problem is that when the command button is clicked, the Listbox OnSelectedIndexChanged event gets fired before the command buttons OnClick
18
2027
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 unselected. when listbox2 is selected, 1 and 3 should have no items selected and the same with listbox 3. Sounds simple... should be simple! I have tried listbox.clearselected(), listbox.selectedindex=-1 and beating it with a large stick. But so...
5
2202
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 added a click event handler that resets the selected items based on whats in the collection it is data bound to... I'm baffled, pounding my head against the wall, it's not working... Please help... I tried posting the code but it was too long,...
3
7777
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 the form. (It's a Company -> People -> Personal Data relationship) When I bind my datasource to the ListBox, I find that the
3
2841
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 SelectedIndexChanged event keep fire and loop doesn't go to next, finally the program stop at infinite loop. Sub lstCem_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstCem.SelectedIndexChanged If...
0
9684
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10236
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.