Building an effective KeyDown event for search RRS feed

  • Question

  • Goal:

    I am attempting to build a KeyDown event so search for a specific string inside of a windows form that I have created.


    # Create Windows Form With Document

    # Now here is my KeyDown Event for searching strings

                 Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName Microsoft.VisualBasic

    $form = New-Object -TypeName System.Windows.Forms.Form $form.StartPosition = 'CenterScreen' $form.KeyPreview = $true $form.Add_KeyDown( { if ($_.Control -and $_.KeyCode -eq 'F') { $stringToFind = [Microsoft.VisualBasic.Interaction]::InputBox('Find what', 'Find') $pos = $textBox.Text.IndexOf($stringToFind); if ($pos -ne -1) { $textBox.SelectionStart = $pos; $textBox.SelectionLength = $stringToFind.Length; } } } ) }

    There is a huge problem here.

    -It only highlights the first string of the document. So if I type 'String' it will only find the first 'String.'


    Can some one point me into the correct direction on how build a more effective KeyDown even for searching particular strings in a windows form? 

    • Edited by Alexander Sinno Thursday, January 12, 2017 11:20 PM fixed format
    Thursday, January 12, 2017 11:19 PM


  • Yes - that is the way "IndexOf" works.  You will have to use the position returned to "SubString" the remainder of the string to find the next occurrence.


    Thursday, January 12, 2017 11:38 PM