473,385 Members | 1,757 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,385 software developers and data experts.

Handling ListBox selection changing

KK
Hi All...
For my purpose, I need to handle listbox selection changed event.I must
be notified before changing occurs and after.After I can handle using
SelectedIndexChanged event.Is there anyway to know which one is selected
item before this event.?One thing is I can have a variable to hold the index
in my application.

I'm looking exactly like TreeView events(which fires events before
expanding and after expanding the nodes).

Thanks in advance...

Regards
Krishna
Nov 16 '05 #1
4 11816
Krishna,

I think that you will probably have to keep track of the last selected
item in your application. However, you are only going to get one event.
Normally, I would say that you should override the WndProc method on the
ListBox, but there is no windows message either that the listbox dispatches
to indicate that the selection has changed before and after, there is only
one message.

So in that sense, you will have to deal with the single event, and keep
track of the previously selected item(s) yourself.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"KK" <kr*****@lucidindia.net> wrote in message
news:Ow*************@TK2MSFTNGP11.phx.gbl...
Hi All...
For my purpose, I need to handle listbox selection changed event.I must
be notified before changing occurs and after.After I can handle using
SelectedIndexChanged event.Is there anyway to know which one is selected
item before this event.?One thing is I can have a variable to hold the
index
in my application.

I'm looking exactly like TreeView events(which fires events before
expanding and after expanding the nodes).

Thanks in advance...

Regards
Krishna

Nov 16 '05 #2
Krishna,

I think that you will probably have to keep track of the last selected
item in your application. However, you are only going to get one event.
Normally, I would say that you should override the WndProc method on the
ListBox, but there is no windows message either that the listbox dispatches
to indicate that the selection has changed before and after, there is only
one message.

So in that sense, you will have to deal with the single event, and keep
track of the previously selected item(s) yourself.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"KK" <kr*****@lucidindia.net> wrote in message
news:Ow*************@TK2MSFTNGP11.phx.gbl...
Hi All...
For my purpose, I need to handle listbox selection changed event.I must
be notified before changing occurs and after.After I can handle using
SelectedIndexChanged event.Is there anyway to know which one is selected
item before this event.?One thing is I can have a variable to hold the
index
in my application.

I'm looking exactly like TreeView events(which fires events before
expanding and after expanding the nodes).

Thanks in advance...

Regards
Krishna

Nov 16 '05 #3
Hi,

If you want to do some processing before the SelectedIndexChanged you
should use the MouseDown event, now the real trick is knowing where it was
clicked.
With a listview it's very easy as the ListViewItem provide a bound
property.
In a listbox, well take a look at this piece of code from opennetcf.org 's
OwnerDrawControl:
protected override void OnMouseDown(MouseEventArgs e)
{
//get out if there are no items
if (listItems.Count == 0)
return;

int prevSelection = selectedIndex;

selectedIndex = this.vScroll.Value + (e.Y / this.ItemHeight);

Graphics gxTemp = this.CreateGraphics();

if (prevSelection!=-1)
PaintItem(gxTemp, prevSelection);

PaintItem(gxTemp, selectedIndex);

DrawBorder(gxTemp);

//this.Focus();
base.OnMouseDown( e);
if ( prevSelection != selectedIndex)
OnSelectedIndexChanged( e);
}
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"KK" <kr*****@lucidindia.net> wrote in message
news:Ow*************@TK2MSFTNGP11.phx.gbl...
Hi All...
For my purpose, I need to handle listbox selection changed event.I must be notified before changing occurs and after.After I can handle using
SelectedIndexChanged event.Is there anyway to know which one is selected
item before this event.?One thing is I can have a variable to hold the index in my application.

I'm looking exactly like TreeView events(which fires events before
expanding and after expanding the nodes).

Thanks in advance...

Regards
Krishna

Nov 16 '05 #4
Hi,

If you want to do some processing before the SelectedIndexChanged you
should use the MouseDown event, now the real trick is knowing where it was
clicked.
With a listview it's very easy as the ListViewItem provide a bound
property.
In a listbox, well take a look at this piece of code from opennetcf.org 's
OwnerDrawControl:
protected override void OnMouseDown(MouseEventArgs e)
{
//get out if there are no items
if (listItems.Count == 0)
return;

int prevSelection = selectedIndex;

selectedIndex = this.vScroll.Value + (e.Y / this.ItemHeight);

Graphics gxTemp = this.CreateGraphics();

if (prevSelection!=-1)
PaintItem(gxTemp, prevSelection);

PaintItem(gxTemp, selectedIndex);

DrawBorder(gxTemp);

//this.Focus();
base.OnMouseDown( e);
if ( prevSelection != selectedIndex)
OnSelectedIndexChanged( e);
}
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"KK" <kr*****@lucidindia.net> wrote in message
news:Ow*************@TK2MSFTNGP11.phx.gbl...
Hi All...
For my purpose, I need to handle listbox selection changed event.I must be notified before changing occurs and after.After I can handle using
SelectedIndexChanged event.Is there anyway to know which one is selected
item before this event.?One thing is I can have a variable to hold the index in my application.

I'm looking exactly like TreeView events(which fires events before
expanding and after expanding the nodes).

Thanks in advance...

Regards
Krishna

Nov 16 '05 #5

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

Similar topics

8
by: Vipin Kedia | last post by:
Hi I have written a code for showing the list boxes as selected using a Listitem and the selected property of the items. Now I have 2 list boxes in my page. But it shows only the selected values...
2
by: (Pete Cresswell) | last post by:
Seems like I've been here before, but can't find anyting in Google. I've got two list boxes on a form. Seems to me like the inactive ListBox's selection rectangle should be something like...
0
by: KK | last post by:
Hi All... For my purpose, I need to handle listbox selection changed event.I must be notified before changing occurs and after.After I can handle using SelectedIndexChanged event.Is there anyway...
2
by: Alpha | last post by:
I have a window application. On one of the form, there is a listbox and a few combox. The lstSchItem has a dataview as a datasource. The comboxes are bind to its selected value but through the...
5
by: mosscliffe | last post by:
I have a listbox, with a code behind on SelectedIndexChanged This works well, except for one condition. On returning to the listbox after PostBack, the previous selection is still highlighted....
3
by: Kevin Walzer | last post by:
I'm trying to set the active item in a Tkinter listbox to my application's currently-defined default font. Here's how I get the fonts loaded into the listbox: ...
1
by: sf | last post by:
Hi, searching this group and othher sources didn't found a match. My problem: Working with VS C#.Net 2003 I have a ListBox and like to retrieve the item which has the focus - not the selected...
8
by: NJonge01 | last post by:
Great thanks to all the helpful responses I've read! Recently using MS Access after a lengthy (7-10 years) away from the tool. I apologize for posting a question that for all intents & purposes...
6
by: AB | last post by:
Hi, I have a listbox and a webbrowser control. For each single item selected I am generating a different web page. However for more than one selection, a common web page gets generated. I...
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: 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:
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.