Help setting focus on the parent

2019-07-04 16:38发布

I have a simple situation (.NET2): a texbox1 on a UserControl1(or Form1).

I want to unfocus(leave) the texbox when I click on the usercontrol/form(focus the usercontrol/form instead): alt text http://lh3.ggpht.com/_1TPOP7DzY1E/S0R1ORVt-pI/AAAAAAAAC3Y/UkS2zEMWa9o/s800/Capture4.png

I do the following on the UC/form:

      Protected Overrides Sub OnMouseClick _
          (ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseClick(e)
        Me.Focus()
      End Sub

Why does it not work on the child textbox, but works very well on the non-child one(focus on textBox2 then click on the panel removes the focus from the textBox2)?

Real project Window

alt text http://lh5.ggpht.com/_1TPOP7DzY1E/S0SVniaeN1I/AAAAAAAAC3g/jafhFG-vA0g/s800/Capture5.png

4条回答
闹够了就滚
2楼-- · 2019-07-04 17:02
  1. Add a new panel control to your form (somewhere out of the way) and resize it to 0,0
  2. Do NOT set Visible = false on this panel.
  3. In your form add a standard MouseClick event handler as follows:

    private void Form1_MouseClick(object sender, MouseEventArgs e)
    {
        // Uncomment if in scrollable control
        //Point scrollPos = new Point(this.AutoScrollPosition.X, -this.AutoScrollPosition.Y);
        newPanel.Select(); 
        // Uncomment if in scrollable control
        //this.AutoScrollPosition = scrollPos;
    
    }
    
  4. Now, when you click anywhere on your main form, any input control will lose focus and you will be able to handle the usual Validating events etc.
查看更多
▲ chillily
3楼-- · 2019-07-04 17:10

You are battling built-in behavior of both the Form and the UserControl class. They were written to never accept the focus if they contain any child controls, they automatically move the focus to a child instead. Built-in behavior for the ContainerControl class, the base class for both. GroupBox is another one.

That makes a lot of sense if you think about it: neither is capable of showing that they have the focus nor is there anything useful that would happen when the user starts typing.

Don't fix this, it will just deeply confuse the user.

查看更多
不美不萌又怎样
4楼-- · 2019-07-04 17:14

It is better simply to trap the mouse event on your usercontrol and set focus on itself. This will automatically take the focus out of textbox.

查看更多
Juvenile、少年°
5楼-- · 2019-07-04 17:20

It looks like you're trying to partially duplicate the behavior you see in a Chart in Excel. You can click on any object in the chart, and it is selected (has focus), but if you click outside the chart area, but still within the chart object, nothing is selected.

This would be very familiar behavior to people who are used to charts in Excel.

Basically, you need a control that will normally accept the focus. I've wrestled with this same problem in the past. The best solution I have come up with (and by 'best' I don't mean it's actually good) is to create a blank textbox, and position it behind your UserControl. Then your code for clicking on the usercontrol looks like this:

Protected Overrides Sub OnMouseClick _
          (ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseClick(e)
        myFakeTextBox.Focus()
End Sub

The user never sees this textbox getting the focus. All they see is that now nothing is selected in the form, and they are free to go about their business.

This method works just fine, but from a maintenance standpoint it is a bit of a headache, as you have to remember that the phony textbox is there and what it's good for.

查看更多
登录 后发表回答