 |
|
 |
What license is the code released under? Is it the BSD? May I have copy of the license? Thanks.
|
|
|
|
 |
|
 |
I would really like to get this control with min and max but I have a suggestion when doing so. The following properties would be great.
bool AllowNegative - Needed if min and max is turned off then the user still be regulated. (If AllowMinMax and allow negative then set min to be -1 if min is positive.)
bool AllowMinMax - Turns on min and max validation.
double Min - minimum value (If min is negative then set AllowNegative to true otherwise false)
double Max - maximum value
Chris
|
|
|
|
 |
|
 |
Thanks Chris, I have one small finished app using this control where I have plans to add AllowNegative type functionality. Thanks for the suggestions and I will use these when I make to updates. Unfortunately I can't guarantee when that might happen, so feel free to have a go. Let me know how it turns out!
Oscar
|
|
|
|
 |
|
 |
Hi there,
Been looking for a while for a control that supports data binding under the .Net framework 2.0. Does this control allow this? If so, how does it deal with nulls?
Thanks in advance
|
|
|
|
 |
|
 |
Great article! I have downloaded your code and enhanced it. Adding date and time values as well.
If you send me your email I will provide you with the code.
Thanks,
Jay
ljscottiii@yahoo.com
|
|
|
|
 |
|
 |
