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.
Locked
User avatar
albaek75
Posts: 14
Joined: Tue Aug 30, 2011 2:09 am

Richtextbox Auto Scroll

Post by albaek75 » Wed Mar 13, 2013 7:01 am

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: 5051
Joined: Thu Aug 18, 2011 4:56 am

Re: Richtextbox Auto Scroll

Post by davidc » Wed Mar 13, 2013 9:07 am

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
Joined: Tue Aug 30, 2011 2:09 am

Re: Richtextbox Auto Scroll

Post by albaek75 » Thu Mar 14, 2013 1:53 am

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 296 times

User avatar
davidc
Posts: 5051
Joined: Thu Aug 18, 2011 4:56 am

Re: Richtextbox Auto Scroll

Post by davidc » Fri Mar 15, 2013 10:19 am

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.

Locked