Member 13736271 Ответов: 2

Как я могу принимать несколько пользовательских входов?


как я могу взять несколько пользовательских входов из 1 texbox, используя массив, подобный коду, который я написал в консоли. Поскольку я новичок в программировании, я искал в течение нескольких часов, и я ничего не могу найти.

Что я уже пробовал:

int i;
int size = 2;
string[] name = новая строка [size];
string[] age = новая строка[размер];

Приставка.WriteLine(" **введите имя и возраст, чтобы найти свою запись** ");

для (i = 0; i < size; i++)
{
Приставка.WriteLine("Введите Имя: ");
имя[i] = консоль.Линия чтения();

Приставка.WriteLine("Введите Возраст: ");
возраст[i] = консоль.Линия чтения();

}


Приставка.WriteLine("введите имя или возраст, чтобы найти запись:");
строка find = Console.Линия чтения();

для (i = 0; i < size; i++)
{

if (find == name[i])
{
Приставка.WriteLine("имя :" + имя[i] + "возраст :" + возраст[i]);

я++;
}

еще если (найти == возраст[i])
{


Приставка.WriteLine("возраст :" + возраст[i] + "имя :" + имя[i]);
я++;

}

Richard Deeming

Это не то, как обычно работают приложения Windows. Вы представляете текстовое поле для каждого ввода и позволяете пользователю заполнять их в любом порядке, который ему нравится.

Member 13736271

я знаю, но что делать, если вы принимаете входные данные из текстового поля после нажатия кнопки. 1-й раз, когда нажимается кнопка, это ваш первый вход, второй раз, когда нажимается кнопка, это ваш второй вход и так далее ??

2 Ответов

Рейтинг:
6

saimanisha

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;

               }
           }

       }


Рейтинг:
1

saimanisha

 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;
                   
            }
        }


Member 13736271

можете ли вы сделать то же самое с помощью winforms?? принимать значения из текстовых полей??

saimanisha

вы мне ясно объясните, хотите ли вы взять несколько имен из одного текстового поля одновременно или из нескольких текстовых полей ..

Member 13736271

несколько имен из одного текстового поля

saimanisha

расскажу вам tmrw

Member 13736271

хорошо, буду ждать вашего ответа

saimanisha

я ответил ниже проверьте это

Member 13736271

спасибо за вашу помощь, очень ценю id.

saimanisha

если это решение помогло тогда примите решение