Autoscroll RichTextArea in C# - c#

I have a Created a logwindow in my project in C#
This log window is nothing but a richtexbox. I am appending the lines in RichTextbox whenever a method is called.
What i want is : It should autoscroll down whenever a new line is Appended in the rich text box.
Can anybody tell me how to do it.
thanks

Basically you just have to set the "cursor" to the end of the box by setting the SelectionStart and afterwards tell the control to scroll to the caret (= the selection):
rtfBox.SelectionStart = rtfBox.Text.Length;
rtfBox.ScrollToCaret();

Related

ToolStripStatusLabel is not displayed after the window be restored(after minimize)

I have a simple StatusStrip with one ToolStripStatusLabel in it. Text in label can be quite long, so I have prefered to display it cutted.
I have set ToolStripStatusLabel properties: Spring=true and TextAlign=MiddleLeft. I didn't want to set StatusStrip's property LayoutStyle = ToolStripLayoutStyle.Flow, because with ToolStripLayoutStyle.Flow the Text will be overridden with triangle(for resizing).
The Text property is set directly after InitializeComonents() and is displayed as expected - cutted.
If I do minimize(to taskbar) and then restore the window, the text will not be displayed at all. If I make window wider I can see the text, and if I bring the window to the initial size I still can see the text, cutted as expected.
I have and will post my solution, but I would ask you, whether you have any elegant one?
The solution I have found is quite simple. Just handle the window's restore event(there is no such event, but there is a workaround:
Is there an event raised in C# when a window is restored?) and reset the text in ToolStripStatusLabel:
var txt = tslabel.Text;
tslabel.Text = " ";
tslabel.Text = txt;

How to save and restore the selection of a WebBrowser control?

I am using a Windows.Forms.WebBrowser control as a text editor. To alter the font size of some selected Text, I display a modal window, where the user can make chices concerning the font size and after closing that window, the previously selected text is decorated with the changes. Unfortunately as soon as the modal window opens, the selection in the main windows isn´t visible anymore and I can´t find a way to save and restore it. I can determine the selected Range using
IHTMLDocument2 htmlDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
IHTMLSelectionObject currentSelection = htmlDocument.selection;
but since htmlDocument.selectionis readonly, I am not able to set it after the modal closes. All I Can do is call Select() on the main window, but then the caret jumps to the end of the text and nothing is selected.
Any ideas how to solve this?
(I know I could use a ComboBox to alter Font-size, but I need the custom window...for reasons.)
You can use bookmarks. Save selection as bookmark:
var bookmark = document.selection.createRange().getBookmark();
Restore:
var range = document.selection.createRange();
range.moveToBookmark(bookmark);
range.select();

How do I avoid having my Rich Text Box, "scroll bar," freeze up?

