在正在运行的程序中覆盖标签文本

时间:2017-03-23 16:50:52

标签: c# winforms label overwrite

我是C#的新手,我需要覆盖windows窗体上的标签。我正在使用循环生成n个值的标签数量。

问题:如果我在运行程序时更改lines[i]的值,则值会更改,但不会在Windows窗体上更新(以前的值不会被新的值替换)。

任何人都可以指导我如何做到这一点?

此外,我每秒使用计时器刷新代码,这也正常工作

这是我创建标签并在其中写入值的部分,它在循环中

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

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

这是我完整的代码ID,任何人都需要检查它:

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


    }
}

1 个答案:

答案 0 :(得分:1)

为您的标签Name添加唯一ID并搜索并替换值:

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