我有一些文本框,我安排了TabIndexes但是。當我從textbox1
到textbox2
TAB,我想textbox2
中的文本被選中。我嘗試過:當我按Tab鍵未選擇下一個文本框
if (e.KeyCode == Keys.Tab)
{
textbox2.SelectAll();
}
但它不起作用。我怎樣才能做到這一點 ?
我有一些文本框,我安排了TabIndexes但是。當我從textbox1
到textbox2
TAB,我想textbox2
中的文本被選中。我嘗試過:當我按Tab鍵未選擇下一個文本框
if (e.KeyCode == Keys.Tab)
{
textbox2.SelectAll();
}
但它不起作用。我怎樣才能做到這一點 ?
試試這個:
textbox2.SelectionStart = 0;
textbox2.SelectionLength = textbox2.Text.Length;
-1:這不會解決他的問題。問題在於他的代碼在錯誤的時間執行或根本不執行。 –
如何對焦點事件或事情TextBox.SelectAll()。
有一個名爲事件輸入文本框上,在這種情況下選擇begining(0)結束(文字lenght)文本
private void textBox2_Enter(object sender, EventArgs e)
{
textBox2.SelectionStart = 0;
textBox2.SelectionLength = textBox2.Text.Length;
//or also
//textBox2.SelectAll()
}
你應該選擇文本時,文本框得到了焦點。
在WPF中,您應該對GotKeyboardFocus
事件做出反應。
在Winforms中,您應該對GotFocus
事件做出反應。
在這兩種情況下,要執行的代碼只是textbox2.SelectAll();
而不檢查製表符。
在Windows窗體和WPF:
textbox.SelectionStart = 0; textbox.SelectionLength =
textbox.Text.Length;
在ASP.Net:
textBox.Attributes.Add("onfocus","this.select();");
欲瞭解更多詳情請Click Here
我在Windows窗體和同樣的問題,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
希望這有助於別人;)
WPF或的WinForms? –
在哪個事件中您是否觸發此代碼?它應該在錯誤的地方。如果你把'textbox2.SelectAll()'放在同一個'TextBox'的'Enter'方法中。 –
順便說一句,你不需要檢查'e.KeyCode'如果你使用TextBox的'Enter'事件 –