Ali Tor
Ali Tor

Reputation: 3015

Why doesn't work custom control's paint event in Form.cs?

I have a custom button. OnPaint method is working in control's class file but the Button.Paint method doesn't work in Form.cs. Why does this happen and how can it be fixed?

My code for button:

//code...        
public AltoButton()
{     
    SetStyle(ControlStyles.AllPaintingInWmPaint|ControlStyles.OptimizedDoubleBuffer|ControlStyles.ResizeRedraw|ControlStyles.SupportsTransparentBackColor|ControlStyles.UserPaint, true);
    BackColor = Color.Transparent;
    ForeColor = Color.Black;
    Font = new System.Drawing.Font("Comic Sans MS", 10, FontStyle.Bold);
    state = MouseState.Leave;
    transparency = false;
}
#endregion
#region Events
protected override void OnPaint(PaintEventArgs e)
{
    //code to draw shape and painting
}
//code...

code in Form.cs:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        altoButton1.Paint += altoButton1_Paint;
    }

    void altoButton1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawLine(Pens.Red, 3, 3, 10, 10);
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        altoButton1.Invalidate();
    }
}

Upvotes: 1

Views: 977

Answers (2)

Denis Timchuk
Denis Timchuk

Reputation: 11

The problem could be with the drawing elements on the screen - you should practise doing this to get known more(this is unique in different projects).Excatly you can add element to screen , to the tab etc...Hope it would help you.(Helped me:P)

Upvotes: 0

Ali Tor
Ali Tor

Reputation: 3015

I have found a solution myself. The reason is the I deleted the base.OnPaint(e) at the OnPaint(e) code block.

The solution is just to add base.OnPaint(e)

protected override void OnPaint(PaintEventArgs e)
{
  //code to draw shape and painting
  base.OnPaint(e);
}

Upvotes: 1

Related Questions