Hi,
did you test overriding the WndProc and catching the "WM_CONTEXTMENU = 0x007B" message. If you not do so, you can disable the context menu and you can want you want.
protected override void WndProc(ref System.Windows.Forms.Message msg)
{
if(msg.Msg == WM_CONTEXTMENU )
{
//Yippi disabled
}
else
base.WndProc( ref msg );
May this help you. Currently I'm doing the same, programming a doubleTextbox and have the same problem like you had, e.g. using a sign or "0" inputs.
Nice work, maybe i use now your work.
Robert.
|
|
|
|
 |
|
 |
Hi Oscar!
you did a good work, thanks, thats almost i need for a project. have you changed your control in the past without posting the changes in here? does the min/max-function now works without bugs?
greets, Tom
|
|
|
|
 |
|
 |
this could be because i use with my keyboard setup to danish, where the comma is used for seperating in floats.
Its works nicely otherwise
|
|
|
|
 |
|
 |
I see that you catch Pastes of data into the field with Ctrl-V and Shift-Ins, but I can still paste bad data into the field by right-clicking on the textbox and selecting Paste.
|
|
|
|
 |
|
 |
Hi
I found a bug while testing your demo app. All the numeric types except the integers allow multiple decimal separators to be entered. I haven't looked at the code yet, but it seems odd that the currency data type allows only 3 decimal separators and all the rest allow unlimited decimal separators to be entered. Also, the currency type doesn't allow any numbers to be entered between or at the end of the "bad" decimal separators although the others do.
This could happen because you don't use the CultureInfo. For example in my country we use "." as group of thousands separator and "," as decimal separator.
Great work though.
As for enhancements, you could add a Date validation to the edit control so the control could be complete and make use of CultureInfo for the decimal separator and negative values. Using culture info can also enable the control to use currency tokens like the $ or € sign. It's a little bit tricky on the currencies that have multiple letters like my country's: 'lei' and doesn't have a symbol.
|
|
|
|
 |
|
 |
Thanks, good find.
I didn't spend alot of time with internationalization issues. I took a look at the Globalization class and there are some interesting features regarding number formatting that will improve the NumEdit control.
I ran a quick test and found that the decimal.Parse() allows "1,,,," and returns 1 (US Culture). This looks very strange and not what I wanted.
My primary design plan was for a simple number entry control that would prevent invalid numbers. I will be making changes to handle international numbers better.
Thanks,
Oscar Bowyer
|
|
|
|
 |
|
 |
I found a case that throws an unhandled exception.
I set up an edit field with a type of decimal. Then I ran my application and changed the initial value from "80.00" to "80-" (I was testing for any sort of insane things a DFU might try to enter). Oddly enough, the decimal.Parse() call in in the IsValid() function accepts it just fine. But, in the OnLeave() the Double.Parse() call chokes on it.
I am not quite sure how to address this yet. I could comment out the else clause in the OnLeave() but the real problem is that IsValid() doesn't catch the problem. Maybe the above is supposed to be an acceptable value for decimal, but that seems unlikely. Might wind up reporting it as a .NET framework bug in Decimal.Parse().
|
|
|
|
 |
|
 |
Good find!
I can't explain why decimal parses 80- when no other numeric datatype will. Currency in the NumEdit is decimal behind the scenes so it suffers from the same problem.
I would be curious if you decide to report this as bug, what M$'s response is. If "by design" then who on earth enters decimals with a following negation and not any other data type?
As far as a work around, I would check for following "-" in the IsValid routine to stop it from being entered. I will post a new rev as soon as I can.
FYI: the double.parse() in OnLeave() is used to avoid another switch on the input type since Double should hold any other numeric type.
Thanks,
Oscar
|
|
|
|
 |
|
 |
As it sits, a value such as -.255 could not be entered. I know how to make it allow "-." in the IsValid function. My concern then is that you could leave the box with the value "-" or "-." in it. I would recommend overriding OnLeave or OnValidate or something and do a final check on whether it is actually an entry of your desired data type.
Great article. It gave me some great ideas. I implemented something very similar using regular expressions, and it has the same "-." issue.
Thanks again.
Paul
|
|
|
|
 |
|
 |
Although the context menu has been diabled by replacing the one having no items. User can also put in invalid data by pasting text using "Shift + Insert".
DoanDu
|
|
|
|
 |
|
 |
Just replace the line
if(keyData == (Keys)Shortcut.CtrlV)
with
if(keyData == (Keys)Shortcut.CtrlV || keyData == (Keys)Shortcut.ShiftIns)
That should fix it for you.
Happy Coding...
|
|
|
|
 |
|
 |
case NumEditType.Currency:
decimal.Parse(val);
int pos = val.IndexOf(".");
if(pos != -1)
ret = val.Substring(pos).Length <= 3; // 2 decimals + "."
break;
the logic must use information from regional settings
string CurDecSep;
string CurGrSep;
NumberFormatInfo cf=CultureInfo.CurrentCulture.NumberFormat;
CurDecSep = cf.CurrencyDecimalSeparator;
CurGrSep = cf.CurrencyGroupSeparator;
|
|
|
|
 |
|
 |
You cannot just enter a single 0 in NumEdit.
|
|
|
|
 |
|
 |
Thanks for catching this! Should have been obvious huh?
I'm working on a couple of other improvements including min/max properties, invalid backcolor blink, and curreny format. I'll include this with this revision, coming soon.
If you want a quick fix the change the following line in IsValid() from:
if(val.Equals("-"))
to
if(val.Equals("-") || val.Equals("0"))
Thanks,
Oscar Bowyer
|
|
|
|
 |
|
 |
I wrote a control very similar to yours and I overrode the WndProc method and looked for the paste message from Windows. I then called my own paste method that stripped any non-numeric characters.
const WM_PASTE = 0x0302,
protected override void WndProc(ref System.Windows.Forms.Message m)
{
switch(m.Msg)
{
case WM_PASTE:
// Own paste code
break;
default:
base.WndProc(ref m);
break;
}
}
|
|
|
|
 |
|
 |
David,
Thanks for the help.
My plan was to work within the standard .NET framework without digging into the WndProc and raw Windows messages. It certainly appears that it is unavoidable in this case. The WndProc() override looks quite straight forward compared to the Win API methods for replacing the WndProc.
I'll be looking at ContextMenu's for other controls I'm using and add message handler code to make all my controls have a consistant ContextMenu.
Thanks,
Oscar Bowyer
|
|
|
|
 |
|
 |
you will also need to handle cut.
I have written a very similar control for a comercial project. I abstracted the validation to a base class and then overode that just for numerics.
I would also say that you should _NEVER_ use exceptions as flow control if there is an alternative.
my validator had 2 stop filters, a char based filter and a string filter, the char being the key entered by the user and the string being the proposed new string.
for simple non negitive numbers you can simply set the edit box style in the create event ,ES_NUMERIC, I think.
adam
|
|
|
|
 |
|
 |
I don't see why you will need to handle cut.
If you only override paste and don't worry about hiding the short-cut menu, cut will only ever cus what's in the text box - numbers.
|
|
|
|
 |
|
 |
I wrote a similiar control also. I implemented cut (and clear). If you use the control to allow entry of floating point numbers, and the user deletes the decimal point, the resulting number may be invalid for the selected InputType. The integral part of the number now includes the digits that were on the right side of the decimal point. The new number may overflow the current InputType.
I also added handling for the delete and backspace keys for the same reason. In these cases, if I couldn't delete the period for the reason mentioned above, I shifted the caret position accordingly.
- Marc
|
|
|
|
 |
|
 |
Oscar,
Your current method seems to deviate to far from standard textbox functionality. I tried to quickly duplicate the functionality using only OnTextChanged() override. I quickly realized that my caret control was very weak. So I coded a few more overrides to handle the selection positioning. I believe my solution is a little more robust (of course only time will tell). It also has an advantage of completely isolating the IsValid() function which could allow for future functionality via override.
On the other hand, when a user enters an incorrect value, my method may flicker the incorrect text for a split second before it resets - yours neatly drops the key so nothing is shown.
I dropped the following methods/properties from your code:
Text
ProcessCmdKey
OnKeyPress
I added the following code:
private string m_LastText = null;
private int m_LastSelectionStart = 0;
private int m_LastSelectionLength = 0;
private bool m_InResetMode = false;
private void SavePosition()
{
m_LastSelectionStart = this.SelectionStart;
m_LastSelectionLength = this.SelectionLength;
}
private void ResetPosition()
{
this.SelectionStart = m_LastSelectionStart;
this.SelectionLength = m_LastSelectionLength;
}
protected override void OnTextChanged(System.EventArgs e)
{
if (!m_InResetMode)
{
string tmp = this.Text;
if (IsValid(tmp))
{
m_LastText = tmp;
base.OnTextChanged(e);
}
else
{
m_InResetMode = true;
this.Text = m_LastText;
ResetPosition();
m_InResetMode = false;
}
}
}
protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs mevent)
{
base.OnMouseUp(mevent);
SavePosition();
}
protected override void OnGotFocus(System.EventArgs e)
{
base.OnGotFocus(e);
SavePosition();
}
protected override void OnKeyUp(System.Windows.Forms.KeyEventArgs e)
{
base.OnKeyUp(e);
SavePosition();
}
Let me know what you think. I have been handling validation the hard way for too long. It is time to come up with a more solid solution.
Mike
|
|
|
|
 |