0

I'm new to C#, I need to overwrite label on windows form. I'm generating number of labels as per value of n using loop.

The issue: if I change the value of lines[i] while running the program, the value does get changed but is not updated on windows form (previous value does not gets replaced by new one).

Can any one guide me how can I do that?

Also I'm refreshing the code every second using timer, which is also working fine

This is the part where I create the label and write the value in it, and it is in loop

Label label = new Label();
label.Text = String.Format("{0}", lines[i]);

label.Left = 10;
label.Top = (i + 1) * 25;
this.Controls.Add(label);

here is my complete code id anyone needs to check it:

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;
using System.IO;

namespace Admin
{
    public partial class Form1 : Form
    {

        string pathuser = @"//192.168.2.10/Shared-Public/Users.txt";

        int check = 0;

        public static string usernam;
        string[] lines = new String[500];
        string[] lines2 = new String[500];
        public Form1()
        {
            InitializeComponent();
        }

        private void timer_Tick(object sender, EventArgs e)
        {

            tc = tc + 1;
            Console.WriteLine(tc);
            int c = 2;
            int u = 0;
            int us = 0; //for user pass
            int z = 0; // for reading values from file

            using (StreamReader sr2 = new StreamReader(pathuser))
            {

                string line;


                while ((line = sr2.ReadLine()) != null)
                {
                    lines[us] = line;


                    us++;
                }
            }


            for (int i = 0; i < us; i++)
            {

                //Create label
                Label label = new Label();
                label.Text = String.Format("{0}", lines[i]);
                //Position label on screen
                label.Left = 10;
                label.Top = (i + 1) * 25;


                Label label2 = new Label();
                label2.Left = 120;

                label2.Top = (i + 1) * 25;


                string line2;

                string path = "//192.168.2.10/Shared-Public/" + lines[i] + DateTime.Now.ToString(" MM-dd-yyyy") + ".txt";
                if (!File.Exists(path))
                {
                    lines2[z] = null;
                }
                else
                {
                    using (StreamReader sr3 = new StreamReader(path))
                    {

                        while ((line2 = sr3.ReadLine()) != null)
                        {
                            lines2[z] = line2;

                            z++;
                        }
                    }
                    label2.Text = String.Format("{0}", lines2[0]);

                    u = z;
                    z = 0;   
                }


                PictureBox picbox = new PictureBox();
                picbox.Location = new Point(240, 25 + (i*25));
                picbox.Size = new Size(15, 15);

                if (u%2==0)

                    picbox.BackColor = Color.Green; 

                else
                    picbox.BackColor = Color.Red;
                u = 0;


                this.Controls.Add(label); 
                this.Controls.Add(label2);
                this.Controls.Add(picbox);   

            }
        }
        int tc = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
           Timer timer = new Timer();
            timer.Interval = (1000);
            timer.Tick += new EventHandler(timer_Tick);           
            timer.Start();
        }


    }
}
Mubi
  • 11
  • 1
  • 8
  • If you create the label you want to change manually, you need to hold onto it in a variable on the form. Then, when you want to change it, you access the label by the variable and change it. However, you're probably doing something else wrong. Run your application and see if you can move the form while your code is refreshing every second. I bet your UI isn't responding, amirite? –  Mar 23 '17 at 16:55
  • 1
    You could give the `Label` a name that includes `i` , for example `label.Name = "label_" + i;`. Then when you update `list[i]` also update `Controls["label_" + i].Text = list[i];` – 001 Mar 23 '17 at 17:03
  • If I add a new value in the file where it is reading values from, it adds it to the form but if I edit the same value, it doesn't replace it..... – Mubi Mar 23 '17 at 17:04
  • @Mubi Read this: [Bind a label to a “variable”](http://stackoverflow.com/q/3147043/669576) – 001 Mar 23 '17 at 17:13
  • @Will I can move the form while refreshing every second. even value in lines[i] is changed (tried by writing it in console) – Mubi Mar 23 '17 at 21:41

1 Answers1

1

Give your label's Name a Unique id and Search for it and replace the value:

Label label = new Label();
var someid = new Guid().ToString();
label.Name = someid;
label.Text ="aaaa";

label.Left = 10;
this.Controls.Add(label);

foreach(Control item in this.Controls)
{
    if (item.Name == someid)
        item.Text = "bbb";
}
Sadique
  • 22,572
  • 7
  • 65
  • 91