自定义控件未绘制Windows窗体按钮

死亡主义的朋友

我有一个自定义控件,试图将System.Windows.Forms.Button显示到该控件上。

这是上漆事件。这确实被调用,并且消息框显示正确的值。位置是1,1。大小为75,23,可见值为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());
    } 
}

这是组件的初始化。

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;
}
皮特·沃科夫斯基(Piotr Wolkowski)

代码的主要问题是,尽管您创建了OptionBtn按钮,但从未将其添加到中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);
}

我添加了这一行:

this.Controls.Add(this.OptionsBtn);

只有这样,按钮才会属于CustomControl

然后,您必须将您的内容添加CustomControl到其中Form这样的事情(假设您也想在代码中使用此位):

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);
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

自定义Windows窗体中的按钮

来自分类Dev

Windows窗体中的自定义控件出现问题

来自分类Dev

Windows窗体自定义控件绘画停止

来自分类Dev

创建自定义Windows窗体控件的最佳方法是什么?

来自分类Dev

自定义街景按钮/控件

来自分类Dev

自定义单选按钮控件(Winforms)

来自分类Dev

自定义控件图像未显示

来自分类Dev

自定义控件绑定未更新

来自分类Dev

在已经绘制的自定义控件上绘制

来自分类Dev

如何使用 .NET Windows 窗体中的实体框架向 DataGridView 添加自定义按钮

来自分类Dev

自定义视图未绘制

来自分类Dev

子控件未继承 WPF 自定义控件样式

来自分类Dev

在自定义WPF控件中强制重新绘制自定义绘制的UIElement

来自分类Dev

如何将自定义Windows窗体控件添加到Visual Studio工具箱

来自分类Dev

状态列表可绘制的自定义单选按钮未更改

来自分类Dev

角度自定义窗体控件永远不会弄脏我的窗体

来自分类Dev

如何绘制自定义控件,然后传播事件?

来自分类Dev

JavaFX-如何创建简单的自定义绘制控件?

来自分类Dev

Win32自定义绘制树视图控件

来自分类Dev

自定义绘制FMX控件位置错误

来自分类Dev

在WTL中的自定义绘制控件中实现滚动

来自分类Dev

Windows窗体图形未绘制

来自分类Dev

有没有办法使用 c#(Windows 窗体)从设计器访问自定义用户控件中的控件属性

来自分类Dev

将自定义控件绑定到外部按钮

来自分类Dev

为什么自定义控件的组件未启动?

来自分类Dev

自定义控件中的Datagrid未更新

来自分类Dev

C#WebBrowser控件未显示自定义HTML

来自分类Dev

Windows窗体控件如何绘制自身?

来自分类Dev

Windows表单的按钮自定义属性

Related 相关文章

热门标签

归档