如何将值从文本框复制到标签数组中

本文关键字:复制 标签 数组 文本 | 更新日期: 2023-09-27 18:19:36

好的,这是我所做的,设置为垂直的值被复制到标签中,但却是水平的。并且只有一列/一行。

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

这是一张照片

所以问题来了:当我点击按钮bStart时,只有一个值被复制并替换到标签数组中的一个标签中。这应该是这样的:用户单击按钮bStart后,文本框txtVnes中的所有值都应该复制到标签数组中的每个标签中。所有标签都有文本"Z",点击按钮后,它们应该用文本框txtVnes中的值进行更改。正如您所看到的,我使用l.Text = txtVnes.Text;来复制值,但它不起作用。如果你能帮我,我很感激,谢谢!

如何将值从文本框复制到标签数组中

您总是设置相同标签l的文本。由于标签位于数组pole中,因此应将文本设置为连续的pole索引。

我想一开始就要所有有效的文本:

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

或者,如果你想混合无效和无效文本:

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

请注意,数组索引从0开始,而不是从1开始。


编辑#2:

IsValid方法如下所示:

private static Regex regex = new Regex(@"^('s)*('d ){6}'d('s)*$"); 
private static bool IsValid(string s)
{
    return regex.IsMatch(s);
}

要在评论中回答您的问题:是的,我上面的例子必须放在bStart_Click中。

表单构造函数中还有另一个错误。this.Controls.Add(l);应该放在内部for循环中,刚好在pole[a] = l;之后,否则表单上只能看到一个标签

最后,在实现并分析了您的代码后,我得出的结论是,您希望能够在文本框中输入以下格式的文本,然后将数字放入相应的标签中:

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

完整的代码应该是这样的:

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

bStart_Click中也需要两个嵌套循环。一个用于行,一个用于列。