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

dismiss a listbox when mouseclick outside boundaries

I have a listbox on my form and I set it to invisible. When a particular
button on the form is pressed, I make the listbox visible and allow the
user to select from it. When the selection occurs, the listbox is made
invisible again.

What I want to do is automatically make the listbox invisible if it is
visible and the user clicks the mouse outside the listbox boundaries.
Seems like this should be easy, but other than trapping all mouseclicks
and then comparing location to the boundaries of the listbox and whether
it is visible, I can't find a method for this.

The behavior I am trying to emulate is built into popup lists in PalmOS.

Anyone have any clever ideas, or something I am missing?

Thanks

Terry Brown
Stickman Software
http://stickmansoftware.com
Dec 26 '06 #1
4 4060
Hi Terry,

Handle the Form's MouseDown event, or override the Form's OnMouseDown
method:

protected override void OnMouseDown(MouseEventArgs e)
{
if (!theListBox.Bounds.Contains(e.Location))
theListBox.Visible = false;
}

The Contains method olf System.Drawing.Rectangle can be used to indicate if
a point is contained inside a rectangle. The Bounds property of a Control is
the outer Rectangle defined by the edges of the Control. The Location
property of the MouseEventArgs is the Point relative to the Control which
received the MouseDown event. The Bounds property of a Control in a Form is
relative to the Form.

--
HTH,

Kevin Spencer
Microsoft MVP
Bit Player
http://unclechutney.blogspot.com

A pea rants as candy be sieving.

"tbrown" <tt****@yahoo.comwrote in message
news:eYckh.5051$6f4.2020@trndny08...
>I have a listbox on my form and I set it to invisible. When a particular
button on the form is pressed, I make the listbox visible and allow the
user to select from it. When the selection occurs, the listbox is made
invisible again.

What I want to do is automatically make the listbox invisible if it is
visible and the user clicks the mouse outside the listbox boundaries.
Seems like this should be easy, but other than trapping all mouseclicks
and then comparing location to the boundaries of the listbox and whether
it is visible, I can't find a method for this.

The behavior I am trying to emulate is built into popup lists in PalmOS.

Anyone have any clever ideas, or something I am missing?

Thanks

Terry Brown
Stickman Software
http://stickmansoftware.com

Dec 26 '06 #2
On Tue, 26 Dec 2006 12:41:01 -0500, Kevin Spencer wrote:
Hi Terry,

Handle the Form's MouseDown event, or override the Form's OnMouseDown
method:

protected override void OnMouseDown(MouseEventArgs e)
{
if (!theListBox.Bounds.Contains(e.Location))
theListBox.Visible = false;
}

The Contains method olf System.Drawing.Rectangle can be used to indicate if
a point is contained inside a rectangle. The Bounds property of a Control is
the outer Rectangle defined by the edges of the Control. The Location
property of the MouseEventArgs is the Point relative to the Control which
received the MouseDown event. The Bounds property of a Control in a Form is
relative to the Form.
thanks a lot for the input. I am using Visual Studio 2003 (which means
..net 1.1). Perhaps this is what causes the error that e.Location doesn't
exist in the MouseEventArgs class. I fixed this by doing the following:

if (!theListBox.Bounds.Contains(new System.Drawing.Point(e.X, e.Y)))

and this works--sort of.

although I capture the MouseDown event when the click is outside of
another control on the form, it is not captured when it is within another
control. so, if the list is visible, and I click on a different control
on the form that is also visible (not obscured by the listbox), then the
MouseDown event is swallowed and executed by the control it occurred
within. Any way to avoid this other than making all the other controls
invisible?
Dec 26 '06 #3
This is going to be tough. You will either have to use a handler for each
Control's MouseDown event, or use a bit of WinApi trickery, by overriding
the Form's PreProcessMessage event, which is an API-level event that gets
all Windows messages coming in to the Form. If you're interested, check out:

