Reputation: 11
I'm really struggling with C#. I've been given a project to work on, but most of my experience is in C. The purpose of the application is to ping an API and display the data from it in a windows form.
I can get the data, and dynamically display it, but I cannot figure out how to update the form with new data grabbed every 3 seconds.
The data source provides User info. There can be x amount of users. Because of this, and after tons of googling and reading other SO threads, I've determined the best route seems to be to create a UserControl and call it x times, adding each user to the screen. This works. However, I can't seem to update the controls with new data.
Here is my code:
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;
}
}
Form: 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;
}
}
As I said, I'm very new to C#, so I've had to cannibalize examples to get this to work so far. I'm sure it's messy.
Since I'm adding the UserControls dynamically, how would I reference them in order to update them?
Upvotes: 1
Views: 90