Get current line's text (where the caret is blinking) of richtextbox - c#

I really need to get the contents of the line where the caret is blinking in a rich text box control. Suppose I have the following text in a rich text box:
This
is
a
test
I would like to retrieve the text of line 2 by providing its index (for example 2 or 1 if the function is zero-based).
Thanks in advance,
Vali

I'm not sure if the question's title and what you really want to know are the same... If you want to get a line given its index, check the Lines property of the RichTextBox.
http://msdn.microsoft.com/en-us/library/system.windows.forms.textboxbase.lines.aspx

Make sure you have the WordWrap for the text box set to false to make sure the index of the line corresponds to the line where the caret is.
Try this:
var pos = richTextBox1.GetLineFromCharIndex(this.richTextBox1.SelectionStart);
MessageBox.Show(richTextBox1.Lines[pos]);

you can do it
richTextBox1.Lines[1]

Related

How to select right text with RichTextBox.Select in C#

I have a RichText in my RichTextBox.
If i print textBox.Text.Length it is shorter than if I print textBox.TextLength.
I want to index search inside the textBox and find a certain string to select, but since they missmatch in length, if i use a textBox.Text.IndexOf("bla"), i get wrong position.
How do I do to get the right position to select?
Try this:
RichTextBox1.Select();
RichTextBox1.Select(RichTextBox1.Text.IndexOf("bla"), "bla".Length);

Get position of selected text in string in textbox

Ok, I'm trying to do something a little specific here. I want to get the location of the selected text in a textbox.
To elaborate- I can use location to select text. If I have a textBox1 I could do:
textBox1.SelectionStart = 1;
textBox1.SelectionLength = 4;
That would start at the second letter and select 4 letters.
What I want to do is the opposite: when the user selects text, I want to find out what the start is and what the length is (or what the start is and what the end is. Either will work).
I thought about just searching the string for the selectedtext (textBox1.SelectedText). The problem comes if it is a common word or a string that is used multiple times. For instance.
This is a cat. This is a cat. This is a cat.
If they select the second sentence, using SelectedText to search the string for that specific sentence does me no good. It could be either of the 3.
So, my question is: When the user clicks a button, how do I determine the exact elements that are selected by the user, so that I can later manipulate those specific elements? Important to note the later part- I likely will not only want to manipulate the text when the button is pressed. I will also want to manipulate it later, at a time when the text may no longer be highlighted. This means I'll want to store SOMETHING to tell me what specific parts of the sentence I'm dealing with. If that solution isn't viable, is there a solution you can think of where, in the above "this is a cat" example, the user could select the second sentence, hit a button, and then later I know which sentence was selected when he hit that button?
According to the documentation, SelectionStart and SelectionLength can be both set and read. Just use those.
You dont even need to know the position of selected text to manipulate them, to edit the text that you have selected in the text you can simple set the SelectedText property to the new edited value.
// if textBox1.text = "Hello World World"; with first "World" selected
textBox1.SelectedText = textBox1.SelectedText.Replace("World", "Raj");
// then it becomes "Hello Raj World"

Issue with textbox rounding a number

I have the following code:
txtbox1.Text = listView1.SelectedItems[0].SubItems[11].Text;
The value of the selected item of the listview is "33,5" but when the code reachs this line, in the textbox writes 34,00.
I don't know why if there's a text inside a text, I have tried convertingo to decimal before asing to the textbox but still put 34,00. I've tried too puting 33.5 instead of 33,5 but then the code writes in the textbox: 3350,0.
What can I do?
Thanks
try this:
string number = listView1.SelectedItems[0].SubItems[11].Text;
and check in debug mode what number contains.
I am convinced you have the right value in there, a simple string, but the txtbox1 is applying certain formatting on text change. You should find this out and fix the way content of txtbox1 is formatted after assignment.

Limit number of characters in Label

I am using label in my winform . It displays the value which I enter in another textbox. My problem is label does not display whole characters I enter in textbox. Label's size is width=160 and height=19. So it truncates the last value. For testing purpose when I enter "W" in caps in textbox ;label can display maximum 13 "W"s. So I am trimming the
charater's by using labelname.substring(0,10); and for next three characters I am appending 3 dots(...)
But this solution is not desirable to my senior. He is telling me that if I enter all small letters "l" in textbox then though label has space to display more than 13 characters it will display only 13 characters(including dots).
Does anybody has solution on that ?? I also cannot increase width or height of label to accomodate more characters.
Well, you could set the AutoEllipsis property to true and don't worry about the length of the text.
Edited to Add: as per comments
If you're using RadLabel from Telerik then you need to dig a little more:
RadLabel.LabelElement.LabelText.AutoEllipsis
Nothing that 5 minutes looking through the documentation doens't solve
Set the AutoEllipsis property of your Label control to true. You can do this either in the designer, or through code:
myLabel.AutoEllipsis = true;
That will cause the ellipsis character (...) to be automatically appended to the text if it overflows the space available in the label. You won't have to manage this yourself in code at all.
This property is available as far back as .NET 3.0.
If I understand your question correctly, you can use Textbox.MaxLength property which only allows the user to enter the maximum number of characters you set the value to.
You can set the label's AutoEllipsis property to true and let it figure this out for itself.
Gets or sets a value indicating
whether the ellipsis character (...)
appears at the right edge of the
Label, denoting that the Label text
extends beyond the specified length of
the Label.

How can I get Column number of the cursor in a TextBox in C#?

I've got a multiline textBox that I would like to have a label on the form displaying the current line and column position of, as Visual Studio does.
I know I can get the line # with GetLineFromCharIndex, but how can I get the column # on that line?
(I really want the Cursor Position on that line, not 'column', per se)
int line = textbox.GetLineFromCharIndex(textbox.SelectionStart);
int column = textbox.SelectionStart - textbox.GetFirstCharIndexFromLine(line);
textBox.SelectionStart -
textBox.GetFirstCharIndexFromLine(textBox.GetLineFromCharIndex(textBox.SelectionStart))
Off the top of my head, I think you want the SelectionStart property.

Categories