In C#, I have a RichTextBox, and I want to get the current line of the cursor. Every answer I've found says to use:
int currentLine = richTextBox1.GetLineFromCharIndex(richTextBox1.SelectionStart);
However, richTextBox1.SelectionStart only updates when you make changes to the text. If you move the cursor with the arrow keys, it does not update (I've verified this by printing SelectionStart as I move around).
How do I get the current line of the cursor, in a way that tracks it even if you use the arrow keys to move the cursor around?
I'm using VS2012 in Win8.
Edit: terrybozzio's answer showed the problem. For anyone else with this problem, you can't put the code in richTextBox1_TextChanged. You need to put it in richTextBox1_SelectionChanged.
First you need to get selectionstart,If there isn't
any selected text, the value returned is the position of the caret(with offset in characters from the start of the text),then you call getlinefromcharindex and pass that value,place it in the selectionchanged event and even with arrow keys moving the caret position it will update:
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
int index = richTextBox1.SelectionStart;
int line = richTextBox1.GetLineFromCharIndex(index);
label1.Text = "cursor at line " + line.ToString();
}
Related
Im making a game kind of like HOI4 and ive come to this problem of not knowing how to check bodered territories
my first thought was to set the console's cursor position and then check around that to see if they border or not
so i searched for ways to check the character that the cursor's is sitting on but none of the solutions ive found works
Use the following code to find the position of the cursor
int left = Console.CursorLeft;
int top = Console.CursorTop;
Use the following code to place the cursor in a specific position
Console.CursorLeft = 10;
Console.CursorTop = 15;
or
Console.SetCursorPosition(10, 15);
I do not think it is possible to read the output in a particular situation
see here
I want to get the substring of the text in the RichTextBox of Windows Form when I move the cursor with arrows in the keyboard.
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
string wholeText = richTextBox1.Text;
if (wholeText.Length >0)
{
int positionBegin = richTextBox1.SelectionStart;
SelectedText = wholeText.Substring(positionBegin);
}
}
But it is incorrect. For example, I typed the sentence:
How are you doing?
I want to get the substring
you doing?
There are two ways.
One is from right to left, say I move the cursor to the end of the sentence, use left arrow to move and stop before the letter y. I got
ou doing?
Or from the left to roght, say I move the cursor to the beginning of the sentence, use right arrow to move and stop before the letter y. I got
you doing?
The case one lacks the letter y'. Case two added a white space beforey`.
I tried +1 or -1 to the index, but it generated "IndexOutOfRangeException".
Anything wrong to use SelectionStart?
Use SelectionChanged event instead of KeyDown. I will return correct substring, also it will handle mouse selections.
I have a question about zooming and scrolling. My scroll function doesn't work perfectly, but close enough. Instead of the Maximum value I want to change the ActualMaximum, but that one was protected.
plotBSITotalA.Model.Axes[0].Maximum = hScrollBarA.Value +
(plotBSITotalA.Model.Axes[0].ActualMaximum - plotBSITotalA.Model.Axes[0].ActualMinimum);
plotBSITotalA.Model.Axes[0].Minimum = hScrollBarA.Value;
Ok, here comes the real problem: When I have zoomed in or out, the scroll function won't work anymore in that specific plot Area, where it have been zoomed. Other plotArea's that weren't zoomed, will work perfectly.
Does somebody know, how I can scroll when I zoomed in??
May be, It can be done by connecting two enevts with two methods:
1. event emitted when scrollbar changed with method of changing axes range
2. event emitted when axes range changed with method of changing the scrollbar position
Quite a few views, so maybe still a problem.
As I understand it, your saying the auto scroll stops when you have zoomed back out and subsequently add new values to the series?
Can't find a formal solution, but this works for me
add this to your axis:
xAxis.AxisChanged += OnXAxisChange;
add the method
bool doAxisReset = false;
private void OnXAxisChange(object sender, AxisChangedEventArgs e)
{
//if zoomed fully out, then enable auto scroll
//
if ((e.DeltaMaximum == 0) && (e.DeltaMinimum == 0))
{
doAxisReset = true;
}
}
In the rendering code (ie when you get a new element that triggers autoscrolling) and before InvalidatePlot
if (doAxisReset)
{
xAxis.Reset();
doAxisReset = false;
}
I am redirecting CMD output to a multi-line textbox, I'm trying to auto-scroll down using the following code:
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();
textBox1.Refresh();
However it's really choppy looking, by that I mean, each time a line comes in, it positions the scroll bar at the top of the textbox, then to the bottom, it's hard to describe so I made a .gif of it happening: http://i.imgur.com/mudqrZy.gif
Is there any way to fix it?
For the same purpose I use RichTextBox:
richTextBox1.AppendText(cmdOutputMsg + "\r\n");
richTextBox1.ScrollToCaret();
In this way the new text is always added at the end of the existing text, and with ScrollToCaret the focus remains on the last inserted text.
I think with normal TextBox will work in the same way.
I have a C# Windows Forms program that has a RichTextBox control. Whenever the text inside the box is changed (other than typing that change), the cursor goes back to the beginning.
In other words, when the text in the RichTextBox is changed by using the Text property, it makes the cursor jump back.
How can I keep the cursor in the same position or move it along with the edited text?
Thanks
You can store the cursor position before making the change, and then restore it afterwards:
int i = richTextBox1.SelectionStart;
richTextBox1.Text += "foo";
richTextBox1.SelectionStart = i;
You might also want to do the same with SelectionLength if you don't want to remove the highlight. Note that this might cause strange behaviour if the inserted text is inside the selection. Then you will need to extend the selection to include the length of the inserted text.
Be careful, if someone refreshes or changes totally the RichTextBox content, the focus method must be invoqued previously in order to move the caret:
richTextBox1.Focus();
int i = richTextBox1.SelectionStart;
richTextBox1.Text = strPreviousBuffer;
richTextBox1.SelectionStart = i;
here's a smaller one, that has the same effect. this.richTextBox1.Select(this.richTextBox1.Text.Length, 0);
That marks 0 chars at the end of the text and sets the cursor to end