niki123
niki123

Reputation: 43

c# assign multiple values taken from a single text box to labels

I want to input more than 1 number to a text box separated by commas and display each number in a label. My problem is how to set the delimiter as comma and what method should I use to get all the numbers from a single text box?

Upvotes: 2

Views: 1102

Answers (2)

Zein Makki
Zein Makki

Reputation: 30022

To Split, you can do the following:

List<int> numbersFromInput = txtBoxInput.Text.Split(',')
                                        .Where(x=> x.All(c => Char.IsNumber(c)))
                                        .Select(x => Int32.Parse(x))
                                        .ToList();

For info, you can combine a list to a single string with a delimeter using the below code:

txtBoxInput.Text = String.Join(",", numbersFromInput);

Upvotes: 1

niki123
niki123

Reputation: 43

This is what I used to solve my own problem :)

private void button1_Click(object sender, EventArgs e)
        {
            char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
            String numbers = textBox1.Text;
            String[] numbersArray = numbers.Split(delimiterChars);
            int[] num = Array.ConvertAll(numbersArray,Convert.ToInt32);
           // MessageBox.Show("No1 :"+num[0].ToString());

        }

Upvotes: 1

Related Questions