dismiss a listbox when mouseclick outside boundaries

  • Thread starter Thread starter tbrown
  • Start date Start date
T

tbrown

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
 
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.
 
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?
 
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/li...trol.preprocessmessage(VS.80).aspx--HTH,Kevin SpencerMicrosoft MVPBit Playerhttp://unclechutney.blogspot.comA pea rants as candy be sieving."tbrown" <[email protected]> wrote in messageOn 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?
 
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
 
Back
Top