C#更新动态生成的UserControl标签的文本

问题描述

我真的在C#中挣扎。我已经获得了一个可以处理的项目,但是我的大部分经验是C。该应用程序的目的是对API进行ping操作,并以Windows形式显示其中的数据。

我可以获取数据并动态显示它,但是我不知道如何用每3秒获取的新数据更新表单。

数据源提供用户信息。可以有x个用户。因此,经过大量的谷歌搜索和读取其他SO线程之后,我确定最好的方法似乎是创建一个UserControl并将其调用x次,从而将每个用户添加到屏幕上。这有效。但是,我似乎无法用新数据更新控件。

这是我的代码

UserControl: UserStatus.cs

    namespace RCQMonitor
    {
        public partial class UserStatus : UserControl
        {
            public UserStatus()
            {
                InitializeComponent();
            }
            public UserStatus(ExtensionInfo user) : this()
            {
                labelName.Text = user.Name;
                labelStatus.Text = user.DndStatus;
                labelStatic.Text = "Status:";
            }
        }
    }

UserStatus.Designer.cs

{
    partial class UserStatus
    {
        /// <summary> 
        /// required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise,false.</param>
        protected override void dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.dispose();
            }
            base.dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.labelName = new System.Windows.Forms.Label();
            this.labelStatic = new System.Windows.Forms.Label();
            this.labelStatus = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // labelName
            // 
            this.labelName.AutoSize = true;
            this.labelName.Location = new System.Drawing.Point(0,0);
            this.labelName.Name = "labelName";
            this.labelName.Size = new System.Drawing.Size(53,15);
            this.labelName.TabIndex = 0;
            this.labelName.Text = "First Last";
            // 
            // labelStatic
            // 
            this.labelStatic.AutoSize = true;
            this.labelStatic.Location = new System.Drawing.Point(118,0);
            this.labelStatic.Name = "labelStatic";
            this.labelStatic.Size = new System.Drawing.Size(42,15);
            this.labelStatic.TabIndex = 1;
            this.labelStatic.Text = "Status:";
            // 
            // labelStatus
            // 
            this.labelStatus.AutoSize = true;
            this.labelStatus.Location = new System.Drawing.Point(166,0);
            this.labelStatus.Name = "labelStatus";
            this.labelStatus.Size = new System.Drawing.Size(38,15);
            this.labelStatus.TabIndex = 2;
            this.labelStatus.Text = "label3";
            // 
            // UserStatus
            // 
            this.AutoScaleDimensions = new System.Drawing.Sizef(7F,15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.labelStatus);
            this.Controls.Add(this.labelStatic);
            this.Controls.Add(this.labelName);
            this.Name = "UserStatus";
            this.Size = new System.Drawing.Size(311,22);
            this.ResumeLayout(false);
            this.Performlayout();

        }

        #endregion

        private System.Windows.Forms.Label labelName;
        private System.Windows.Forms.Label labelStatic;
        private System.Windows.Forms.Label labelStatus;
    }
}

表格: Form1.cs

namespace RCQMonitor
{
    public partial class Form1 : Form
    {
        private static System.Timers.Timer timer;

        public Form1()
        {
            InitializeComponent();
            timer = new System.Timers.Timer(3000);
            timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            timer.Enabled = true;

        }

        private void Form1_Load(object sender,EventArgs e)
        {

        }

        private async void OnTimedEvent(object source,ElapsedEventArgs e)
        {
            await Program.GetPresence(); //This just grabs the new data,it works.
            this.Refresh();
        }

    }
}

Form1.Designer.cs

namespace RCQMonitor
{
    partial class Form1
    {
        /// <summary>
        ///  required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise,false.</param>
        protected override void dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.dispose();
            }
            base.dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        ///  required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();

            
            int y = 0;
            int x = 0;

            for (int i = 0; i < Program.NumOfExtensions; i++)
            {
                var control = new UserStatus(Program.extensions[i]);
                control.Location = new Point(x,y);
                this.Controls.Add(control);
                y += control.Height;
            }

            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.Sizef(7F,15F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(350,165);
            this.Name = "Form1";
            this.Text = "QMonitor";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.Performlayout();

        }

        #endregion
        private System.Windows.Forms.Label extLabel1;
        private System.Windows.Forms.Label statusLabel1;
        private System.Windows.Forms.Label statusLabel2;
    }
}

就像我说的那样,我对C#还是很陌生,所以我不得不蚕食了一些示例才能使它工作到现在。我确定这很乱。

由于我是动态添加UserControl的,因此如何引用它们以更新它们?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)