I have several groupBoxes-controls with a NumericUpDown-control in each on of them. The NumericUpDowns have a small modification - they can also decrement in the negative range of decimal. Here is the code:
//Add KeyDown event to the numericUpDown control
numericUpDownGBPC12_angleRange.KeyDown += new KeyEventHandler(numericUpDownNegative);
The code of the function numericUpDownNegative is as follows:
void numericUpDownNegative(object sender, KeyEventArgs e)
{
NumericUpDown temp = (NumericUpDown)sender;
temp.Value -= temp.Increment;
sender = (object)temp;
NumericUpDown temp = (NumericUpDown)sender;
}
Suggestions for improving the code above are most welcome however I'm more interested if it is possible to enable negative input in a NumericUpDown. The above code works but when I try to put a negative number I get something weird. This behaviour does not apply for a non-modified NumericUpDown.
Example:
Let's say numericUpDownGBPC12_angleRange has a minimum of -70.0000000000 and a maximum of 70.0000000000, which I have set by the Minimum/Maximum property parameters of the control. The starting value of the control is 0.0000000000. If I push the Down-button, I get accordingly -0.0000000001, -0.0000000002, -0.0000000003 etc. until I reach -70.0000000000. However if I decide to type -x.xxxxxxxxxx (let's say -24.2398324119) I get x-0.0000000000 (4-0.0000000000). So not only I cannot enter the full number 24 (it seems the NumericUpDown takes the last typed digit in this case, which is 4), but I get the whole part after the point completely annihilated unless it was set by using the case in which case the problem is only with the part before the point. So only the first digit (on the most left of the number) can be changed. :-/
I was thinking of using textBox-controls however the amount of number fields I have as part of the interface will create a huge overhead because of the parsing of each and every textBox (we all know that sadly many users love to experiment with things that where never intended to be experimented with ;)) to make sure a certain number is entered. Despite the negative-thingy the NumericUpDown has really nice feature such as - only a digit can be entered and you can also specify the precision, the range of values etc.
So again the question is - is it possible for a NumericUpDown to accept negative input by the user?
Problem was in the KeyDown-event (had to remove it completely) and also in the format I was trying to input as a number. I have the ',' seperator and not the '.' in my Visual Studio (due to localization). So typing '.' made the NumericUpDown go berserk.
Related
I'm currently working on a form with a bunch of textboxes with quite specific requirements. For example one textbox contain cadastral number and should look like ##:##:#######:~ (last range of digits varies) and also it would be quite nice to see the pattern before you even type anything (if I recall correctly it's called mask). Also giving requirements first two digits always should be 24, so the end result should look shomething like this 24:##:#######:~. Another example is a numeric textbox with units and spaces between big digit numbers (ex 1 000 000 m2). For short this one text box and the others have both static elements (which user should not be able to edit) and dynamic (which makes masked textboxes and similar stuff quite hard to deal with).
So, I've tried different things:
Using maskedTextBox from toolkit package, which turned out bad, because it did poorly handle last part with range of digits, and also when a key was pressed when in the middle of the static mask, it just pushed the carret, but not actually add anything to the text;
Using converters prove quite chalenging at first, but gave remarcable results, it handles variable range of part perfectly, partialy, because of custom converter, but it was difficult to manage things, when user deleted text, because of static parts being integrated in the converter itself;
Using StingFormat for textbox with binding text property was almost useless, although it handle static part quite well, and in the end I couldn't make it work.
Intuition tells me a combination of a custom converter (handling dynamic part) and and StringFormat (handling uneditable static part) should do the job. Maybe the number of requirements is just too much for a simple textbox. Also one thing is bugging me, there could be a sutable existing general solution, that I am not aware of (At first I didn't know Converters was a thing).
Now the question, how would you generally approach this problem? Is there any existing solutions, that with a bit of tweaking would work?
I am creating a Calculator in Windows Store Application. I have successfully created the app in the store.
Now there is a problem in my app, after getting the result from performing any operation whenever I press on any numeric value, that value got append in the existing value.
In the following snapshot: I have added two numbers (1,1):
Now I am entering another value to perform some other option, but the new value got append in the existing value. I am entering 1 here:
What is the code for removing the existing value, if any numeric values pressed?
you could declare a bool value which is false and when you have your calculation done you switch it to true. Then you write a method that checks if the calculation is done or not and if it's done you simply clear the (i guess you use a textblock / box?) output. That would be my way in this situation - maybe there is a better solution for you. I hope it helps you to get a clearer way in mind.
As the author of the Windows Calculator that shipped from Windows 3.0 through Windows Vista, I agree with user3645029's response. You need to work out the input model for the app, so you understand clearly when you begin entering a new number and when you append to the one showing. I suspect that your app logic isn't making this distinction.
Let me be more specific:
If the key pressed is a number and the last key pressed was a number, then you add that new digit, which effectively means multiplying the current value by 10 and then adding the new key.
If the key pressed is a number and the last key pressed was an operator, =, or similar keys, then you're starting a new number input and your current value should be reset to 0 first.
In short, writing a calculator app requires an internal state machines that understands how to proceed from one input to the next. From what you describe, it sounds like you're missing the logic for the = key. Generally speaking, hand-held calculators with an = sign effectively clear the current value if you start entering a new number after =. Only if you press an operator does that value persist, and in that case you're also starting a new current value and keeping the "2" in your case as the first operand.
I need to use basic functionality of the MaskedTextBox. I can get use of the 5 digit mask but there are few things that I want to change. Right now the box is looking like this:
and there are two thing I don't like. First - the Prompt char which is undersoce _. I deleted the field value in order to leave it empty (as I would like it to appear) but this gives an error - The property value is invalid. So is there a way to get rid of these underscores? And second - I use this value for one of my entity properties which is of integer type so I make a convertion :
if (txtNumOfAreas.Text != "")
{
string temp = txtNumOfAreas.Text;
bool result = Int32.TryParse(temp, out convertValue);
if (result)
{
entity.AreasCnt = convertValue;
}
else
{
MessageBox.Show(Resources.ERROR_SAVE, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
else
{
entity.AreasCnt = null;
}
which works fine unless someone decides to make experiments and insert something like _1__5_ then the conversion fails, but at first place I would like to make possible to write the digits only one after another. So is this possible too?
It looks like your MaskedEdit is more trouble than it's worth to deal with your particular range of issues. A better control to use might be the NumericUpDown.
The upside to NumericUpDown:
There are no underscore prompts to try and get rid of
It will only accept numeric input
So there is no need to try and convert the value. You will always have an integer
Setting Minimum and Maximum value properties gives you automatic data entry validation
Provides multiple modes of data entry:
Typing
Clicking up/down buttons with a mouse
Up/down with keyboard
If you like, you could hide the up/down buttons altogether: C# WinForms numericUpDown control (removing the spin box)
So to get the _ to be a space you just need to set the mask character to a single space. And to resolve the conversion error, just set the AllowPromptAsInput property to false so that the user can't actually end up with something like your example.
In my WPF application the user is inputting a GPS coordinates in the format of Degrees : Minutes : Seconds as decimal fraction of minutes.
So 60° 30' 45" would be entered as 60° 30.750' .
I then store it as a pure decimal number so the above example would be stored as 60.5125.
The idea was that so the users wouldn't mess up the input it would be set in 3 different textboxes. One for Degrees, other for Minutes and one for the fractionalSeconds. It's somewhat bad that one of the numbers is seperated into two textboxes but if they have to type the whole number in the are afraid of all the point or comma confusion and that they could mess up.
So one thing I thought might work was a IMultiValueConverter but it seems to only work with MultiBindings which is something I'm not doing here.
Currently my solution is to bind to different properties and do all the calculations in code behind but I'm not really happy about the fractional bit. I assume 3 fractional letters but if they enter only 7 and assume 0.700 but get 0.007 so I thought I would have to do a string format bit.
Is there a better solution out there. Can I use MultiValueConverter for something like this?
You could try using a MaskedTextBox, such as the one from the Extended WPF Toolkit.
You could use a Masked TextBox. This implementation uses MaskedTextProvider which is a .net class.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
How to handle when the backspace button is pressed and the input panel has digit grouped numbers. E.g. 434,343,334.232.
Basically the question says it all, I'm making a calculator in c# and I'm stuck on this problem. When digit grouping is hit the numbers get separated by commas, when the user hits the backspace button, the commas act like characters that get deleted.
inputPanelTextBox.Text = inputPanelTextBox.Text.Replace(",", "") I put this piece of code under my backspace click event. The problem is that say the panel has 234,232 and the user presses the backspace button, the comma is gone as well as the 2 on the far right.
What I want to happen is the same way the windows 7 calculator does when the digit grouped menu is checked and the user hits the backspace.
Any idea of how to go about this? Thank you so much in advance.
How about you store the number just as a number then format the number to look nice when you output it. The same thing will work with string.Format(). You could reformat the number into the display string every time the user hits backspace or adds a new digit. There are lots of the string format modifiers listed here.
int numWithComma = 3000;
int numWithoutComma = 50;
Console.WriteLine("numWithComma: {0:n}", numWithComma);
Console.WriteLine("numWithoutComma: {0:n}", numWithoutComma);
// prints:
// numWithComma: 3,000.00
// numWithoutComma: 50.00
If I were making this calculator, I would save the number in a double, then when the number needs to be displayed I would run it through a function like this and display the string to the user:
private static string FormatForDisplay(double number)
{
return string.Format("{0:n}", number);
}
How about using a format string when you output the number, that way you don't have to manage commas at all.
double myDouble = 500000.012345
inputPanelTextBox.Text = myDouble.ToString("N", CultureInfo.InvariantCulture);
Its been a while since I've used Visual Studio but I'm pretty sure you can set these format strings directly on text boxes so its simply automatically handled when they type. And its easy as pie to set in the properties if I remember correctly. Check out Standard Numeric Format Strings for a bit more on it.
Also a MaskedTextBox might work as well, set the mask to something like "999,999,999,999.99" and see how that formats different numbers.
Good Luck! :D