Click here to Skip to main content
15,896,522 members
Home / Discussions / C#
   

C#

 
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 
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 
QuestionI found this strange in C# Pin
Brian_TheLion19-Mar-19 13:06
Brian_TheLion19-Mar-19 13:06 

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.