Manual word-wrap in RichTextBox - winforms

I need to create RichTextBox in Windows Forms, which will have maximum 18 characters in a row and maximum 6 rows. The problem with RichTextBox is when you write very long word, it is displayed in two rows, but it is treated like one row. For example:
This is
supercalifragilist
icexpialidocious
This is a text with two lines.
My first approach was to create own class, which was inheriting from RichTextBox and override OnTextChanged. It was nicely done, but I totally forgot about word-wrap. So I implemented word-wrapping in my approach.
Algorithm is pretty simple: when text changes, split it by 'space' and 'new line' characters. Then I'm counting how many characters were entered in a row, and if there is space for word, I place it in this row. When there is no space for word, I'm creating new line.
And annoying part: now it have some bugs, which I don't know how to solve. Let's assume user has written 18-characters long word. He has a cursor after last character. And now:
He can press Return, to manually make new line character, which always should be in this place.
He can press space to start writing new word.
He can continue to write other characters for this word.
In each scenario he will end up with 18-characters long word and 'new line' character. I don't know how to detect, what he wanted to do. And please keep in mind that user can place cursor everywhere and edit the text he has entered previously.
I know I can create custom string class (inheriting from string) or keep list of entered words, but implementing this will be painful and code will start to be unreadable.
Any ideas how to do this better?

I figured it out. My other idea was to use StringReader and read whole text character by character, but it was also very buggy and painful to implement.
The best way is to not change the default behaviour of RichTextBox. I'm not splitting the text, I'm not checking if there is a space for word in line. My RichTextBox has fixed size and I'm okay with that. There is no problem with 18 characters in row, but the problem is with counting lines, because it's not that simple like counting new line characters.
The reason I wanted to change the behaviour is because later I must send this text line by line. Manually counting how many words fit in one line and dynamically create lines seemed to be easy, but it was to buggy.
RichTextBox has very useful method GetLineFromCharIndex. I'm able to determine how many actual lines I have, by passing Text.Count() to it. Now I have two time shorter code than before and it seems there are no bigger bugs. Sending the text line by line it's not so easy, because I needed to manually count in which line I am, when reading characters, but overall, it was more acceptable then previous solution.

Related

How to make a gtktextview drop to the next row when getting to the border of the window?

I want to make a textview that will output strings that may be multiple rows long, and to divide them when they reach the border of the window. The window is set to non-resizable, so its size is fixed.
I tried to get the column and row numbers and work with these, but each character has a different size in pixels, so while a row full of "m" characters would reach the end of the row with the tenth character, a row of "0" would need about sixteen characters to get to the end of the line.
Any ideas?
It seems that you want to implement line wrapping in a GtkTextView. You don't need to do it yourself, the text view widget already supports line wrapping. To turn it on, call gtk_text_view_set_wrap_mode() with the text view and the appropriate wrap mode, such as GTK_WRAP_CHAR.

ToolTip works very slow for long text (2000+ chars)

We need to use the built-in WinForms tooltip control to display a very long tooltip (about 4000 characters) for one of our controls. But if we do so, the form freezes for a minute or two when we place the mouse pointer into the target control to see the tip. And nothing happens after that.
We experimented and detected that the standard tooltip starts to work very slow when it has about 2000 chars, and the situation becomes much worse when we increase the number of chars. Is it a known issue, and is there any workaround for it? Please, don't suggest to trim the tip text - we need to display the string as is.
When you assign a string of text to a ToolTip, part of the process of drawing it involves calls to USP10.dll which handles Unicode layout of characters on screen. I was able to see this by looking at the stack trace while the program was freezing. The performance of this layout is terrible for long strings.
Disabling Visual Styles for the application (commenting out EnableVisualStyles()) fixed the problem - the tooltip displays immediately, though this is not an optimal solution.
I kept looking and found this page which indicates the problem may be linked to layout of long strings where word-wrap is necessary. By inserting line breaks into the tooltip text, I found that the string displayed immediately. So, if you can determine where to insert the line breaks manually, the ToolTip should display quickly.
What about using another Tooltip , i.e. HtmlToolip?

