Jmeker
Jmeker

Reputation: 3

I get a keyboard input in numbers and i want to keep a specific section of them to use in cases

I want to make a program that reads a line of numbers and symbols and I want to read only some specific numbers so I can make 3 cases that then outputs on a textbox something depending on the numbers from that. Can you help me by providing the code necessary for that? (I plan to use it in a windows form application)

Example: I get keyboard input -> (01)04006544860533(10)L825308500 and from that I want to keep only 04006544860533 so I can use it for case 1 and give me back the textbox4="....".

I can't change the input it has to be the long one.

Edit

Thanks for very fast answer

ok i used the code you provided in a button but i always get the default response on my text box so i think i miss something xD. if i type the number on the cases it works

(Program texts in greek hehe)

Code

private void button1_Click(object sender, EventArgs e) { string input = BarcodeTextBox.Text; string number = input.Substring(5, 14);

        switch (input) //tried switch (number) also
        {
            case ("4006544849033"):
            ProductTextBox.Text = "SLS ΛΕΥΚΗ ΖΑΧΑΡΗ ΑΠΟ ΖΑΧΑΡΟΚΑΛΑΜΟ 1kg";
            break;

            case ("4006544860533"):
            ProductTextBox.Text = "SLS ΑΚΑΤΕΡΓΑΣΤΗ ΖΑΧΑΡΗ 0,5kg";
            break;

            case ("4006544849637"):
            ProductTextBox.Text = "SLS ΑΧΝΗ ΖΑΧΑΡΗ 0,4kg";
            break;

            case ("4006544851630"):
            ProductTextBox.Text = "ΛΕΥΚΗ ΖΑΧΑΡΗ EU2-F 25κιλά ΖΑΧ/ΛΑΜΟ";
            break;

            case ("4006544901137"):
            ProductTextBox.Text = "ΚΡΤΣΤ. ZAX.GR 10X1kg AB";
            break;

            case ("4006544901335"):
            ProductTextBox.Text = "ΚΡΤΣΤ.GR 10X1 Κιλά LIDL ΕΛΛΑΣ";
            break;

            case ("5410256208115"):
            ProductTextBox.Text = "ΚΡΤΣΤ. ZAX.GR 10X1kg ΣΚΛΑΒΕΝΙΤΗΣ";
            break;

            case ("4006544901731"):
            ProductTextBox.Text = "ΚΡΤΣΤ.GR 10X1 Κιλά LIDL ΕΛΛΑΣ DHP";
            break;

            case ("4006544901830"):
            ProductTextBox.Text = "SLS ΑΧΝΗ ΖΑΧΑΡΗ 0,4kg Limited Edition";
            break;

            default:
            ProductTextBox.Text = "Λάθος Αριθμός Barcode, Ξαναπροσπαθήστε";
            break;
        } 
    }

Upvotes: 0

Views: 187

Answers (2)

Mike Dinescu
Mike Dinescu

Reputation: 55720

If the input text is always the same length and format that you could just use the Substring function like this:

 string input = "(01)04006544860533(10)L825308500";
 string number = input.Substring(4, 14);
 // if you also need the first number in parenthesis
 int firstNumber = Int32.Parse(input.Substring(1,2));  // this will be equal to 1

If string might have variable length but the format is always the same then you can use a Regular expression to get the number:

 Regex rex = new Regex("^\\(\\d+\\)(\\d+)");
 Match m = rex.Match(input);
 if(m.Success && m.Groups[1].Success){
     string number = m.Group[1].Value;
 }

Upvotes: 1

Caleb Keith
Caleb Keith

Reputation: 816

You can use a regular expression to validate numbers from the input.

\d{10} will match a 10 digit number.

You can do tutorials and learn more about them on this site: http://www.regular-expressions.info/

Upvotes: 0

Related Questions