简体   繁体   中英

vb.net Find Form

In the Windows, the native Notepad program has a find form. 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.

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.

Currently, I'm using Regex to do this and I am using a WPF TextBox using HostElement:

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. How can this be achieved?

I think you should catch "keyup" event of your form like this :

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

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