简体   繁体   中英

How to copy values from textbox into array of labels?

OK Here's what I did and the values which were set vertical are copied in the labels but horizontal. And only one column/row.

public partial class Form1 : Form
{
    private Label l;
    private Button bStart;
    private TextBox txtVnes;
    private Label[] pole;

    public Form1()
    {
        InitializeComponent();
        bStart = new Button();
        bStart.Location = new Point(240, 165);
        bStart.Width = 75;
        bStart.Height = 25;
        bStart.Text = "START";

        txtVnes = new TextBox();
        txtVnes.Location = new Point(240, 10);
        txtVnes.Width = 160;
        txtVnes.Height = 130;
        txtVnes.Multiline = true;

        int a = 0;
        pole = new Label[42];
        for (int i = 1; i <= 6; i++)
        {
            for (int j = 1; j <= 7; j++)
            {
                l = new Label();
                l.Name = "label" + i.ToString() + j.ToString();
                l.Text = "Z";
                l.Width = 20;
                l.Height = 20;
                l.TextAlign = ContentAlignment.MiddleCenter;
                l.Parent = this;
                l.BackColor = Color.FromArgb(100, 149, 237);
                l.Location = new Point(10 + (j - 1) * 25, 15 + (i - 1) * 25);
                pole[a] = l;
                this.Controls.Add(l);
                a++;

            }
        }

        this.Controls.Add(bStart);
        this.Controls.Add(txtVnes);

        bStart.Click += new EventHandler(bStart_Click);

    }


    private void bStart_Click(object sender, EventArgs e)
    {

        Regex regex = new Regex(@"^(\s)*(\d ){6}\d(\s)*$");
        bool isValid = true;
        string[] ts = txtVnes.Text.Split(new string[] { "\r\n" },
        StringSplitOptions.RemoveEmptyEntries);


        if (ts == null || ts.Length < 1 || ts.Length > 6)
        {
            MessageBox.Show("Not valid");
        }
        else
        {
            foreach (string t in ts)
            {
                if (regex.IsMatch(t) == false)
                {
                    MessageBox.Show("Not valid");
                    break;
                }
            }

        }

        if (isValid)
        {

            for (int i = 0; i < 6; i++)
            {
                if (i < ts.Length && regex.IsMatch(ts[i]))
                { 

                    pole[i].Text = ts[i];
                }
                else
                {
                    pole[i].Text = "not valid";
                }
            }

        }
    }

Here's a photo

So here is the problem: When I click on the button bStart only one value is copied and replaced in one labe from the array of labels. This should work like this: After the user clicks on the button bStart, all values from the textbox txtVnes should be copied in each label in the array of labels. All the labels have text "Z", and after click on the button they should be changed with the values in the textbox txtVnes. As you can see i used l.Text = txtVnes.Text; to copy the values, but it doesn't work. I appreciate if you can help me, thank you!

You are always setting the text of the same label l . Since your labels are in the array pole , you should set the text to consecutive pole indices.

I you want all the valid texts at the beginning:

string[] ts = txtVnes.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
int k = 0;
for (int i = 0; i < ts.Length && k < 6; i++) {
    if (IsValid(ts[i])) { // Where IsValid is a method containing your validation logic.
        pole[k++].Text = ts[i];
    }
}

// Fill remaining labels
for (int i = k; i < 6; i++) {
    pole[i].Text = "not valid";
}

Or, if you want vaild and invalid texts mixed:

string[] ts = txtVnes.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < 6; i++) {
    if (i < ts.Length && IsValid(ts[i])) { // Where IsValid is a method containing your validation logic.
        pole[i].Text = ts[i];
    } else {
        pole[i].Text = "not valid";
    }
}

Note that array indices begin at 0, not at 1.


EDIT #2:

The IsValid method would look like this:

private static Regex regex = new Regex(@"^(\s)*(\d ){6}\d(\s)*$"); 

private static bool IsValid(string s)
{
    return regex.IsMatch(s);
}

To answer your question in the comment: Yes, my examples above have to be placed in bStart_Click .

And there is also another error in your form constructor. this.Controls.Add(l); should be placed inside the inner for-loop, just after pole[a] = l; , otherwise only one label will be visible on your form.

Finally after having implemented and anlyzed your code, I came to the conclusion that you want to be able to enter text in the following format into the textbox and then place the digits into corresponding labels:

1 2 3 4 5 6 7
2 3 4 6 7 8 0
0 1 2 6 6 6 7
1 2 3 4 5 6 7
2 3 4 6 7 8 0
0 1 2 6 6 6 7

The complete code should look like this:

public partial class Form1 : Form
{
    private Button bStart;
    private TextBox txtVnes;
    private Label[] pole;

    public Form1()
    {
        InitializeComponent();
        bStart = new Button();
        bStart.Location = new Point(240, 165);
        bStart.Width = 75;
        bStart.Height = 25;
        bStart.Text = "START";
        bStart.Click += new EventHandler(bStart_Click);
        this.Controls.Add(bStart);

        txtVnes = new TextBox();
        txtVnes.Location = new Point(240, 10);
        txtVnes.Width = 160;
        txtVnes.Height = 130;
        txtVnes.Multiline = true;
        this.Controls.Add(txtVnes);

        int a = 0;
        pole = new Label[42];
        for (int i = 1; i <= 6; i++) {
            for (int j = 1; j <= 7; j++) {
                var l = new Label();
                l.Name = "label" + i.ToString() + j.ToString();
                l.Text = "Z";
                l.Width = 20;
                l.Height = 20;
                l.TextAlign = ContentAlignment.MiddleCenter;
                l.Parent = this;
                l.BackColor = Color.FromArgb(100, 149, 237);
                l.Location = new Point(10 + (j - 1) * 25, 15 + (i - 1) * 25);
                pole[a] = l;
                this.Controls.Add(l);
                a++;
            }
        }
    }

    private void bStart_Click(object sender, EventArgs e)
    {
        string[] ts = txtVnes.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        int row = 0;
        for (int i = 0; i < ts.Length && row < 6; i++) {
            if (LineIsValid(ts[i])) {
                for (int col = 0; col < 7; col++) {
                    pole[row * 7 + col].Text = ts[i][2 * col].ToString();
                }
                row++;
            }
        }

        // Fill remaining labels
        for (; row < 6; row++) {
            for (int col = 0; col < 7; col++) {
                pole[row * 7 + col].Text = "Z";
            }
        }
    }

    private static Regex regex = new Regex(@"^(\s)*(\d ){6}\d(\s)*$");

    private static bool LineIsValid(string line)
    {
        return regex.IsMatch(line);
    }
}

Two nested loops are required in bStart_Click as well. One for the rows and one for the columns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM