【问题标题】:When I press TAB the next textbox is not being selected当我按 TAB 时,未选择下一个文本框
【发布时间】:2012-12-28 07:52:03
【问题描述】:

我有一些文本框,但我安排了 TabIndexes。当我从textbox1 切换到textbox2 时,我希望选择textbox2 中的文本。我试过了:

if (e.KeyCode == Keys.Tab)
{
   textbox2.SelectAll();
}

但它不起作用。我该怎么做?

【问题讨论】:

  • 您在哪个事件中触发此代码?它应该在错误的地方。如果您将textbox2.SelectAll() 放入同一TextBoxEnter 方法中,则它可以工作。
  • 顺便说一句,如果你使用TextBox的Enter事件,你不需要检查e.KeyCode
  • 检查这个答案...stackoverflow.com/a/102095/314488

标签: c#


【解决方案1】:

TextBox.SelectAll() 在焦点事件上怎么样?

【讨论】:

    【解决方案2】:

    TextBox上有一个名为Enter的事件,在这个事件中选择从开始(0)到结束(文本长度)的文本

    private void textBox2_Enter(object sender, EventArgs e)
    {
        textBox2.SelectionStart = 0;
        textBox2.SelectionLength = textBox2.Text.Length;
        //or also
        //textBox2.SelectAll()
    }
    

    【讨论】:

      【解决方案3】:

      在 Windows 窗体和 WPF 中:

       textbox.SelectionStart = 0; textbox.SelectionLength =
       textbox.Text.Length;
      

      在 ASP.Net 中:

      textBox.Attributes.Add("onfocus","this.select();");
      

      更多详情请Click Here

      【讨论】:

        【解决方案4】:

        当文本框获得焦点时,您应该选择文本。

        在 WPF 中,您应该对 GotKeyboardFocus 事件做出反应。
        在 Winforms 中,您应该对 GotFocus 事件做出反应。

        在这两种情况下,要执行的代码都是简单的textbox2.SelectAll();,没有检查制表键。

        【讨论】:

          【解决方案5】:

          我在 Windows Forms 和 vb.net 中遇到了同样的问题(它可能很容易转换为 c#),我通过以下方式解决了它:

          1.将表单 KeyPreview 属性设置为 true。

          获取或设置一个值,该值指示在将事件传递给具有焦点的控件之前表单是否会接收键事件。

          这允许您只为表单而不是为每个文本框处理键事件。显然,如果您只有一个文本框,这将不会为您节省任何工作。

          2。处理表单 KeyUp 事件

          看起来 KeyDown 和 KeyPressed 事件不会为 Tab 键触发,但出乎意料的是,KeyUp 会...

          我留下我在 KeyUp 事件中使用的代码:

          Private Sub MyForm_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
              // Do nothing if key other than TAB is pressed
              If Not e.KeyCode = Keys.Tab Then Exit Sub
          
              // Search for the control that currently has the focus
              // As we are only interested in doing something when the focus is in textboxes, we do not even search the focus for other controls
              Dim focused_textbox As TextBox = Nothing
              For Each p As TextBox In GetAllTextBoxes(Me) //GetAllTextBoxes is a function that gets a list with all the textboxes for the form passed as a parameter.
                  If p.Focused Then
                      focused_textbox = p
                      Exit For
                  End If
              Next
          
              // If no textbox has the focus, no actions are required.
              If focused_textbox Is Nothing Then Exit Sub
          
              // If the textbox with the focus does not have any content, nothing is to be selected....
              If String.IsNullOrEmpty(focused_textbox.Text) Then Exit Sub
          
              // select all the textbox contents
              focused_textbox.SelectAll()
              /* I've also seen arroun the following sollution, instead of the 'focused_textbox.SelectAll()', but I have not tried it, as SelectAll worked perfect for me
              focused_textbox.SelectionStart = 0
              focused_textbox.SelectionLength = focused_textbox.Text.Length
              */
          End Sub
          

          我还给你我的“GetAllTextBoxes”函数,它可能不是最有效的方法,但它有效。

          Function GetAllTextBoxes(ByVal control_or_form As Object) As List(Of TextBox)
              Dim l As List(Of TextBox) = New List(Of TextBox)
          
              // Fill control_collection with child controls of the control_or_form 
              Dim control_collection As List(Of Control) = New List(Of Control)
              If TypeOf control_or_form Is Windows.Forms.Form Then
                  Dim form As Windows.Forms.Form = CType(control_or_form, Windows.Forms.Form)
                  If form.HasChildren Then
                      For Each c As Control In form.Controls
                          control_collection.Add(c)
                      Next
                  Else
                      Return l
                  End If
              ElseIf TypeOf control_or_form Is Windows.Forms.Control Then
                  Dim control As Windows.Forms.Control = CType(control_or_form, Windows.Forms.Control)
                  If control.HasChildren Then
                      For Each c As Control In control.Controls
                          control_collection.Add(c)
                      Next
                  Else
                      Return l
                  End If
              Else
                  Return l
              End If
          
             // At this point if control_or_form is not a control or a form, or if it has no children, the function had already returned an empty list meaning 'this object has no child textboxes'
             // Now, for all the child controls, store them into the list if they are TextBoxes and, if not, search more TextBoxes within its childs if it has any.
              For Each child_c As Control In control_collection
                  If TypeOf child_c Is TextBox Then
                      l.Add(child_c)
                  End If
          
                  If child_c.HasChildren Then
                      l.AddRange(GetAllTextBoxes(child_c)) //Here we see why this function needs to allow input form and control at the same time 
                  End If
              Next
          
              Return l
          End Function
          

          希望这对某人有所帮助;)

          【讨论】:

            【解决方案6】:

            试试这个:

            textbox2.SelectionStart = 0;
            textbox2.SelectionLength = textbox2.Text.Length;
            

            【讨论】:

            • -1:这不会解决他的问题。问题是他的代码在错误的时间执行或根本没有执行。
            猜你喜欢
            • 1970-01-01
            • 2019-05-22
            • 1970-01-01
            • 2015-07-04
            • 1970-01-01
            • 1970-01-01
            • 2017-11-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多