Processing control characters in a Winforms TextBox - winforms

I am using this sequence: Alt+27 (using the numeric keypad) in order to enter the 'Esc' character in a Winforms TextBox. Then I attempt to read back the textbox string using the Text property. I was expecting the first character to be 'Esc' (integer value 27) but instead I found that the integer value of 8592. What is going on? How can I read back correctly the 'Esc' character?

Alt-27 is a shortcut for ← which is indeed Unicode character 8592 - see http://www.alt-codes.net/ for the full list.

Related

Text is overwritten while writing in DataGridViewCell

I'm working on a Winform application. I have a DataGridView. When I write the first character in Cell, cursor automatically focuses on the first character. If I write a second character, it overwrites the first character.
Thus if I need to write more characters, every time I have to press Right Arrow key to move. Any ideas?

Manual word-wrap in RichTextBox

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.

silverlight string format to limit the character count

I am using the SubString method to limit the character count. Is this possible to do in XAML? If so, how do you set the format string to limit the character count in XAML as in String.Format
For example:
My Big text Message -> My Big text....
Using the TextTrimming property of TextBlock in Silverlight 4 should work:
<TextBlock TextTrimming="WordEllipsis"/>
What's nice about this is that it will use actual text measurements instead of arbitrary character counts, since obviously not all characters are the same width.

How to change Tab character width in silverlight TextBox

The width of Tab (\t) character in silverlight TextBox is not equal to 4 spaces or 8 spaces. It's too short.
Is it possible to change the width of the TAB (\t) character displayed in a silverlight TextBox?
Note that I want to avoid replacing TABs with spaces.
Any ideas on how to do this?
Silverlight does not allow you to change the tab character length in a TextBox.
If your reading in a string (from a file or something) and setting the Text to it then if you look at your Text Property you'll see the escaped tab (\t). Searching for a \t is easy
TabTextBox.Text = TabTextBox.Text.Replace("\t", " ");
So this will replace all tabs with 4 spaces.
Besides pressing tab in a TextBox will not tab the text. It will focus to the next UIElement within the parent UIElement.
Or maybe on tab key press event, append the string literal to the text box. Similar to what MyK is suggesting.
If you're trying to get this to display, write a converter. the syntax is around--just grab an example, gut it, rename to something like 'tabstoptexttospacedtextconverter', add a reference to your local controls in app.xaml, then create an instance of it and give it an x:name to use it. Bind the data for your text box and assign it your new converter.
It'll be a little bit of hassle because you'll have to determine the appropriate width of the final display TB, and then probably use a converterparameter to make that work. But long story short, split on \t, then foreach(string str in splitSourceText) do something like this:
for (int i = 0; i < (str.Length % 8 > 0 ? str.Length % 8 : 8); i++) str+= " ";
You can limit the characters on the split string arrays with the parameter or split on \r\n first.

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.

Resources