Martijn
Martijn

Reputation: 24789

C# Onpaint is not firing

I have this code:

    private void HandleGUI()
    {
        if (_currentForm == null)
        {
            navigationSideBar1.Visible = false;
            pnlToolbar.Visible = false;

            return;
        }

        if (_currentForm.ShowNavigationBar)
        {
            HandleNavigationButton(_currentForm);
        }

        btnSave.Visible = _currentForm.ShowSaveButton;
        btnClose.Visible = _currentForm.ShowCloseButton;
        btnSave.Paint += new PaintEventHandler(btnSave_Paint);
        navigationSideBar1.Visible = _currentForm.ShowNavigationBar;
        pnlToolbar.Visible = _currentForm.ShowToolBar;

        btnSave.Refresh();
        btnSave.Invalidate();
    }

I am registered on the onpaint event of the save button (btnSave), but this event is not fired, even when I call Refresh or Invalidate. How is this possible?

EDIT: This is how the saave button class looks like:

public class SaveButton : ButtonX
{
    public SaveButton()
    {
        this.Image = Properties.Resources.Save;
        this.Text = "Opslaan";
        this.Size = new Size(108, 39);

    }
}

Upvotes: 0

Views: 6530

Answers (4)

Juraj Maciak
Juraj Maciak

Reputation: 11

It looks like you're not calling the base class constructor in the constructor of your SaveButton component.

public class SaveButton : ButtonX
{
    public SaveButton() : base()
...

Upvotes: 0

Rashmi
Rashmi

Reputation: 21

Setting userpaint to true will fire the onpaint event

this.SetStyle(ControlStyles.UserPaint, true);

Upvotes: 2

MusiGenesis
MusiGenesis

Reputation: 75296

Try adding a regular DevComponent button (i.e. not a subclass of it) to a test form and see if it ever fires its Paint event. They may have exposed a Paint event (so that their interface matches that of a regular button) but not actually implemented it.

Upvotes: 2

jklemmack
jklemmack

Reputation: 3636

From MSDN:

Calling the Invalidate method does not force a synchronous paint; to force a synchronous paint, call the Update method after calling the Invalidate method.

So, you need an Update call. Now, Refresh is just Invalidate w/ update children + Update, so theoretically you're taken care of. All I can think is that Windows doesn't call Paint unless it really needs to, ie when the form is shown on the UI or written to a graphics device ("screenshot" of an invisible window). Are either of these the case?

Upvotes: 1

Related Questions