Scrollbar problem
-
Hi! I have a richtextbox with a vertical scrollbar. My problem is that the scrollbar is always at the top. How do I get the scrollbar to always be down, showing the lastest added text? Andreas Färnstrand
Just a little supplement: if you're using the .NET Framework 2.0 you will encounter an error in the Beta 1 Build of the RichTextBox (see RichTextBox ScrollToCaret has different behavior then TextBox ScrollToCaret[^]) Best regards Dennis
-
Hi Andreas! I have a richtextbox with a vertical scrollbar. My problem is that the scrollbar is always at the top. You can use the TextBoxBase.ScrollToCaret[^] method.
richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.SelectionLength = 0;
richTextBox1.ScrollToCaret();Best regards Dennis
Hi Dennis! Thanks for your help. It doesn't seem to work though. Here's my code: private void Print(string text){ this.messagesrichtextbox.AppendText(text + "\n"); this.messagesrichtextbox.SelectionStart = this.messagesrichtextbox.Text.Length; this.messagesrichtextbox.SelectionLength = 0; this.messagesrichtextbox.ScrollToCaret(); } Simple enough, but it won't work. I've tried adding a this.messagesrichtextbox.Select also but it didn't work either. What's wrong? Thanx, Andreas Andreas Färnstrand
-
Hi Dennis! Thanks for your help. It doesn't seem to work though. Here's my code: private void Print(string text){ this.messagesrichtextbox.AppendText(text + "\n"); this.messagesrichtextbox.SelectionStart = this.messagesrichtextbox.Text.Length; this.messagesrichtextbox.SelectionLength = 0; this.messagesrichtextbox.ScrollToCaret(); } Simple enough, but it won't work. I've tried adding a this.messagesrichtextbox.Select also but it didn't work either. What's wrong? Thanx, Andreas Andreas Färnstrand
Hi Andreas! Thanks for your help. It doesn't seem to work though. Well, I've tested this using VS 2005 so this difference in the behaviour could be due to chances made to the .NET Framework 2.0. Try setting the focus to the RichTextBox before calling
ScrollToCaret
(right now I don't have the Framework 1.1 installed so I can't verfiy if that's the problem). If this solves your problem you might want to add a few lines to restore the focus after scrolling the text.richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.SelectionLength = 0;
richTextBox1.Focus();
richTextBox1.ScrollToCaret();Best regards Dennis
-
Hi Andreas! Thanks for your help. It doesn't seem to work though. Well, I've tested this using VS 2005 so this difference in the behaviour could be due to chances made to the .NET Framework 2.0. Try setting the focus to the RichTextBox before calling
ScrollToCaret
(right now I don't have the Framework 1.1 installed so I can't verfiy if that's the problem). If this solves your problem you might want to add a few lines to restore the focus after scrolling the text.richTextBox1.SelectionStart = richTextBox1.Text.Length;
richTextBox1.SelectionLength = 0;
richTextBox1.Focus();
richTextBox1.ScrollToCaret();Best regards Dennis
Hi again! I'm sorry! It does work. The problem seems to be that there's a loop that wont let SetToCaret() work until it's finished. You can add text to the richtextbo but it doesn't scroll down until I shut the loop down. The loop is lístening to a Socket, while( online ). Thanks again, Andreas Andreas Färnstrand
-
Hi again! I'm sorry! It does work. The problem seems to be that there's a loop that wont let SetToCaret() work until it's finished. You can add text to the richtextbo but it doesn't scroll down until I shut the loop down. The loop is lístening to a Socket, while( online ). Thanks again, Andreas Andreas Färnstrand
Hi Andreas! The problem seems to be that there's a loop that wont let SetToCaret() work until it's finished. Then try to call
RichTextBox.Refresh()
afterRichTextBox.ScrollToCaret()
. The loop is lístening to a Socket, while( online ). Another idea would be to use asynchronous calls to get the data (see Using an Asynchronous Client Socket[^]). Best regards Dennis -
Hi! I have a richtextbox with a vertical scrollbar. My problem is that the scrollbar is always at the top. How do I get the scrollbar to always be down, showing the lastest added text? Andreas Färnstrand
This has been asked 10 gazillion times, so here goes one solution:
public class RichTextBox : System.Windows.Forms.RichTextBox
{
void ScrollToEnd()
{
const int msg = 0x400 + 222;
int h = Height;
int y = Font.Height * Lines.Length - h;
Point point = new Point(0, y);
SendMessage(Handle, msg, 0, ref point);
}[DllImport("user32.dll")]
extern static int SendMessage(IntPtr hwnd, int id, int res, ref Point pos);protected override void OnSizeChanged(EventArgs e)
{
ScrollToEnd();
base.OnSizeChanged (e);
}//this scrolls the text to the last line when ever text is changed.
protected override void OnTextChanged(EventArgs e)
{
ScrollToEnd();
base.OnTextChanged (e);
}
}top secret
Download xacc-ide 0.0.3 now!
See some screenshots -
This has been asked 10 gazillion times, so here goes one solution:
public class RichTextBox : System.Windows.Forms.RichTextBox
{
void ScrollToEnd()
{
const int msg = 0x400 + 222;
int h = Height;
int y = Font.Height * Lines.Length - h;
Point point = new Point(0, y);
SendMessage(Handle, msg, 0, ref point);
}[DllImport("user32.dll")]
extern static int SendMessage(IntPtr hwnd, int id, int res, ref Point pos);protected override void OnSizeChanged(EventArgs e)
{
ScrollToEnd();
base.OnSizeChanged (e);
}//this scrolls the text to the last line when ever text is changed.
protected override void OnTextChanged(EventArgs e)
{
ScrollToEnd();
base.OnTextChanged (e);
}
}top secret
Download xacc-ide 0.0.3 now!
See some screenshotsleppie wrote: This has been asked 10 gazillion times, so here goes one solution I don't see the advantage of using
SendMessage
instead of the RichTextBox's own methods. Why would you prefer that solution? Best regards Dennis -
leppie wrote: This has been asked 10 gazillion times, so here goes one solution I don't see the advantage of using
SendMessage
instead of the RichTextBox's own methods. Why would you prefer that solution? Best regards DennisBecause the methods suppliedin RichTextBox, doesnt work if the control dont have focus, also focusing the control, brings in a host of other issues. Try it! :p top secret
Download xacc-ide 0.0.3 now!
See some screenshots -
Because the methods suppliedin RichTextBox, doesnt work if the control dont have focus, also focusing the control, brings in a host of other issues. Try it! :p top secret
Download xacc-ide 0.0.3 now!
See some screenshotsleppie wrote: Because the methods suppliedin RichTextBox, doesnt work if the control dont have focus, also focusing the control, brings in a host of other issues. Try it! Well, I did. Where is no need to set the focus. Not with the .NET Framework 2.0. ;-) I know it's just the first Beta but hopefully this change will also be in the RTM. Best regards Dennis
-
leppie wrote: Because the methods suppliedin RichTextBox, doesnt work if the control dont have focus, also focusing the control, brings in a host of other issues. Try it! Well, I did. Where is no need to set the focus. Not with the .NET Framework 2.0. ;-) I know it's just the first Beta but hopefully this change will also be in the RTM. Best regards Dennis
Dennis C. Dietrich wrote: Not with the .NET Framework 2.0. Most people deal with .NET 1.1 unless otherwise specified, what is the point of mentioning it will be fixed, if it is broken currently? top secret
Download xacc-ide 0.0.3 now!
See some screenshots -
leppie wrote: Because the methods suppliedin RichTextBox, doesnt work if the control dont have focus, also focusing the control, brings in a host of other issues. Try it! Well, I did. Where is no need to set the focus. Not with the .NET Framework 2.0. ;-) I know it's just the first Beta but hopefully this change will also be in the RTM. Best regards Dennis
Your method also fails to consider that text might be selected in the RTB, so much more work to handle that, now add some color and threads, and you have a nice mess. top secret
Download xacc-ide 0.0.3 now!
See some screenshots -
Dennis C. Dietrich wrote: Not with the .NET Framework 2.0. Most people deal with .NET 1.1 unless otherwise specified, what is the point of mentioning it will be fixed, if it is broken currently? top secret
Download xacc-ide 0.0.3 now!
See some screenshotsleppie wrote: Most people deal with .NET 1.1 unless otherwise specified, what is the point of mentioning it will be fixed, if it is broken currently? It depends. In case someone needs the functionality right now there is no point. However if someone just started a project which is supposed to be finished in one or two years it would already be worth considering .NET 2.0 specific solutions. Besides it doesn't hurt to learn about new features as soon as possible (that might keep one from using obsolete code later). Best regards Dennis
-
Your method also fails to consider that text might be selected in the RTB, so much more work to handle that, now add some color and threads, and you have a nice mess. top secret
Download xacc-ide 0.0.3 now!
See some screenshotsleppie wrote: Your method also fails to consider that text might be selected in the RTB, so much more work to handle that, now add some color and threads, and you have a nice mess. Yeah, you're right. But how am I supposed to know if that's required? I guess that's the whole point here. You contributed a solution, I contributed a solution. Yours is a general purpose one. However it depends on Win32 API calls (and therefore isn't platform-independent... yeah, I know... I don't know if that's required either ;-)). Mine has some drawback with complex states of the RichTextBox. But following the Simplicity XP rule[^] (as well as the KISS principle[^])
ScrollToCaret()
would still be my first suggestion. If it turns out not to meet the requirements it has to be re-written. You see, if one starts with the more complex solution and it is unnecessary someone might replace it during refactoring anyway (following the Refactor Mercilessly rule[^]). Best regards Dennis