John Smith
John Smith

Reputation: 8821

How to detect MouseDown on any non-TextBox control?

TextBoxes and NumericUpDowns have the odd property of not allowing you to deselect them once they are selected. When my user selects a NumericUpDown and clicks else-where on the form, the NumericUpDown should be deselected.

Unfortunately, this is not the case. Currently I am just handling the MouseDown event of all other controls on the form (like the panels and actual form itself) and just calling the Focus method of a random label to remove the focus from the NumericUpDown. However, this cannot be applied to menu items or scrollbars.

There must be a better way to do this. The user may want to scroll the panel instead of the NumericUpDown and intuitively click the Panel and then use the scroll-wheel, but currently that would scroll the NumericUpDown instead, since it still has focus.

Thanks for reading.

Edit: Problem still unsolved.

Upvotes: 0

Views: 551

Answers (2)

sallushan
sallushan

Reputation: 1147

Normally Panel Control is a Non-Focusable control. Therefore clicking on Panel will NOT remove focus from TextBox or NumericUpDown Countrol.

The workaround can be, place a button on panel and move it away from view for example setting its x = -100 and y = -100. Do NOT set visible = false.

Now whenever user clicks on Panel (Panel_Click event) set focus (Button.Focus()) to that button. In this way panel will be scrollable through scroll-wheel.

Upvotes: 1

swordfish
swordfish

Reputation: 4995

Enclose the numeric box within a panel of some sort and then do

panel1.MouseHover += new EventHandler(panel1_MouseHover);

private void panel1_MouseHover(object sender, EventArgs e)
        {
            if (numericUpDown1.Focused)
            {
                panel1.Focus();
            }
        }

I tested it and it works.!

Upvotes: 0

Related Questions