Ability to Search for and highlight text in textbox?

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
morgange
Posts: 15
Last visit: Sun Jan 22, 2023 1:14 pm

Ability to Search for and highlight text in textbox?

Post by morgange »

Hi,
I have a GUI that writes a lot of information to a textbox.
I would like to be able to search for certain text and highlight/change its colour & jump to it in the 'results' textbox.

Ideally i would have a button and a textbox at the bottom of the form. The user enters the text they are looking for into the textbox and presses the button.
I have scroll bars on the results textbox but finding specific text at a later time can be difficult.

Pointers are welcome.

Geoff
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Ability to Search for and highlight text in textbox?

Post by davidc »

I recommend looking at the Text Box with Search form template. It offers some basic search functionality out of the box.
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Ability to Search for and highlight text in textbox?

Post by jvierra »

It is not possible to do this immediately with a textbox. It can be done by managing the "paint event of the control.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Ability to Search for and highlight text in textbox?

Post by jvierra »

It is possible to do this with a RichTextBox. See: RichtTextBox Find
User avatar
morgange
Posts: 15
Last visit: Sun Jan 22, 2023 1:14 pm

Re: Ability to Search for and highlight text in textbox?

Post by morgange »

Thanks both,

I have used a textbox in the GUI as i require the Copy & Paste right click context menu functionality.
There is no find method on a textbox.
User avatar
owinsloe
Posts: 161
Last visit: Tue Mar 26, 2024 8:14 pm
Been upvoted: 1 time

Re: Ability to Search for and highlight text in textbox?

Post by owinsloe »

Hi, Are you after something like this...

textbox1 contains your text
textbox2 contains the string you want to search for
when you click the buttonFindText the string will be highlighted in textbox1 and each subsequent click will move onto the next matched string.


$form1_Load = {
$textbox1.Select(0,0)
}

$buttonFindText_Click = {
$Content = $textbox1.Text
$keyword = $textbox2.Text

$IndexArray = @()
$first = $true

$Selected = $textbox1.SelectionStart
($content -split "$keyword") | %{
if ($first)
{
$len += $_.length
}
elseif ( { $len + $_.length + $keyword.length} -ne $Content.length )
{
$len += $_.length + $keyword.length
}
if ($len -ne $Content.length ){ $IndexArray += $len }
$first = $false
}

$textbox1.focus()
$textbox1.Select($IndexArray[0], $keyword.Length)

if ($Selected -eq 0)
{
$global:indexStart = 0
}
else
{
$global:indexStart++
}

$s = $IndexArray[$global:indexStart]
if (-not $s)
{
$global:indexStart = 0
return
}

$textbox1.focus()
$textbox1.Select($IndexArray[$global:indexStart], $keyword.Length)
}
This topic is 6 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked