I have a component which hooks up with the paint event of assigned control and draws on its surface, Whenever i make any changes to the component i need to resize the form or control at design time in order to see the changes.

I want to ask if there is any other way to refresh the contents of a control at design time which forces it to redraw.

Dani AI

Generated

You can get instant redraws in the designer without resizing. The trick is to invalidate the control you draw on whenever your component’s design-time state changes, and, in the designer, force the paint to happen immediately. is right that Refresh works at run time; at design time prefer Invalidate() and then Update() so the designer repaints right away. Invalidate queues a paint; Update flushes it synchronously. (learn.microsoft.com)

A small pattern I use inside a non-visual component that custom-paints a target control:

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;

public class OverlayPainter : Component
{
    public Control Target { get; set; }

    Color _color = System.Drawing.Color.Red;
    public Color Color
    {
        get => _color;
        set
        {
            if (_color == value) return;
            _color = value;

            // repaint the surface
            Target?.Invalidate();
            if (IsDesignMode) Target?.Update();

            // tell the designer something changed
            var change = (IComponentChangeService)Site?.GetService(typeof(IComponentChangeService));
            change?.OnComponentChanged(this, null, null, null);
        }
    }

    bool IsDesignMode =>
        LicenseManager.UsageMode == LicenseUsageMode.Designtime || (Site?.DesignMode ?? false);
}

Notes and gotchas:

  • Do not call Refresh or Invalidate from inside the control’s Paint handler; that can recurse. Trigger paints from property setters or explicit design-time commands. Invalidate schedules the paint; Update forces it now. (learn.microsoft.com)
  • The IsDesignMode check avoids surprising runtime costs and uses a reliable design-time detector for components. (learn.microsoft.com)
  • If you author a custom designer, you can also listen to IComponentChangeService.ComponentChanged and call Control.Refresh() (or Invalidate/Update) when related properties change, which is the pattern Microsoft shows in their design-time walkthroughs. (learn.microsoft.com)

If you need to debug this behavior interactively, attach a second VS instance to the designer and step through your setters and paint code. (learn.microsoft.com)

Recommended Answers

All 6 Replies

Are you sure you mean design time? Design time is when you are editing the control in the designer and you wouldnt see changes to its paint event in the designer.
Do you mean you want to show changes to the control whilst the application is running? If so, thats run-time. You can call Refresh on the control to force a repaint. SO for a button called btn1 you would use btn1.Refresh()

Are you sure you mean design time? Design time is when you are editing the control in the designer and you wouldnt see changes to its paint event in the designer.
Do you mean you want to show changes to the control whilst the application is running? If so, thats run-time. You can call Refresh on the control to force a repaint. SO for a button called btn1 you would use btn1.Refresh()

Im sure its called design time.

Design time means when you are designing your applications interface, as you know you see the form you are editing because it was created by visual studios ide for you by writing code which is hidden from you the extension for this file is . and accompanies any corresponding .vb file which has a interface or inherits from Form Class.

Now for the refresh or invalidate, i mean that if you close the design window or either resize the form or control and then re open the designer of this control again it will be rendered again

I am simply asking if i can force the designer of Visual Studio to re Render the form or control so that i can immediately see the changes.

I hope you understand what i mean now .

I understand what you mean, but i dont understand why :/ As far as i was aware, any code you place in the Paint event would process at run time.
Where abouts are you putting the code, because anything i place in a Paint event is not reflected in the designer

No thats not true, any handler that handles the paint event of a component will be executed and event will be processed for once before the form or control is displayed in designer, only difference is that you cannot place break execution at any point, unless ur using another instance of Visual studio to debug at design time.

In that case i guess i cant help you. As far as i was aware, the design view updated whenever it recieved focus. Good luck with your problem, i hope someone who knows more than me can pitch in :)

yea i hope, but thanks anyways for showing interest in solving my problem. i appreciate it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.