简体   繁体   中英

How to prevent text box from gaining focus unless clicked?

I have a WinForms application that has a TextBox control (search box) at the top of it. This TextBox is constantly receiving focus during normal application use, and it is very distracting.

I would like the TextBox to only receive the focus if the user explicitly clicks on it.

I can think of a couple rather complicated ways to accomplish this:

  • Change an image of a text box into a text box when clicked
  • Keep track of mouse clicks and shift the focus away based on mouse state

Is there something simpler that I can do to accomplish this?


Edit to add better description of problem based on new understanding

Based on the answers that I have received, I now have a bit of a better understanding of what was causing this problem. As the user interacted with my application, various actions would cause controls to either be disabled or to completely disappear. If one of these controls happened to have the focus at the time, then the next control in the tab order would receive the focus.

I don't know what was the "next control" before I added the text box in question. The application has hundreds of controls on screen at any given time, and I'm pretty sure that tab order was never intentionally defined. Whatever it was before, it was innocuous. After adding the search text box, it seemed like that control would always end up with the focus.

Here is a very simple example that demonstrates what was happening:

public class Form1 : Form
{
    public Form1()
    {
        var button = new Button
        {
            Location = new System.Drawing.Point(159, 67),
            Size = new System.Drawing.Size(75, 23),
            TabIndex = 0,
            Text = @"Click me"
        };
        button.Click += (sender, args) => button.Enabled = false;

        var textBox = new TextBox
        {
            Location = new System.Drawing.Point(159, 142),
            Name = "textBox1",
            Size = new System.Drawing.Size(174, 20),
            TabIndex = 1
        };

        SuspendLayout();
        ClientSize = new System.Drawing.Size(486, 392);
        Controls.Add(textBox);
        Controls.Add(_button);
        ResumeLayout(false);
        PerformLayout();
    }
}

After starting the application, clicking on the button will force the text box to get the focus, since it is the next in the tab order. As mentioned by Handbag Crab in the accepted answer, this behavior can be avoided by setting TabStop = false on the text box.

textBox1.TabStop = false;

上面的内容应该阻止它从Tab键接收焦点。

Subclass the TextBox and over WndProc function to capture the focus message and handle it. Maybe something like this:

if (m.Msg == WM_MOUSEACTIVATE) {
    m.Result = (IntPtr)MA_NOACTIVATEANDEAT;
    return;
  }
  base.WndProc(ref m);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM