In my project I want to count lines displayed by richtextbox in C# with word wrap property set true. For example, in richtextbox, I write a text, and when text reaches the width of the richtextbox, then it automatically goes to second line. So what happens is the text contains actually one line but it is displayed in two lines. So I want to know how can I count this as two lines instead of one? Help will be really appreciated.
You may go for RichTextBox.GetLineFromCharIndex Method
Retrieves the line number from the
specified character position within
the text of the RichTextBox control. If WordWrap is set to false, no portion of the line wraps to the next, and the method returns 0 for the specified character index.
Also, check out this link.
Related
I am currently struggling with finding a proper solution for following problem:
Imagine you have an MS Word (2019) document containing a table consisting of two columns 'A' and 'B'. Text in column B shall always be aligned vertically with the last text line of the text in column 'A'. OK, easy task if the row is on one page only: simply set vertical text alignment of 'B' to 'bottom' and you're done.
Now the BUT: If the the text in 'A' gets too long, Word shall break the cell across pages (hat's a mandatory requirement!). But even if the long text in column 'A' now ends on page 2, the short text in 'B' will be placed on bottom of page 1!
A possible workaround would be to fill up the 'B' with multiple LFs and/or LF+CRs, but I hope someone can explain how a proper solution could look like?!
Thanks in advance!
Desired text position
Wrong text position if row is split across 2 pages
I have a WinForms DataGrid. One column takes formulas from user (like "1+55-var+4"). The content of this column could be very long so I enabled text wrapping in that column.
myGridView.Columns(5).DefaultCellStyle.WrapMode = DataGridViewTriState.True
myGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders
myGridView.Columns(5).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
That behaviour works perfectly for common text columns because the wrapping recognizes white spaces for line breaking.
But my fomula content does not have white spaces. I want that line breaks happen on special character (that means +, -, /, *) or if the column space overlaps.
Has anyone an idea? Thanks.
Assuming you don't need the text to wrap while the text box is being edited, the most straightforward solution would be to add and remove return characters yourself every time the TextBox loses focus or is resized. (Like G3nt_M3caj said) However, depending on the font figuring out exactly when and where to put returns might get messy.
Alternatively, everytime the TextBox loses focus find all of the special characters in the text and append & prepend the character with a Zero Width Space (U+200B). And everytime the textbox gains focus find and remove all Zero Width Spaces. You then don't need to even worry about the TextBox resizing.
If your making a digital button calculator, you additionally need to add the Zero Width Space before and after every special character like so:
private void ButtonClick(object sender, EventArgs e)
{ string key = ((Button)sender).Text; //A string containing the character that has been clicked.
if ( '0' <= key[0] && key[0] <= '9' ) textBox1.AppendText(key);
else textBox1.AppendText('\u200B'+ key + '\u200B');
}
On a final note, whatever you do make sure the text with Zero Width Spaces cannot be copied. It would be a nightmare to debug code that somehow got a Zero Width character in it.
EDIT: I just realized you never said you were using a TextBox inside your DataGrid. Well I think it will work either way.
I'm looking for a code-how that can bold selective words and/or lines of text within one of WPF's RichTextBoxes, so that, for example, I could have code that ensures the word bananabread would be automatically set to bold whenever it were typed. Another example of this could be setting an entire line to bold if it begins with the word "Therefore."
I'm aware that you can bold specific lines via XAML, and I know that pressing Ctrl + B can bold specific words while in the editor itself, but how can I go about doing this through C#?
I am using the Scintilla .NET text editor control (ScintillaNet.dll) to display SQL. I am using the following command to position the caret cursor at a given line number. In the example below, I am positioning the caret cursor at line 102 (0 based. The grid displays 1-based line numbers.)
scintilla1.GoTo.Line(102); //0 based
I'd like text in the viewport to be displayed at the top of the screen as shown below, as the first visible line
How can I identify how to do this?
Update
This looked promising:
scintilla1.Lines.FirstVisible.Number = targetLineNumber;
but after executing, scintilla1.Lines.FirstVisible.Number wasn't always equal to targetLineNumber and I don't know what is interfering with it.There are hundreds of lines following the targetLineNumber line.
Get/Set first visible line works for me https://www.scintilla.org/ScintillaDoc.html#SCI_SETFIRSTVISIBLELINE
You can keep the cursor position first (SCI_GOTOPOS) and then set the first visible line
I am custom drawing a text box and now I am implementing the part where the user can click on a line and have the cursor move to where he clicked.
I know how to get the row of text he clicked on because the character height is constant across all fonts, but not the column, because I'm not sure how to say "get me all the text that can be drawn before this amount of pixels," and because character width is not consistent unless you're using a fixed-width font, which is not a guarantee.
So I have the point from which I'm drawing the string (0) then I have the point that the user clicked. How do I get the string index of the character they clicked on?
Extra info: I am drawing the text by storing the lines in a List then iterating the list and using Graphics.DrawString on each line.
There is no simple method to find the character at a pixel.
However you can find the pixels that a string will fill. Use the Graphics.MeasureCharacterRanges method. You can perform a binary search on your string until you find the string where MeasureCharacterRanges returns your cursor position.
Note: You might see the Graphics.MeasureString method and be tempted to use that. DON'T! That method doesn't return accurate measurements. I can't remember why, but you will do your head in if you try!