PC Review


Reply
Thread Tools Rate Thread

dismiss a listbox when mouseclick outside boundaries

 
 
tbrown
Guest
Posts: n/a
 
      26th Dec 2006
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
 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      26th Dec 2006
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" <(E-Mail Removed)> wrote 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



 
Reply With Quote
 
tbrown
Guest
Posts: n/a
 
      26th Dec 2006
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?
 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      26th Dec 2006
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/library/system.windows.forms.control.preprocessmessage(VS.80).aspx--HTH,Kevin SpencerMicrosoft MVPBit Playerhttp://unclechutney.blogspot.comA pea rants as candy be sieving."tbrown" <(E-Mail Removed)> wrote 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'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?

 
Reply With Quote
 
mike
Guest
Posts: n/a
 
      26th Dec 2006
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" <(E-Mail Removed)> wrote 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



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ListBox/Query/Mouseclick Select/Open Form - Getting closer... =?Utf-8?B?U3RlaW5fNjg=?= Microsoft Access 4 16th May 2005 06:21 PM
Mouseclick zoneal@yahoo.com Microsoft VB .NET 3 15th Feb 2005 09:32 PM
Re: Disabling Dismiss or Dismiss all in task reminder Sue Mosher [MVP-Outlook] Microsoft Outlook Form Programming 0 4th Aug 2004 01:58 PM
How to make a ListBox Extending beyond Form Boundaries =?Utf-8?B?YmFsZw==?= Microsoft Dot NET Framework Forms 1 21st Jul 2004 12:38 PM
Dismiss doesn't dismiss Jeff Poretsky Microsoft Outlook 1 16th Oct 2003 05:20 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:01 PM.