【问题标题】:Why does calling Focus() not set focus in this instance?为什么在这种情况下调用 Focus() 不设置焦点?
【发布时间】:2014-07-13 09:34:38
【问题描述】:

我根据this改编了一个自己滚动的输入框。

我将代码修改为:

using System;
using System.Windows.Forms;

public static class PromptForText
{
    public static string ShowDialog(string caption, string text)
    {
        Form prompt = new Form();
        prompt.Width = 280;
        prompt.Height = 150;
        prompt.Text = caption;
        Label textLabel = new Label() { Left = 16, Top = 20, Width = 240, Text = text };
        TextBox textBox = new TextBox() { Left = 16, Top = 40, Width = 240 };
        Button confirmation = new Button() { Text = "Okie Doak", Left = 16, Width = 80, Top = 72 };
        confirmation.Click += (sender, e) => { prompt.Close(); };
        prompt.Controls.Add(confirmation);
        prompt.Controls.Add(textLabel);
        prompt.Controls.Add(textBox);
        prompt.StartPosition = FormStartPosition.CenterScreen;
        prompt.ShowDialog();
        textBox.Focus();
        return textBox.Text;
    }
}

我添加了“textBox.Focus()”,但它并没有达到我的预期。我在调用 ShowDialog() 之前和之后都尝试过。

我错过了什么?为什么没有在文本框上调用焦点将焦点设置为相同?

更新

现在根据史蒂夫(沃兹尼亚克的?)的回答创建控件:

TextBox textBox = new TextBox() { Left = 16, Top = 40, Width = 240, TabIndex = 0, TabStop = true };
Label textLabel = new Label() { Left = 16, Top = 20, Width = 240, Text = text };
Button confirmation = new Button() { Text = "Okie Doak", Left = 16, Width = 80, Top = 72, TabIndex = 1, TabStop = true };

【问题讨论】:

    标签: c# dynamic focus windows-forms-designer


    【解决方案1】:

    调用 ShowDialog 是一个阻塞调用。这意味着在您(或您的用户)关闭表单之前,在此调用之后不会执行任何代码。此时,对 Focus 的调用没有任何效果,因为该表单虽然仍在内存中,但已被隐藏并准备关闭和处理。

    作为一个简单的解决方法,只需将 TextBox 的 TabIndex 属性设置为表单上的第一个控件。这样,焦点由 Forms Framework 代码自动处理(还需要将 TabStop 属性设置为 true)

     TextBox textBox = new TextBox() 
              { Left = 16, Top = 40, Width = 240, TabIndex = 0, TabStop = true };
    

    经过一些测试,我应该补充一点,您还需要在 Button 控件上设置相同的属性才能使其正常工作

     Label textLabel = new Label() { Left = 16, Top = 20, Width = 240, Text = text };
     TextBox textBox = new TextBox()
              { Left = 16, Top = 40, Width = 240, TabIndex = 0, TabStop = true };
     Button confirmation = new Button() 
              { Text = "Okie Doak", Left = 16, Width = 80, Top = 72, TabIndex = 1, TabStop = true };
    

    另一种可能性是在添加按钮和标签之前将 TextBox 作为第一个控件添加到表单的控件集合中

     prompt.Controls.Add(textBox);
     prompt.Controls.Add(confirmation);
     prompt.Controls.Add(textLabel);
    

    【讨论】:

      【解决方案2】:

      您的表单仅在调用ShowDialog() 期间可见。为什么在ShowDialog() 之前设置焦点不起作用我不确定,但可能在加载和显示表单的过程中焦点丢失了。至于为什么 after ShowDialog() 不起作用,此时表单已被用户显示并关闭。太晚了。要在窗体显示之前立即设置焦点,您可以使用其中包含焦点代码的方法处理 prompt.Load

      【讨论】:

        【解决方案3】:

        您的 Focus 命令仅在 ShowDialog 完成后才会发生。如果您查看 ShowDialog 文档 - http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx

        您将看到 ShowDialog 仅在窗口关闭时完成。

        【讨论】:

          猜你喜欢
          • 2021-08-09
          • 1970-01-01
          • 2010-10-16
          • 2011-05-28
          • 2017-01-07
          • 2020-04-21
          • 2012-02-17
          • 2016-06-04
          • 2017-11-26
          相关资源
          最近更新 更多