deathismyfriend
deathismyfriend

Reputation: 2192

Custom Control is not Drawing a Windows Form Button

I have a custom control that I am trying to display a System.Windows.Forms.Button onto.

Here is the On paint event. This does get called and the message box is displaying the correct values. Location is 1,1. Size is 75,23 and visible is true.

public partial class CustomControl : Control
{
    public CustomControl()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics graphics = e.Graphics;
        Rectangle rect = e.ClipRectangle;
        // Other code here.
        OptionsBtn.Refresh(); // I tried Invalidate and Update. Neither of them worked.
        //MessageBox.Show(OptionsBtn.Location.ToString() + "\n" + OptionsBtn.Size.ToString() + "\n" + OptionsBtn.Visible.ToString());
    } 
}

Here is the Initialization for the components.

partial class CustomControl
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Component Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.OptionsBtn = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // OptionsBtn
        // 
        this.OptionsBtn.BackColor = System.Drawing.Color.Blue;
        this.OptionsBtn.ForeColor = System.Drawing.Color.Red;
        this.OptionsBtn.Location = new System.Drawing.Point(1, 1);
        this.OptionsBtn.Name = "Options";
        this.OptionsBtn.Size = new System.Drawing.Size(75, 23);
        this.OptionsBtn.TabIndex = 0;
        this.OptionsBtn.Text = "Options";
        this.OptionsBtn.UseVisualStyleBackColor = false;
        this.ResumeLayout(false);
    }

    #endregion

    private System.Windows.Forms.Button OptionsBtn;
}

Upvotes: 0

Views: 464

Answers (1)

PiotrWolkowski
PiotrWolkowski

Reputation: 8782

Main issue with your code is that although you create the OptionBtn button you never add it to the CustomControl.

private void InitializeComponent()
{
    this.OptionsBtn = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // OptionsBtn
    // 
    this.OptionsBtn.BackColor = System.Drawing.Color.Blue;
    this.OptionsBtn.ForeColor = System.Drawing.Color.Red;
    this.OptionsBtn.Location = new System.Drawing.Point(1, 1);
    this.OptionsBtn.Name = "Options";
    this.OptionsBtn.Size = new System.Drawing.Size(75, 23);
    this.OptionsBtn.TabIndex = 0;
    this.OptionsBtn.Text = "Options";
    this.OptionsBtn.UseVisualStyleBackColor = false;
    this.Controls.Add(this.OptionsBtn);
    this.ResumeLayout(false);
}

I've added this line:

this.Controls.Add(this.OptionsBtn);

Only then the button will belong to the CustomControl.

Then, you have to add your CustomControl to some Form. Something like that (assuming you want this bit in code as well):

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        CustomControl cc = new CustomControl();
        cc.Location = new Point(100, 100);
        cc.Size = new Size(300, 300);
        this.Controls.Add(cc);
    }
}

Upvotes: 1

Related Questions