S
S
swyt2020-06-20 23:13:10
C++ / C#
swyt, 2020-06-20 23:13:10

How to make array input through textbox?

In my code, for some reason, only the last element of the array is written. For example, I enter an array size of 3 and I enter the numbers 1, 2 and 3, then the output I get is 0 0 3.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _masss
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int a = 0, b, c = 0, d = 1, f = -1;

        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = "Введите размер массива";
        }
        string str = "";
        private void button1_Click(object sender, EventArgs e)
        {   
            if (a == 0)
            {
                b = int.Parse(textBox1.Text);
                label1.Text = d.ToString() + " число";
            }
            int[] arr = new int[b];
            if (a > 0)
            {
                if (f <= b)
                {
                    label1.Text = d.ToString() + " число";
                    while (c == 0)
                    {
                        arr[f] = int.Parse(textBox1.Text);
                        c++;
                    }
                }
                
            }
            c = 0;
            a++;
            f++;
            d++;
            if (f == b)
            {
                for (int i = 0; i < b; i++)
                {
                    str += arr[i] + " ";
                }
                label1.Text = str;
            }
            textBox1.Clear();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
LiptonOlolo, 2020-06-20
@swyt

Some kind of nightmare.

public int[] ParseString(string str)
{
  return str.Split(' ').Select(x => int.Parse(x)).ToArray();
}

S
Sergei Chamkin, 2020-06-20
@Sergei1337

Skip the extra 4-variable complexities and just grab the text from the field and get an array right away:
https://www.quora.com/How-can-I-convert-a-string-t...
https://stackoverflow.com /questions/2959161/convert...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question