http://msdn2.microsoft.com/en-us/lib...spx--HTH,Kevin SpencerMicrosoft MVPBit Playerhttp://unclechutney.blogspot.comA pea rants as candy be sieving."tbrown" <tt****@yahoo.comwrote in messagenews:_Edkh.5059$dw6.2428@trndny02...On Tue, 26 Dec 2006 12:41:01 -0500, Kevin Spencer wrote:>>Hi Terry,>>>Handle the Form's MouseDown event, or override the Form's OnMouseDown>method:>>>protected override void OnMouseDown(MouseEventArgs e)>{> if (!theListBox.Bounds.Contains(e.Location))> theListBox.Visible = false;>}>>>The Contains method olf System.Drawing.Rectangle can be used to indicateif>a point is contained inside a rectangle. The Bounds property of a Controlis>the outer Rectangle defined by the edges of the Control. The Location>property of the MouseEventArgs is the Point relative to the Control which>received the MouseDown event. The Bounds property of a Control in a Formis>relative to the Form.>>>thanks a lot for the input. I am using Visual Studio 2003 (which means.net 1.1). Perhaps this is what causes the error that e.Location doesn'texist in the MouseEventArgs class. I fixed this by doing the following:>if (!theListBox.Bounds.Contains(new System.Drawing.Point(e.X, e.Y)))>and this works--sort of.>although I capture the MouseDown event when the click is outside ofanother control on the form, it is not captured when it is within anothercontrol. so, if the list is visible, and I click on a different controlon the form that is also visible (not obscured by the listbox), then theMouseDown event is swallowed and executed by the control it occurredwithin. Any way to avoid this other than making all the other controlsinvisible?

Dec 26 '06 #4
New to c# but not new to windows programming
isn't there a window.focuschange() event or somthing like this to captuere
control focus changes

mike

"tbrown" <tt****@yahoo.comwrote in message
news:eYckh.5051$6f4.2020@trndny08...
>I have a listbox on my form and I set it to invisible. When a particular
button on the form is pressed, I make the listbox visible and allow the
user to select from it. When the selection occurs, the listbox is made
invisible again.

What I want to do is automatically make the listbox invisible if it is
visible and the user clicks the mouse outside the listbox boundaries.
Seems like this should be easy, but other than trapping all mouseclicks
and then comparing location to the boundaries of the listbox and whether
it is visible, I can't find a method for this.

The behavior I am trying to emulate is built into popup lists in PalmOS.

Anyone have any clever ideas, or something I am missing?

Thanks

Terry Brown
Stickman Software
http://stickmansoftware.com

Dec 26 '06 #5

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

Similar topics

3
by: Alex Stevens | last post by:
I'd already posted this in microsoft.public.dotnet.framework.windowsforms and microsoft.public.dotnet.framework.windowsforms.controls to no avail so apologies for the cross-posting. Hi, I'm...
6
by: Jim H | last post by:
I have a dialog that I bring up as an options dialog. I have a variable for this dialog as a member of the main application form and I create it in the constructor. I bring up the dialog by...
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...
28
by: cjobes | last post by:
Hi all, I need to populate a listbox from a table column. The column has multiple entries of usernames and I need to pull a unique set of usernames. The table is part of an untyped Dataset that...
0
by: Alex Pierson | last post by:
2 listboxes are in a non-VB application When the item changes (via mouseclick) of listbox 1, the text in listbox2 changes. I am trying to control this programatically. I have the handles of the...
4
by: Peter Rilling | last post by:
What is the difference between the MouseClick and Click events?
6
by: Kristian Frost | last post by:
Hi, I'm trying to add, as you might guess, mouseclick listeners to the shapes I am drawing using the GDI+ commands in a similar sort of way as could be done with the old VB "shapes". Problem is,...
2
by: rn5a | last post by:
A ListBox lists all the files & directories existing in a directory on the server. If an item in the ListBox happens to be a directory, then the name of the directory is appended with the text ....
3
by: Joe Cool | last post by:
I have a ListBox on a form than has some items. The select mode is one item only. I select one, it is selected and hightlighted. I then click somewhere in the listbox not on any item, in the clear...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.