Click here to Skip to main content
15,920,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do i take multiple user inputs from 1 texbox using array like the code i wrote in console. As i am new to programming, i have searched for hours and i can't find anything.

What I have tried:

int i;
int size = 2;
string[] name = new string [size];
string[] age = new string[size];

Console.WriteLine(" **Enter name and age to find your record** ");

for (i = 0; i < size; i++)
{
Console.WriteLine("Enter Name: ");
name[i] = Console.ReadLine();

Console.WriteLine("Enter Age: ");
age[i] = Console.ReadLine();

}


Console.WriteLine("Enter name or age to find record:");
string find = Console.ReadLine();

for (i = 0; i < size; i++)
{

if (find == name[i])
{
Console.WriteLine("Name : " + name[i] + " Age : " + age[i]);

i++;
}

else if (find == age[i])
{


Console.WriteLine("Age :" + age[i] + " Name : " + name[i]);
i++;

}
Posted
Updated 21-Mar-18 23:46pm
Comments
Richard Deeming 20-Mar-18 10:22am    
That's not how Windows applications usually work. You present a textbox for each input, and let the user complete them in whatever order they like.
Member 13736271 21-Mar-18 3:23am    
i know, but what if you take inputs from textbox after a button is clicked. 1st time the button is clicked, that's your first input, second time the button is clicked that's your second input and so on ??

 static   Dictionary<int, string> nameage = new Dictionary<int, string>();

        public static void Main(string[] args)
        {
           
            Console.WriteLine("enter number records do u need");
            int records = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < records; i++)
            {
                Console.WriteLine("Enter name and age");
                Console.WriteLine("Name  :");
                string name = Convert.ToString(Console.ReadLine());
                Console.WriteLine("Age :");
                int age = Convert.ToInt32(Console.ReadLine());
                nameage.Add(age, name);
            }
            switchcase();
            Console.WriteLine("do you want check it again if yes enter 1 else enter 2");
            int a =Convert.ToInt16( Console.ReadLine());
            if (a == 1)
            {
                switchcase();
            }
}



public static void switchcase()
        {
            Console.WriteLine("do u want to enter name select 1 else select 2");
            int select = Convert.ToInt16(Console.ReadLine());
            switch (select)
            {
                case 1:
                    Console.WriteLine("enter the name you want to search");
                    string name = Convert.ToString(Console.ReadLine());
                    foreach(KeyValuePair<int,string> d in nameage)
                    {
                        if (d.Value == name)
                        {
                            Console.WriteLine("the name is " + name + "  " + "the age is " + d.Key);
                            Console.ReadLine();
                        }
                    }

                    break;
                case 2:
                    Console.WriteLine("enter the age you want to search");
                   int age = Convert.ToInt16(Console.ReadLine());
                    foreach (KeyValuePair<int, string> d in nameage)
                    {
                        if (d.Key == age)
                        {
                            Console.WriteLine("the name is " + d.Value + "  " + "the age is " + d.Key); Console.ReadLine();
                        }
                    }

                    break;
                   
            }
        }
 
Share this answer
 
Comments
Member 13736271 20-Mar-18 7:57am    
can you do the same by using winforms?? taking values from textBoxes??
saimanisha 20-Mar-18 8:18am    
you explain me clearly .do u want take multiple names from single textbox at a time or from multiple textboxes ..
Member 13736271 21-Mar-18 0:17am    
multiple names from single textbox
saimanisha 21-Mar-18 9:08am    
will tell u tmrw
Member 13736271 22-Mar-18 0:17am    
ok, will be waiting for your reply
Dictionary<string, string> nameage = new Dictionary<string, string>();
       //this is for saving data what you enetered
       private void btn_Click(object sender, RoutedEventArgs e)
       {
           //tb textbox name of property :NAME
           //agetb is the textbox name of property : AGE
           //Here am considering tb and agetb separate textboxes ,names with spaces in tb textbox and age with spaces in agetb textbox

           string str = null;
           string[] strArrname = null;
           str = tb.Text;
           string agee = agetb.Text;
           char[] splitchar = { ' ' };
           strArrname = str.Split(splitchar);
           string[] strarrayage = agee.Split(splitchar);
           for (int name = 0; name < strArrname.Length ; name++)
           {
              for(int age=0; age ==name; age++)
               {
                   nameage.Add(strArrname[name],strarrayage[age]);
               }
           }
           tb.Text = "";agetb.Text = "";
       }
       //this is used to search if u enter name in tb textbox it displays name and age in agetb textbox
       private void search_Click(object sender, RoutedEventArgs e)
       {
           //here you enter name or age for searching
           string nameAGE = tb.Text;
           foreach (KeyValuePair<string, string> d in nameage)
           {
               if (d.Value == nameAGE)
               {
                   agetb.Text = "Name is " + "  " + d.Value + "  " + "Age is " + "   " + d.Key;
               }
               else if(d.Key == nameAGE)
               {
                   agetb.Text = "Name is " + "  " + d.Value + "  " + "Age is " + "   " + d.Key;

               }
           }

       }
 
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