How i can read the content of a Windows 8 WinRT TextBox line for line in C#? I found nowhere a method for this? It's not working like in .Net
var lines = textboxName.Text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
would give you lines from a textbox explicitly split according to the content of the box. (Removing empty entries accounts for the fact that Environment.NewLine is actually two characters long.)
If you want to get a string split by the UI in a multiline textbox (i.e. where wrapping occurs) you'll have to go into more detail with measuring strings, etc, but I wouldn't recommend it unless you absolutely have to have the strings as laid out by the UI
Related
OK. Simple as possible: I want to inject a RichTextFormat comment (e.g. '{\*\atnid 1}' into the RichTextBox.Rtf;. That command doesn't show up in the richtextbox display area, which is the way I want it. The trouble is, there's no way I can see to put that comment in using C# and .NET. Adding it as a string adds an extra backslash each time, since the backslash char is the escape char, and the RTB displays it.
How to get'{\*\atnid 1}' into the Rtf buffer without additional backslashes being added by .NET?
The other day I made a small program in C# to list things (dynamically via code) in a richtextbox (I could have used a listbox, but I didn't) but everything was on the same line.
Now at time of writing I had placed some text in a button (via the properties panel, and again, this is in C#), and I wanted part of that text to be on the next line (the button being big enough to support two lines)
So it got me wondering: How can you put line breaks in the common text supporting items, both in code and outside of it (via properties window)? By common I mean:
Rich Text boxes
Labels
Buttons
"\n" is the escaped character for a line break. So code like:
label.Text = "This is a \nbutton";
Should put the word button on a new line.
Edit:
If you want to do it using the properties window in designer, click on the arrow on the far right of the text property field and it will open a small box. If you type multiple lines on that as you would normally (ie actually pressing enter, not using \n) then the component will treat them as new lines and put the new lines in for you.
Pharap is correct, but if you want to be a little more precise with code use Environment.NewLine. This will match to the newline character based on what platform the code is running on. But if you are lazy "\n" will work 99% percent of the time.
For input into the properties window, there is a small arrow next to the property. Click that and you will get a multi-line text box to enter stuff.
The newline(\n) or with verbatim like this:
label1.Text = #"some very
very
very
long text";
With buttons for example you set the AutoSize property to true:
button1.AutoSize = true;
button1.Text = #"some very
very
very
long text";
Is there a way to prevent the MessageBox from breaking a string instead of just growing to the width of the string? I'm porting an old VB application and they use message boxes to present a sizable amount of data to the user. I suppose I could create a form, but I've already started down this road, and would not like to have to go back.
Thanks.
The only (supported) way to have such control over how things get laid out here is to ditch the MessageBox helper class and build a custom Form class that does what you need.
We have dealt with this issue in the past be embedding newlines (\r\n) in the text of our message. MessageBox will grow vertically to honor the wrapping text. We used to have MessageBoxs so wide you couldn't even see the centered buttons, but now they show up fine.
Depending on the complexity of generating the message text and/or whether you have control over that, this may be your simplest solution. Otherwise, I think you will need to create your own form.
For example:
MessageBox.Show("Line1\r\nLine2\r\nLine3\r\nLine4", "MessageBox test");
MessageBox.Show("Line1 Line2 Line3 Line4", "MessageBox test");
The first line creates a MessageBox with 4 lines of text and the window has grown to the correct height to show all. The second line creates a MessageBox with a single line and the appropriate width to show the whole line.
I need to create a usercontrol "Console".
I was faced with such problems:
If I use a TextBox, how do I prevent removal of an already recruited command?
If I use a ListBox/ListView, how do I select all the text?
Please tell me what to do from the Console.
The console should be able to complete the command (by pressing Tab), allow selection of text, and prevent the entry of already established commands.
Here is a start:
http://ansiconsole.codeplex.com
I used a bitmap, and render text to it. This way I have complete control over the input and output.
If you need some "simple" console application: insert commands, I presume in some DSL language, view result of execution, and other stuff, you can try to programm on RichTextBox base, which can give also some styling to content.
Reuse some already ready (complicated) editors, like for example:
Scintilla
And work to limit possibilities of that kind of component to fit your needs.
Regards.
You could consider deriving from the RichTextBox control, as Tigran suggested.
Depending on what you want the user to be able to do, you will have to put some logic in there that restricts what they can and cannot select. (For example, if you don't want them selecting previous commands). You can obtain the text that they've selected via the SelectedText property. And then put in your custom logic, for example, Ctrl+C will copy the text into a variable.
You may consider having a MaximumSize property so that old commands will be erased after the console becomes so large.
Winforms already has a type of Autocomplete that you could use, or simply keep a list of keywords and when the user presses TAB, fill in the first word in your list that starts with what they've already typed.
To obtain the command itself, and not any of the previous text that was entered, you will probably want to take everything from the LAST newline to the end.
The code may look something like this:
String allText = this.richTextBox1.Text; // All the text from the rich text box
Int32 lastIndex = allText.LastIndexOf("\n"); // Find the position of the last newline
String command = allText.Substring(lastIndex + 1); // Substring starting at the character after the last newline
And of course when the user presses RETURN, the command will be sent to your code and executed.
How do i add a TAB (\t) to a string resource ?
"\tText" doesn't work
You have to explicitly add the tab in. The easiest way of doing this is probably to type out your string in notepad (with the tab explicitly set in place rather then using an escape character) and copy and paste the text into the resource editor.
You will have a similar problem with newlines, the easiest way of adding them in is to - again - add newlines in explicitly by using the shift-enter key combination.
You have two options that I am aware of:
Do a string replace after reading your resource string: s = s.Replace("\\t","\t");
Enter the escape sequence directly into your resource string at creation time by typing Alt-012 (I think that's tab) on the numeric keypad.
Articles on the same here and here.
Use the Alt Code for Tab (Alt + 009)
Newlines are added using Shift + Return.
1) Open up resources file in VS.
2) Put cursor where you want the Tab character
3) Hold down Alt key
4) Press 0, 0, 9 on the numeric keypad.
5) Let go alt key.
When you click off the resource string, you will see the tabs get removed from the display, rest assured they are still there. This can be verified by opening the Resources.Designer.cs and looking at the comment for the resource string and highlighting the area where the tab was inserted.
It's nearly six years since this thread was last modified, and the recommendation to use escapes still rules the day. For what it's worth, earlier today, I copied some text from a C# string constant into the resource string editor, and the tab got replaced by spaces. However, since the code expected to see the actual tab character, it threw an InvalidOperationException (my code, my exception!). Once again, I fell back to the tab, following the excellent instructions in the DevX article, "Another Way to Escape Sequences in .NET Resource Files," mentioned in the second citation in the accepted answer.
Moral: Don't count on the Windows Clipboard to faithfully copy your text.
Have you tried the XML tab character?
Sorry my tab character didn't show! Must have got eaten up by the browser.
\t does add an ascii tab but if you are displaying this in an html page you will not see that tab except in the page source. HTML doesn't render tabs or new-lines as non-breaking space. They all get reduced to 1 space character when displayed. Formatting HTML with whitespace is not recommended, that is what div with CSS or even Table are for. If you must add extra white space in HTML use the repeatedly but it will not be tab stop correct and will create a nightmare if you ever copy and paste.
Alternately you can display your string data in a read-only Text Area. This will preserve your string format. Without knowing the specifics of what you are trying to do with your string or how you are creating it these are the best suggestions I can give you.
You can also create a variable but the \t works inline.
string TAB = char.ConvertFromUtf32(9).ToString();