My issue is in the .NET framework using C# to create a simple form application that contains a rich text box (RTB) control.
Briefly the issue I am experiencing is that when trying to clear the contents (.Text) of the RTB, the scroll bar doesn't go away. I would like to know if there is anything inherently wrong with the way I am using the RTB. I apologize, but the site will not allow me to post images yet. So if there is a misunderstanding regarding what "doesn't go away" means, please ask!
So first, I write data to the box using the following code snippet:
// append the new message
this.rtb_receive_0.Text += message;
this.rtb_receive_0.SelectionStart = this.rtb_receive_0.Text.Length;
this.rtb_receive_0.ScrollToCaret();
Later on, I clear the RTB contents (RTB.Text) with the following code:
this.rtb_receive_0.Text = String.Empty;
this.rtb_receive_0.Refresh();
In the above code I have attempted to fix my problem with the, "Refresh," method. However it does not seem to be doing the job.
When I clear the RTB contents, the scroll bar does not go away... I noticed that if I grab another window and drag it over the top of the application, that the frozen scroll bar disappears. Also, I can minimize the application, then maximize it again and the bar will disappear. There has to be a way to prevent this frozen scroll bar from happening in the first place though.
Per the answer, here was the fix to stop the bar from freezing up:
this.rtb_receive_0.Text = String.Empty;
this.rtb_receive_0.Clear();
this.rtb_receive_0.ScrollBars = RichTextBoxScrollBars.None;
this.rtb_receive_0.ScrollBars = RichTextBoxScrollBars.Vertical;
this.rtb_receive_0.Refresh();
Have you tried simply just programatically setting the Scrollbars property on the RTB?
myRichTextBox.ScrollBars = RichTextBoxScrollBars.None;
Edit: I think I misinterpreted what you needed. Searching around, I found this similar post on another forum: http://www.vbforums.com/showthread.php?793671-RESOLVED-RichTextBox-Visual-Bug
This user is setting the value of an RTB based on a selection in a list view. When a new value is set and does not require a scrollbar it doesn't re-draw and still shows the bar.
It seems like adding myRichTextBox.Clear(); myRichTextBox.Refresh(); should help. In this case that user is also programatically setting the ScrollBars property as well.
Also, are you able to determine how many lines of text can fit in the RichTextBox before a scrollbar is needed? I suppose that might vary based on system settings on the machine, but you might just be able to programatically check myrtb.Scrollbars = (myrtb.Lines.Length > X) ? Vertical : None; (excuse the psuedo code syntax)
What helped for me was just calling the refresh() method twice. Very ugly, but it does the job.
Hmm, after more thorough testing this ugly fix proved to be not so much of a fix afterall. It helps, but still some glitches.
refresh();
update();
seems like a better solution.
I was having this same problem. I solved it by calling the Invalidate() method which forces the control to repaint.
Me.RichTextBox.Clear()
'Call Invalidate in order to force the RichTextBox to repaint. I do this so that any
'Visible Scroll bars are removed after clearing the Text
Me.RichTextBox.Invalidate()
I tried with Refresh(); Update(); Invalidate();,but it didn't worked for me.
I solved this problem using following three lines :-
RitchTextBox.Clear(); //Clearing text in RichTextBox
RitchTextBox.ScrollBars = RichTextBoxScrollBars.None; //Remove scroll
RitchTextBox.ScrollBars = RichTextBoxScrollBars.Vertical; //Again add scroll
Try those above three lines. It will work 100%.

Is there a way to append output to a text field without taking focus?

I'm trying to add text to a RichTextBox using the AppendText method, and would like to find a way to Not take focus of the text box in this motion - reason being that I have an event response to the text box getting focus, that causes a conflict in my overall scheme...
Again, the question here is effectively; How can I use the AppendText method without triggering focus on a rich text box.
As I'm typing this I've almost decided that I can remove my event response method before the append and add it in again after; but if anyone has a better suggestion I'm all ears.
Thanks. And if I can submit any code to spur suggestions I'm open to it; I just assume that most anyone using this site can visualize what I'm portraying.
You can use a boolean variable to determine if it was you who fired the event (or the user)
bool firedByUser ;
When calling the AppendText method do something like this
firedByUser = false ;
rtb.AppendText("sample") ;
firedByUser = true ;
And in the method that you are handling the Focus on the RichTextBox
if(firedByUser)
{
//keep doing what you are doing now
}

Reset the panel scroll position in winform application c#

I'm working on winfom application c#.
I have two forms called Welome and Details.
Details contains 7 grids in the Panel.
Scenario:
If I click on any of the item on welcome page it will take to the Details page with seven grids. If I drag the scroll bar down, and come back after moving back to welcome form, still the scroll bar stays at the same position.
Question:
I want to reset the scroll position to top each time the user visits the details form, so that I can always see first grid.
Set AutoScroll to true
panel1.AutoScroll = true;
And, then in Details form's load event, set the VerticalScroll
panel1.VerticalScroll.Value = 0;
If Angshuman Agarwal's answer doesn't work for you, the culprit is likely that after load some control in the form is receiving focus, which will scroll into view and override any changes to the scroll position.
You could set TabStop to false, but then your form wouldn't be tabbable :(
A clunky work-around, but still relatively simple fix, is manually fire focus on the first control in your form:
yourFirstControl1.Select();
See also: How to make the panel scroll bar to be at the TOP position on loading the form
Old post, but still relevant. The above only worked when I added a line:
displayPanel.AutoScroll = true;
displayPanel.AutoScrollPosition = new Point(displayPanel.AutoScrollPosition.X, 0);
displayPanel.VerticalScroll.Value = 0;
Then it worked great, without having to set any tab indexes.
What is mentioned above is fine but, you should add VerticalScroll.Value in Panel1_Paint
Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
Panel1.VerticalScroll.Value = 0
End Sub

Categories