How to copy values from textbox into array of labels?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Isabella
    New Member
    • Jan 2012
    • 3

    How to copy values from textbox into array of labels?

    Hi
    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!

    Code:
    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;
                    a++;
    
                }
            }
    
             this.Controls.Add(bStart);
             this.Controls.Add(txtVnes);
             this.Controls.Add(l);
             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)
            {
    
    
              l.Text = txtVnes.Text;
    
            }
           }
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    That's because you don't loop through your labels. l is just a single label.

    Comment

    • Allwin Jeba
      New Member
      • Jan 2011
      • 7

      #3
      Loop through your labels and assign them the text from respective textbox

      Comment

      Working...