WPF TextBlock Cutoff

Hi Guyz I have a WPF TextBlock of fixed width say 100 , If the string doesnt fit in the width the last character is being cutoff always as all the characters are of not the same size. I dont want to cut the character instead I want to skip the text from there and just display the text with no character cutoff.
You have a couple of options to control wrapping and cutting of text:
TextWrapping can be used to make the text flow to the next line
TextTrimming can be used to decide how to cut text that doesn't fit
TextTrimming=None (the default) will mean that text which doesn't fit will be hidden, but it may cut down the middle of a character, which sounds like the problem you describe.
TextTrimming=WordEllipsis or TextTrimming=CharacterEllipsis will avoid showing half a character, but will append "..." to the end of the text. That will probably look better to users.
If you want to cut off the extra characters without adding the ellipsis, you'd have to use the technique Ed S. described
I suppose that I don't really understand your use case here. My first suggestion would be to simply dynamically size your TextBlock. If that's not possible then you wil have to get the width of the string and manipulate it yourself before you set it in the TextBlock (or use a fixed width font assuming that you can and you know the max length of the string).
If you need to measure the width of the string before it is displayed you can use the FormattedText class to do so.

GtkTextView can't wrap line

Hello I'm using Gtk on C, I need to have a GtkTextView in the middle of my window with many other widgets, I can't make the widget wrap lines. This is a very annoying behavior, anyone have any idea of what am I missing? This is the code I'm using to set it's properties:
gtk_text_view_set_left_margin(GTK_TEXT_VIEW(commentsTextView),20);
gtk_text_view_set_right_margin(GTK_TEXT_VIEW(commentsTextView),290);
gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(commentsTextView),GTK_WRAP_WORD);
gtk_text_view_set_pixels_inside_wrap(GTK_TEXT_VIEW(commentsTextView),0);
gtk_widget_set_size_request(commentsTextView,300,300);
Edit:
I solved this in a different way, still the problem remains unsolved :S
Did you put your text view into a GtkScrolledWindow?
You also need to make sure there are spaces in your text if you want the wrapping to occur on words. If there are no spaces in your string that is inserted into the text view gtk won't know where 1 word ends and the other begins.
If you want the text to wrap on a string without spaces you could use GTK_WRAP_CHAR in place of GTK_WRAP_WORD.

Resizing Labels

I have a chart in WPF with a lot of labels. The text on these labels is dynamically loaded and subject to change. If I set the width just to auto, then these labels may overlap, which makes the text unreadable.
The chart support multiple sizes, so if it gets larger, then the bars are re sized and there is more space for text. Now I want to adjust the text to the space which is available. If it gets too small, I don't want to display the label anymore (a tooltip is available, so the user still gets the required information). Consider the string "Case 1, blah blah", there is probably not enough space to display the whole string, but just the first word. In this case I want the string to be "Case 1..", with .. indicating that there is some more information in the tooltip.
I can determine the length available for the string. But how can I determine the space a single letter will take? Of course I could also just re size the label, but then it would just cut off the string anywhere which is probably not helpful for the user (and looks ugly).
Any ideas?
If you can use TextBlocks instead of labels then they have a TextTrimming property which will do this for you to either the nearest character or the nearest word.
While you seem happy with the TextTrimming property, I'll edit this to add that the TextBox control has a GetRectFromCharacterIndex method that would allow you to find out the size on screen of one or more characters as long as the font settings matched your label. This might be useful if you wanted to trim at specific places in the label rather than the nearest character / word.
Not an expert in WPF, but I would think that you'll need to do this in code rather than XAML.
Start by obtaining the actual pixel width of the space available for the text.
Then look at the character set, dot pitch etc. utilised on the XAML front end and from there calculate the pixel width required per character.
You could also look at changing the character sizes as well as reducing the label length.

Resources