Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a textbox (multiline = true) with values ​​separated by lines, for example:
1
2
3
4
I wanted these values ​​to be placed, via a button, in a list or even a vector to be able to use them in a calculation

What I have tried:

C#
//cálcular o Nspt inicial e final
//textInt_FNspt_N1 it's a texbox

        List<int32> N1 = new List<int>();

        private void btn_FNspt_calnspt_Click(object sender, EventArgs e)
        {
            N1.Clear();

            foreach (Int32 item in textInt_FNspt_N1.Text)
	        {
                N1.Add(Convert.ToInt32(item));		 
	        }
            MessageBox.Show(N1.Count.ToString());
        }
but when I do this code I ask to show the list size to the messagebox, which generates a different value ...
Posted
Updated 9-Mar-20 20:42pm
v2

1 solution

You have to enumerate the lines and parse the values:
C#
private void btn_FNspt_calnspt_Click(object sender, EventArgs e)
{
   N1.Clear();

   string[] lines = textInt_FNspt_N1.Text.Split
   (
      new string[] { Environment.NewLine },
      StringSplitOptions.RemoveEmptyEntries
   );

   foreach (string line in lines)
   {
      if (int.TryParse(line, out int value))
      {
         N1.Add(value);
      }
   }

   MessageBox.Show(N1.Count.ToString());
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900