Click here to Skip to main content
15,914,360 members
Home / Discussions / C#
   

C#

 
AnswerRe: Question about login form with access database Pin
OriginalGriff24-Mar-19 2:03
mveOriginalGriff24-Mar-19 2:03 
QuestionMessage Removed Pin
23-Mar-19 12:08
Member 1419354023-Mar-19 12:08 
QuestionCzytanie z drukarki Posnet / Receive Posnet POS printer answer Pin
OraToraCora21-Mar-19 13:24
OraToraCora21-Mar-19 13:24 
AnswerRe: Czytanie z drukarki Posnet / Receive Posnet POS printer answer Pin
Luc Pattyn21-Mar-19 15:37
sitebuilderLuc Pattyn21-Mar-19 15:37 
GeneralRe: Czytanie z drukarki Posnet / Receive Posnet POS printer answer Pin
glennPattonWork325-Mar-19 6:39
professionalglennPattonWork325-Mar-19 6:39 
QuestionDecimal values input validation Pin
Rap Gutierrez21-Mar-19 2:31
professionalRap Gutierrez21-Mar-19 2:31 
AnswerRe: Decimal values input validation Pin
OriginalGriff21-Mar-19 3:31
mveOriginalGriff21-Mar-19 3:31 
AnswerRe: Decimal values input validation Pin
Member 230317321-Mar-19 21:26
Member 230317321-Mar-19 21:26 
We use something like the code below for a WinForms TextBox control, and I imagine it could be adapted to anything with a KeyPress or similar event.

private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
    var control = sender as TextBox;
    if (control != null && !char.IsControl(e.KeyChar))
    {
        string testString = control.Text.Substring(0, control.SelectionStart) + e.KeyChar +
            control.Text.Substring(control.SelectionStart + control.SelectionLength);
        e.Handled = !ValueIsNumeric(testString, 5, 2, -100.00, 200.00, true, true);
    }
}

private bool ValueIsNumeric(string testString, int precision, int scale, decimal? minValue,
    decimal? maxValue, bool allowNegativeValues, bool allowNullValues)
{
    bool result = false;
    // Precision must be greater than or equal to zero.
    if (precision < 0)
    {
        precision = 0;
    }

    // Scale must be greater than or equal to zero.
    if (scale < 0)
    {
        scale = 0;
    }

    // Precision must be greater than scale
    if ((precision - scale) < 0)
    {
        precision = 0;
        scale = 0;
    }


    if (string.IsNullOrWhiteSpace(testString))
    {
        // If we allow null values, then return true
        if (allowNullValues)
        {
            result = true;
        }
    }
    else
    {
        // If we have a period in the string, validate the Scale of the number.
        bool checkDefinitionMinMax = true;
        int periodIndex = testString.IndexOf('.');
        if (periodIndex >= 0)
        {
            if (scale > 0)
            {
                // We have to add 1 to the period index to eliminate
                // the period character from our result string.
                if (testString.Substring(periodIndex + 1).Length > scale)
                {
                    // Remainder of string is greater than our specified scale,
                    // something like 12.345 would fail for Numeric(5,2).
                    checkDefinitionMinMax = false;
                }
            }
            else
            {
                // We do not support a number with a period.
                checkDefinitionMinMax = false;
            }
        }

        decimal value = 0;
        if (checkDefinitionMinMax && decimal.TryParse(testString, out value))
        {
            // Validate that this value is within the precision and scale specified.
            decimal maxDefinitionValue = decimal.Parse(string.Empty.PadLeft(precision, '9'));
            if (scale > 0)
            {
                string p = string.Empty.PadLeft(precision - scale, '9');
                string s = string.Empty.PadLeft(scale, '9');
                maxDefinitionValue = decimal.Parse(string.Format("{0}.{1}", p, s));
            }

            decimal minDefinitionValue = 0;
            if (allowNegativeValues)
            {
                minDefinitionValue = maxDefinitionValue * -1;
            }

            // Minimum/Maximum values may not be inclusive of the type definition's precision/scale values.
            // Example: days of year = 365, but type declaration is Numeric(3,0).
            if (value >= minDefinitionValue && value <= maxDefinitionValue)
            {
                if (minValue.HasValue | maxValue.HasValue)
                {
                    if (minValue.HasValue & maxValue.HasValue)
                    {
                        if (value >= minValue.Value && value <= maxValue.Value)
                        {
                            result = true;
                        }
                    }
                    else if (minValue.HasValue)
                    {
                        if (value >= minValue.Value)
                        {
                            result = true;
                        }
                    }
                    else if (maxValue.HasValue)
                    {
                        if (value <= maxValue.Value)
                        {
                            result = true;
                        }
                    }
                }
                else
                {
                    result = true;
                }
            }
        }
    }
    return result;
}

AnswerRe: Decimal values input validation Pin
BillWoodruff22-Mar-19 19:47
professionalBillWoodruff22-Mar-19 19:47 
AnswerRe: Decimal values input validation Pin
jschell23-Mar-19 5:42
jschell23-Mar-19 5:42 
GeneralRe: Decimal values input validation Pin
BillWoodruff23-Mar-19 6:24
professionalBillWoodruff23-Mar-19 6:24 
Questionprogramming Pin
Member 1419026720-Mar-19 12:41
Member 1419026720-Mar-19 12:41 
AnswerRe: programming Pin
Dave Kreskowiak20-Mar-19 15:51
mveDave Kreskowiak20-Mar-19 15:51 
AnswerRe: programming Pin
OriginalGriff20-Mar-19 21:03
mveOriginalGriff20-Mar-19 21:03 
GeneralRe: programming Pin
Ralf Meier20-Mar-19 22:17
mveRalf Meier20-Mar-19 22:17 
GeneralRe: programming Pin
OriginalGriff20-Mar-19 22:23
mveOriginalGriff20-Mar-19 22:23 
GeneralRe: programming Pin
Ralf Meier20-Mar-19 23:32
mveRalf Meier20-Mar-19 23:32 
GeneralRe: programming Pin
OriginalGriff20-Mar-19 23:42
mveOriginalGriff20-Mar-19 23:42 
AnswerRe: programming Pin
jschell23-Mar-19 5:45
jschell23-Mar-19 5:45 
QuestionQuantization the value of HSV Color using Non-equal Intervals Pin
gnjr9720-Mar-19 0:28
gnjr9720-Mar-19 0:28 
AnswerRe: Quantization the value of HSV Color using Non-equal Intervals Pin
OriginalGriff20-Mar-19 0:48
mveOriginalGriff20-Mar-19 0:48 
AnswerRe: Quantization the value of HSV Color using Non-equal Intervals Pin
Richard MacCutchan20-Mar-19 2:29
mveRichard MacCutchan20-Mar-19 2:29 
QuestionC#In Canon Camera Development, find error Device Busy Pin
Member 1415621219-Mar-19 17:12
Member 1415621219-Mar-19 17:12 
AnswerRe: C#In Canon Camera Development, find error Device Busy Pin
OriginalGriff19-Mar-19 20:59
mveOriginalGriff19-Mar-19 20:59 
AnswerRe: C#In Canon Camera Development, find error Device Busy Pin
Gerry Schmitz20-Mar-19 5:15
mveGerry Schmitz20-Mar-19 5:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.