Click here to Skip to main content
15,905,963 members
Home / Discussions / C#
   

C#

 
AnswerRe: OData InvalidOperationException. Bad Request - Error in query syntax Pin
dan!sh 27-Mar-19 3:58
professional dan!sh 27-Mar-19 3:58 
GeneralRe: OData InvalidOperationException. Bad Request - Error in query syntax Pin
Bastien Vandamme28-Mar-19 15:01
Bastien Vandamme28-Mar-19 15:01 
QuestionRestore fingerprint data for timekeeper Ronald jack x628-C ? Pin
Member 245846725-Mar-19 16:30
Member 245846725-Mar-19 16:30 
AnswerRe: Restore fingerprint data for timekeeper Ronald jack x628-C ? Pin
Pete O'Hanlon25-Mar-19 21:42
mvePete O'Hanlon25-Mar-19 21:42 
GeneralRe: Restore fingerprint data for timekeeper Ronald jack x628-C ? Pin
Member 245846726-Mar-19 16:28
Member 245846726-Mar-19 16:28 
AnswerRe: Restore fingerprint data for timekeeper Ronald jack x628-C ? Pin
OriginalGriff26-Mar-19 21:24
mveOriginalGriff26-Mar-19 21:24 
AnswerRe: Restore fingerprint data for timekeeper Ronald jack x628-C ? Pin
OriginalGriff25-Mar-19 22:54
mveOriginalGriff25-Mar-19 22:54 
QuestionExport from DataGridView to access _ c# Pin
Member 1419439024-Mar-19 12:15
Member 1419439024-Mar-19 12:15 
AnswerRe: Export from DataGridView to access _ c# Pin
josda100024-Mar-19 15:41
josda100024-Mar-19 15:41 
AnswerRe: Export from DataGridView to access _ c# Pin
OriginalGriff24-Mar-19 21:10
mveOriginalGriff24-Mar-19 21:10 
GeneralRe: Export from DataGridView to access _ c# Pin
Member 1419439024-Mar-19 22:11
Member 1419439024-Mar-19 22:11 
GeneralRe: Export from DataGridView to access _ c# Pin
OriginalGriff24-Mar-19 22:40
mveOriginalGriff24-Mar-19 22:40 
QuestionQuestion about login form with access database Pin
Member 1114879324-Mar-19 1:42
Member 1114879324-Mar-19 1:42 
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 
AnswerRe: Decimal values input validation Pin
BillWoodruff22-Mar-19 19:47
professionalBillWoodruff22-Mar-19 19:47 
On the one hand, I think developing step-by-step solutions for problems like this is a valuable educational experience for C# students that gets them familiar with character input, the Char Type, and the Switch statement; from another POV, it's something those familiar with RegEx can do relatively easily, and much more succinctly. And, as others have pointed out, can be handled by the NumericUpDown Control.

Left for you to do in the following example is implementing code to limit the range of legal input values: I would do that by handling either the Leave Event, or by handling the Enter Keydown, or, at the cost of more computation, the TextChanged Event. Think about whether at some point you want to notify the user the input value is invalid.

Usually ... users expect the Enter and Tab Keys to do something: think about that.

I suggest you sub-class a TextBox, and handle the character input like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;

namespace FourFormUI
{
    [ToolboxItem(true)]
    public partial class DoubleTextBox : TextBox
    {
        public DoubleTextBox()
        {
            InitializeComponent();
        }

        private bool delimiterSeen = false;

        private const char delimiter = '.';
        private const char backspace = '\b';
        private const char minus = '-';

        private string AllowedChars = ".-\b01234567890";
        private List<char> allowedChars;

        private int postDelimiterLimit = 3;

        public DoubleTextBox(IContainer container)
        {
            container.Add(this);

            InitializeComponent();

            allowedChars = AllowedChars.ToCharArray().ToList();
        }

        private void DoubleTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var ch = e.KeyChar;

            // legal ?
            if (!allowedChars.Contains(ch))
            {
                e.Handled = true;
                return;
            }

            switch (ch)
            {
                case delimiter:
                {
                    if (delimiterSeen)
                    {
                        e.Handled = true;
                        return;
                    }

                    delimiterSeen = true;

                    break;
                }

                case backspace: // delimiter erased
                {
                    delimiterSeen = Text.Contains(delimiter);
                    break;
                }

                case minus:
                {
                    if (this.SelectionStart != 0)
                    {
                        e.Handled = true;
                        return;
                    }

                    break;
                }

                default:
                {
                    if (delimiterSeen)
                    {
                        int delimiterpos = Text.IndexOf(delimiter);

                        if (this.SelectionStart > delimiterpos)
                        {
                            if (Text.Length - delimiterpos > postDelimiterLimit)
                            {
                                e.Handled = true;
                                return;
                            }
                        }
                    }

                    break;
                }
            }
        }
    }
}

«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

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 

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.