Simon Woker
Simon Woker

Reputation: 5034

Visual Studio Add-In: Force Invalidate

I'm writing a Visual Studio toolbar Add-In that displays the current time.

I have a CommandBar toolbar with a CommandBarButton timeLabel on it (because there's no label available) and a Timer.
Everytime the Timer-Event hits, I set the caption to the current time.

DateTime t = DateTime.Now;
timeLabel.Caption = String.Format("{0}:{1}:{2}", t.Hour, t.Minute, t.Second);
// force Invalidate/repaint
timeLabel.Visible = !timeLabel.Visible;
timeLabel.Visible = !timeLabel.Visible;

Is there a more elegant way to do an Invalidate()? I feel really uncomfortable with this solution.

Thanks & kind regards
Simon

Upvotes: 0

Views: 312

Answers (1)

Simon Woker
Simon Woker

Reputation: 5034

I found an answer for VS 2010, but this doesn't seem to work for VS 08 unfortunately.
It updates the whole UI:

// Invalidate the VS UI
ThreadHelper.Generic.Invoke(new System.Action(() =>
{
    ServiceProvider serviceProvider = new ServiceProvider(((DTE)Microsoft.VisualStudio.Shell.ServiceProvider.GlobalProvider.GetService(typeof(DTE))) as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
    IVsUIShell uiShell = serviceProvider.GetService(typeof(SVsUIShell)) as IVsUIShell;
    uiShell.UpdateCommandUI(0);
}));

IMHO my first solution is more understandable than these 3 lines of code, but this could help in case somebody comes across another problem like I did.

Upvotes: 1

Related Questions