C# ToolStrip label sometimes printing last character first - c#

I have this line of code that doesn't seem to print correctly:
toolStripStatusLabel1.Text = String.Format("Done ({0} results)", _count);
And it prints out like so:
It also prints ".Done" if no String.Format is applied:
toolStripStatusLabel1.Text = "Done.";
But it prints fine if just given "Done" (it doesn't print 'eDon'):
But the String.Format seems to run fine (the relevant code is in the background)
What's up with that?

This happens when you set the control's RightToLeft property to True. That's a property that was intended to render text properly for users that speak Hebrew or Arabic, languages whose script is written right-to-left. Problem is, you are not displaying any Hebrew or Arabic, "Done" is English. Those languages render foreign words left-to-right. But not parentheses.
You'll have to set RightToLeft back to False and achieve layout another way.

Related

Label displays punctuation incorrectly with RightToLeft

I'm trying to learn C# and .NET by creating a calculator app. However, I'm seeing some weird behavior with WinForms label and punctuation. My app has a series of number buttons, a "period" button for decimals, and various operators. When you press a button, I add the value to the label that is displaying the value:
displayLbl.Text += selectedButton.Text;
or
displayLbl.Text += ".";
The label has RightToLeft set to "true" to mimic the display of a typical calculator.
However, when a period first appears in the label, it appears ahead of the rest of the numbers that were added before it. For example, it will look like ".456" even though the "456" was added earlier. As soon as you add another number, the period will then appear back in its right place like "456.7".
This also happens with the negative sign (-). If you add "-478" to the label, it will appear as "478-".
This seems really buggy. Is there any way to fix this?
I set RightToLeft to "No" and then did the following and it worked beautifully:
this.displayLbl.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

Label.Content Not Working Properly

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

Adding "new line"s to text in text supporting objects (i.e Buttons, Rich Text boxs) both in and out of code

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";

SpellCheck Functionality in Textbox

I have an issue in Spellchecking the text that is entered in a textbox in wpf 3.5
<TextBox Name="tbxBack" SpellCheck.IsEnabled="True"/>
It will Check fine for the wrongly spell words in the sentence that I have entered in the text box, but when I right click on the wrongly spell word and try to make correct, it is only making the first word correct in the sentence.
for example:
the sentence is:
tey are plaing in playgroun (They are playing in playground)
when I right click on "tey", system is giving the appropriate results and setting the correct value ("They" for "tey"),
but when I right click on "plaing", system gives the correct values, but does not set it to correctvalue ("playing").
same for "playgroun" (doesn't set to playground), System is checking only the first word.
How to make it work for the whole sentence?

RichTextBox invisible selectable character at end of string

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
}

Categories

Resources