Richtextbox Auto Scroll

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 11 years and 1 week 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
albaek75
Posts: 14
Last visit: Tue Apr 19, 2016 11:01 pm

Richtextbox Auto Scroll

Post by albaek75 »

Hi

Is it possible to get a richtextbox to autoscroll vertical.

I would like the box to autoscroll when new text is added.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Richtextbox Auto Scroll

Post by davidc »

FYI, I moved this post to the GUI forums.

I guess it depends on where you are inserting the text but you can use the ScrollToCaret method:
PowerShell Code
Double-click the code block to select all.
#Scroll to the end of the textbox
$richtextbox1.SelectionStart = $richtextbox1.TextLength;
$richtextbox1.ScrollToCaret()
David
David
SAPIEN Technologies, Inc.
User avatar
albaek75
Posts: 14
Last visit: Tue Apr 19, 2016 11:01 pm

Re: Richtextbox Auto Scroll

Post by albaek75 »

Hi David

I have attached an example of what I am trying to do.

eks.
Button1: add text to richtextbox1, and auto scroll to end

Button2: append text to richtextbox1, and auto scroll to end

I can not get this to work.

Can you see what I am doing wrong
Attachments
Test.zip
(3.09 KiB) Downloaded 720 times
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Richtextbox Auto Scroll

Post by davidc »

You never should declare a function in a script block if you plan to call it from outside the script block. This works in PowerShell V2 but will fail in V3.

Other than the function declaration the code worked for me. Note when the textbox is not in focus you can't see the caret. You can focus the control when the text changes:
PowerShell Code
Double-click the code block to select all.
$richtextbox1_TextChanged={
	$richtextbox1.SelectionStart = $richtextbox1.TextLength;
	$richtextbox1.ScrollToCaret()
	$richtextbox1.Focus()
}
Also I recommend using the AppendText method:
PowerShell Code
Double-click the code block to select all.
function outTextBox1($textOut)
{
	$richtextbox1.AppendText($textOut)	
}
David
David
SAPIEN Technologies, Inc.
This topic is 11 years and 1 week 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