Reputation: 11
I have a windows form which is set to be refreshed automatically using button as shown below: (code not working) how to do ?
Collapse | Copy Code
private void Refresh_Click(object sender, EventArgs e)
{
this.Refresh();
}
Upvotes: 0
Views: 8896
Reputation: 941397
This code will most certainly refresh the form. It is however very unlikely to make any visible difference, Windows and Winforms are already very good at keeping the form updated without any help at all. You have to do something else as well, something that would make the form paint differently.
A trivial example:
private bool drawMessage;
private void Refresh_Click(object sender, EventArgs e) {
drawMessage = !drawMessage;
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e) {
if (drawMessage) {
TextRenderer.DrawText(e.Graphics, "You clicked Refresh", this.Font, Point.Empty, this.ForeColor);
}
base.OnPaint(e);
}
Upvotes: 1
Reputation: 23833
What you have, should refresh the current form that that event belongs to. It is hard to make out what your problem is due to the lack of intelligable information (what is happening and why are you saying it does not work? etc.). However, you could try
private void Refresh_Click(object sender, EventArgs e)
{
this.Update();
}
The difference between the two methods is thus:
Control.Update()
The Update() function calls the UpdateWindow function which updates the client area of the control by sending WM_PAINT message to the window (of the control) if the window's update region is not empty. This function sends a WM_PAINT directly to WNDPROC() bypassing the application message queue.
Thus, if the window update region is previously “invalidated” then calling “update” would immediately "update" (and cause repaint) the invalidation.
Control.Refresh()
Refresh() calls Invalidate(true) to invalidate the control and its children and then calls Update() to force paint the control so that the invalidation is synchronous.
I hope this helps.
Upvotes: 1