I am attempting to assign the following string to a label created in XAML from the C# code-behind file: "Recall_AUX_002"
To do this, I am using the following:
lblRecall.Content = currentAddress.Recall;
When I run the program, the first underscore magically disappears and the result becomes "RecallAUX_002." If I try to assign the same variable to a random text box in the form, with the following code, it works fine:
txtGivenName.Content = currentAddress.Recall;
Why is the underscore randomly being removed in labels?
This is because Windows thinks that the first "_" character is an "Access Key". That is, its the key that if you hold down Alt it is the character that is underlined.
You can escape it by writing two underscores:
<Label Content="Recall__AUX_002"/>
or by turning the RecognizeAccessKey property to false. This is in the content presenter, so you have to modify the ControlTemplate
You can also always just switch to using a TextBlock, which ignores access keys.
Underscore are considered as access keys. It has to be disabled if you like to display it in the content.
Refer the below link
Underscores not displayed in WPF
Related
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";
I'm making a typing windows program that has two text Boxes,
the first is the Source Text textBox and it is read only,
the other is where the user get to type in the text that is in the source TextBox.
When the user typing a letter in the TypingTextBox, I want that letter to be highlighted in the SourceTextBox..
I tried doing this in a couple of events, but none really worked:
SourceTextBox.Select(TypingTextBox.SelectionStart , 1);
I even Tried Making my own event, Also didn't work.
The thing is, I won't see the SourceTextBox highlighting unless I click on it.
and As I mentioned, I tried putting the above code in events like:
Mouse-Focus-Leave in the SourceTextBox
and: TextChanged in the TypingTextBox.
All didn't work .. :(
and If I manged to do that, Can I change the Highlight color ?
Assuming this is WinForm, you need to set the HideSelection property on the TextBox to "False". As far as changing the highlight colour, none that I'm aware of.
When selecting text in a RichTextBox using the mouse, or arrow keys + shift, I can select an extra blank character at the end of the string. To reproduce:
type a few characters (or nothing at all) in a RichTextBox
set the cursor to the end of the string
hold shift and press the right arrow key
You'll see a narrow highlighted selection appear, which cannot be deleted.
This causes a problem in my application because the SelectionFont property returns null when the extra character is selected along with some valid text. Any ideas on how to disable this extra character, or work around it otherwise?
c# winforms, visual studio 2010
Interesting. (This is not Dr House, MD, speaking.)
When nothing is selected in an empty RTF edit control, SelectedRtf returns:
"{\rtf1\ansi\ansicpg1252\deff0\deflang2055\uc1 }"
When the "phantom" stuff is selected in the empty RTF edit control, SelectedRtf returns:
"{\rtf1\ansi\ansicpg1252\deff0\deflang2055{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\f0\fs17\par
}"
Of course, SelectedText returns the empty string in both cases.
So I suspect what is selected is the metainfo that makes sure that newly entered text that would replace the empty selection will be inserted with the correct font.
However, that seems to be nonsense since newly entered text will be inserted with the correct font even if nothing is selected.
So all that does not make sense. (Well, it does, kind of -- see last paragraph)
Which makes me believe this is a bug, or at least a glitch, in RichTextBox.
The formatting info is probably created by the selection routine which makes sure that selected non-empty text will be replaced by the newly typed text formatted in the same format as the text it replaces. For this to work, a selection must always contain the formatting info, even if no text is selected. I think. Maybe one can pre-select a char and paragraph formatting different from the default one, somehow, and then typing text uses that format.
FIred up a new winforms app in studio 2010 and wasn't really able to reproduce this. I can select the invisible 'character' but it doesn't cause me any problems. SelectedFont still returns a valid object for me. I can get the text without issue.
The SelectionFont property for the RichTextBox control can ONLY return a single font. If the selected range contains more than one font, reading the SelectionFont property will throw a NullReferenceException.
Most likely what is happening is that the RichTextBox Font property is different than the current font you are using on the selected range. That "extra" character selected at the end is your Font property, not your SelectionFont property.
If your RichTextBox is just using a single font, just make sure the Font and SelectionFont property are the same.
Otherwise, just check if it's null:
if (richTextBox1.SelectionFont != null) {
//do something
}
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();