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;
Related
I'm trying to make a simple calculator, and it has to focus on the last number so that when the text is too long, the user can still see what he/she is typing. Also, when the caret is not at the end of the string (arrow keys used), pressing a number button will put it back to the end of the string.
How do you do this?
Set the CaretIndex to the end of input like this when text input is received.
TextBoxName.CaretIndex = TextBoxName.Text.Length - 1;
If you’re trying to create a calculator like that in Windows10, then the textbox should be read only. You can set the TextAlignment to Right to display the text from right as in Windows calculator.
Since the textbox is readonly, when user clicks on some number that string needs to be concatenated to the textbox. The logic is very simple.
You can google for wpf calculator and find some good examples.
If you’re having a 2 textbox and 1 result textbox type calculator, then you can follow the method I mentioned above to achieve the expected behavior.
I have a Textedit Control that I want to behave in a certain way:
When the Control has input focus, I want to only allow the input of positive whole Numbers (not zero). I achieve that by using Properties.Mask, which works fine.
When the Control does NOT have input focus, I want it to Display the entered number but with ",00" at the end.
So basically, while I enter something, I only see what I enter e.g. "17" but when the Control loses focus, I want it to show "17,00". So that I am only allowed to enter whole positive numbers but the Control will always add ",00" afterwards.
My understanding is that there are basically two different "modes": DisplayMode and EditMode.
EditMode = The Control has focus.
DisplayMode = The Control does not have focus.
In EditMode, I can type things into my Textedit Control. What I can and can not enter is determined by the Mask.
When I lose focus, it goes into DisplayMode. Here I cant type anything into the TextEdit but now the displayed text is not determined by the mask any more but by the Property "Properties.DisplayFormat". So to achieve my goal, I tried to set the DisplayFormat.FormatString to "0.00", so that it would always show two decimal places "x,00".
Somehow, this doesn't work as expected. The DisplayFormat doesn't seem to do anything and even in DisplayMode, the TextEdit still shows just the whole number without the decimal places.
I realize that I could use events to work around this problem but I think that's what DisplayFormat, EditFormat and Mask are for and I really don't want to handle multiple events for something that small.
Accodring to DevExpress Knowledge Base topic DisplayFormat is not working in unbound mode.
Problems with formatting occurs because an unbound text editor stores
a value as a string, therefore formatting cannot be applied.
If you use XtraEditors 3 or higher, you may wish to set the editor's
Mask.MaskType property to Numeric. In this case, the editor is forced
to handle the edit value as a number and, therefore, it can format it.
If you wish not to use the Numeric (or DateTime) mask, please use the
ParseEditValue event to convert a string to a number.
I can suggest you to use Numeric mask with n0 as edit mask:
To accomplish the above is fairly simple:
To only allow positive whole numbers, you need to set the MaskType to Numeric and use the EditMask ##########;. The number of # represents the possible number of didgets so ten times # means you can use a ten-digit number. (see nempoBu4's answer)
To show an additional ,00 when the control loses the focus, you simply need to set the DisplayFormat as FormatType = Numeric and FormatString = n2.
(I cannot believe this has never been asked; most likely I failed at searching.)
Having a single line WinForms TextBox control with content in it:
When the user uses Ctrl+→ or Ctrl+←, the caret moves to the very end respectively very beginning of the content of the text box.
My goal
I want to configure the text box so that navigating with the above keys (or in addition with Shift), the caret stops at word boundaries.
E.g. when the caret is at position 0 and the user presses Ctrl+Shift+→ it would look like this:
I.e. it selects the first "word" only.
The standard behaviour is:
I.e. it selects the whole text.
Interestingly enough, the native Windows controls like e.g. in the "Run" dialog behave as described:
The address bar of Edge or Internet Explorer or even Windows Explorer also stops at word boundaries.
My question
Is it possible (maybe through P/Invoke) to configure certain stop characters for the TextBox control?
Update 1
I could think of handling keyboard events and doing this manually.
To answer your question.. definitely, but I wouldn't know how.
But
There is a control that has the behavior you're looking for, namely the RichTextBox.
It has the behavior you are asking for and has a property for disabling the default multiline behavior.
Properties to make the RichTextbox look like a regular textbox
Multiline - false
DetectUrls - false
height - 20
Note:
The richTextBox does NOT have autocomplete out of the box, like the regular textbox does.
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.
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?