简体   繁体   English

vb.net查找表格

[英]vb.net Find Form

In the Windows, the native Notepad program has a find form. 在Windows中,本机记事本程序具有查找表单。 Basically When the user types and hits 'Find Next', the program proceeds to find the text while keeping the focus on the Find form. 基本上,当用户键入并单击“查找下一个”时,程序将继续查找文本,同时将重点放在“查找”表单上。 This way the user can keep hitting the 'Enter' key or the button and still have the text highlighted while the form is focused. 这样,用户可以继续按“ Enter”键或按钮,并在突出显示表单时仍然突出显示文本。

My problem is that I have a different form for the "Search" feature and whenever the user hits "Enter" the text is found and the focus is set on the TextBox but when the user hits "Enter" again, the text gets edited because of the focus. 我的问题是,“搜索”功能的格式不同,每当用户单击“ Enter”时,都会找到文本并将焦点设置在TextBox上,但是当用户再次单击“ Enter”时,文本将被编辑,因为的重点。

Currently, I'm using Regex to do this and I am using a WPF TextBox using HostElement: 当前,我正在使用正则表达式来执行此操作,并且正在使用通过HostElement的WPF TextBox:

Private Function GetRegExpression() As Regex
    Dim result As Regex
    Dim regExString As [String]
    regExString = txtbx_Find.Text

    If matchCaseCheckBox.Checked Then
        result = New Regex(regExString)
    Else
        result = New Regex(regExString, RegexOptions.IgnoreCase)
    End If
    Return result
End Function

Private Sub FindText()
    ''
    Dim WpfTest1 As New SpellPad.Tb
    Dim ElementHost1 As System.Windows.Forms.Integration.ElementHost = frm_Menu.Controls("ElementHost1")
    Dim TheTextBox As System.Windows.Controls.TextBox = CType(ElementHost1.Child, Tb).ctrl_TextBox
    ''
    If isFirstFind Then
        regex = GetRegExpression()
        match = regex.Match(TheTextBox.Text)
        isFirstFind = False
    Else
        match = regex.Match(TheTextBox.Text, match.Index + 1)
    End If
    If match.Success Then
        Dim row As Integer = TheTextBox.GetLineIndexFromCharacterIndex(TheTextBox.CaretIndex)
        MoveCaretToLine(TheTextBox, row + 1)
        TheTextBox.SelectionStart = match.Index
        TheTextBox.SelectionLength = match.Length
        TheTextBox.Focus()
        Me.Focus()
    Else
        MessageBox.Show([String].Format("Cannot find ""{0}""   ", txtbx_Find.Text), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        isFirstFind = True
    End If
End Sub


Private Sub btn_FindNext_Click(sender As Object, e As EventArgs) Handles btn_FindNext.Click
    ''
    Dim WpfTest1 As New SpellPad.Tb
    Dim ElementHost1 As System.Windows.Forms.Integration.ElementHost = frm_Menu.Controls("ElementHost1")
    Dim TheTextBox As System.Windows.Controls.TextBox = CType(ElementHost1.Child, Tb).ctrl_TextBox
    ''
    FindText()
    'theTextBox.Focus()

End Sub

I want it to be just like Notepad where the user hits "Enter" and keeps focus on the Find Form while selecting the text. 我希望它就像记事本一样,用户单击“ Enter”并在选择文本时将重点放在“查找表单”上。 How can this be achieved? 如何做到这一点?

I think you should catch "keyup" event of your form like this : 我认为您应该像这样捕获表单的“ keyup”事件:

Class MainWindow
Private Sub Window_KeyUp(sender As System.Object, e As System.Windows.Input.KeyEventArgs) Handles MyBase.KeyUp
    If e.Key = Key.Enter Then
        FindNext()
    End If
End Sub
Private Sub btn_FindNext_Click(sender As Object, e As EventArgs) Handles btn_FindNext.Click
    FindText()
End Sub
Private Sub FindNext()
    ''
    Dim WpfTest1 As New SpellPad.Tb
    Dim ElementHost1 As System.Windows.Forms.Integration.ElementHost = frm_Menu.Controls("ElementHost1")
    Dim TheTextBox As System.Windows.Controls.TextBox = CType(ElementHost1.Child, Tb).ctrl_TextBox
    ''
    FindText()
    'theTextBox.Focus()

End Sub
End Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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