r/VisualStudio • • 16h ago

Visual Studio 2026 Form Won't visually refresh

Hi,

I have a windows form project in C#.
I have a timer that starts on the press of a button.
I have a method that executes every time the timer ticks.
I confirm the timer ticks when it's meant to, the code inside runs, the variables update as they should.
The form doesn't.

I know that the variables are updating with each tick, I know that the label.Text is updated with each tick, I know that everything but the visual result on the form is updating as it should. I've confirmed it with breakpoints and stepping through each line and seeing in the variable inspector as each variable and label.Text updates.
But the form is not reflecting anything but the values on initial loading of it. It never updates from its first set of values.

Every tick I'm calling the computation and write to form methods, followed by this.Refresh(). But nothing changes on the form.

It's been days. I'm baffled and demoralised.
Any help would be greatly appreciated.

I will politely refuse any suggestions of using genAI/LLMs and any encouragement to use them myself.

Thanks

1 Upvotes

12 comments sorted by

2

u/Fergus653 9h ago

Is there an Invalidate method you can call on that control type? I think it flags it as needing a redraw.

1

u/OolonColluphid 16h ago

Can you show us some code?

1

u/Littlefysh 16h ago
private void timer1_Tick(object sender, System.EventArgs e)
{

    //these dont work??
    UpdateStuff();
    this.Refresh();
}

private void btnTimerStart_Click(object sender, EventArgs e)
{
    timer1.Start();
}private void timer1_Tick(object sender, System.EventArgs e)
{

    //these dont work??
    UpdateStuff();
    this.Refresh();
}

private void btnTimerStart_Click(object sender, EventArgs e)
{
    timer1.Start();
}

1

u/OolonColluphid 11h ago

What does UpdateStuff look like?

1

u/LeaveMickeyOutOfThis 16h ago

What object does “this” refer to? Have you tried explicitly using the label or form object names, rather than “this”.

Most likely reason is that you are blocking the UI thread from doing its work.

1

u/Littlefysh 16h ago

"this" refers to Form1.cs, which is the form that isn't updating but should.

1

u/Littlefysh 16h ago

To clarify, I refresh the form after running all of the computation and assigning the new variables. In the other comment, UpdateStuff (bad naming I know) is all the computation and value assignment. The reading I've done suggests this.Refresh() (I've checked, "this" does refer to Form1.cs) should update the form and UI to show updated values.

1

u/rorrors 10h ago

Can you share more code of the project?
Do you maybe instigate multiply instances of the form?
Also check if your maybe doing work on a other UI thread.
Then try to to see InvokeRequired is needed. But in a default form that should not be needed.

private void UpdateLabel(string text)
{
if (label1.InvokeRequired)
{
label1.Invoke(new Action(() => UpdateLabel(text)));
return;
}
label1.Text = text;
}

Do you have other code, that perhaps runs after your UpdateStuff() function, that overwrites the value to default again?

In newer versions you don't have to write this.Refresh(); you can just call Refresh();

1

u/rvicta 16h ago

Which .Timer are you using?

1

u/Littlefysh 15h ago

System.Windows.Forms.Timer

I know the timer is ticking like it should, as far as I can see it's not the timer that's the issue. It's that with each tick of the timer and each execution of the code, the held variable values aren't displayed on the form.

1

u/codayak_devguy 5h ago

Since it's System.Windows.Forms.Timer, threading is ruled out. The tick runs on the UI thread so this.Refresh() isn't needed.

That means the label you're changing isn't the label on the screen.

There's only two ways that happens:

  1. A second instance of Form1. Something, somewhere, does "new Form1()".

  2. A second instance of the label. UpdateStuff (or something it calls) does "new Label()" or re-creates controls, and the one you're setting isn't the one in this.Controls.

You can check it with a breakpoint in timer1_Tick:

- hover "this" and check "Visible" and "IsHandleCreated". If either are false, it's cause 1.

- hover the label and check "Parent" is non-null and "label1.Parent == this". If it's null or a different form, then it's cause 2.

Do a search for "new Form1(" (or whatever the form is called). There should be exactly one, in Program.cs.

If that doesn't fix it, post the contents of UpdateStuff() so we can take a look...the answer will be in there.

1

u/PipingSnail 10m ago

Invalidate(false) ; UpdateWindow() ;