我真的在 C# 上苦苦挣扎。我得到了一个项目要处理,但我的大部分经验都是在 C 中。应用程序的目的是 ping API 并以 Windows 形式显示来自它的数据。
我可以获取数据并动态显示它,但我不知道如何使用每 3 秒抓取一次的新数据更新表单。
数据源提供用户信息。可以有x个用户。因此,经过大量谷歌搜索和阅读其他 SO 线程后,我确定最好的方法似乎是创建一个 UserControl 并调用它 x 次,将每个用户添加到屏幕上。这行得通。但是,我似乎无法用新数据更新控件。
这是我的代码:
用户控件: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:";
}
}
}
用户状态.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# 很陌生,所以到目前为止我不得不拆解一些例子来让它工作。我确定它很乱。
由于我动态添加 UserControls,我将如何引用它们以更新它们?