自定义按钮不起作用

时间:2014-12-19 13:58:54

标签: c# visual-studio-2013

我想在UserControl中创建一个自定义按钮...就像悬停动画一样。但在用户控制中它不会让我执行事件?!

编辑:我尝试将其从用户控件中删除,但这并没有改变任何内容......

我的代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {
        public partial class Title_Bar : UserControl
        {
            public Title_Bar()
            {
                InitializeComponent();
            }

            private void cross_idle_MouseEnter(object sender, EventArgs e)
            {
                cross_hover.Show();
            }

            private void cross_hover_MouseLeave(object sender, EventArgs e)
            {
                cross_hover.Hide();
            }

            private void max_idle_MouseEnter(object sender, EventArgs e)
            {
                max_hover.Show();
            }

            private void max_hover_MouseLeave(object sender, EventArgs e)
            {
                max_hover.Hide();
            }

            private void min_idle_MouseEnter(object sender, EventArgs e)
            {
                min_hover.Show();
            }

            private void min_hover_MouseLeave(object sender, EventArgs e)
            {
                min_hover.Hide();
            }

            private void cross_hover_Click(object sender, EventArgs e)
            {
                this.ParentForm.Close();
            }


  }
}    

设计师代码:

namespace WindowsFormsApplication1
{
    partial class Title_Bar
    {
        /// <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()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Title_Bar));
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.pictureBox2 = new System.Windows.Forms.PictureBox();
            this.pictureBox3 = new System.Windows.Forms.PictureBox();
            this.cross_idle = new System.Windows.Forms.PictureBox();
            this.max_idle = new System.Windows.Forms.PictureBox();
            this.max_hover = new System.Windows.Forms.PictureBox();
            this.min_idle = new System.Windows.Forms.PictureBox();
            this.min_hover = new System.Windows.Forms.PictureBox();
            this.cross_hover = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.cross_idle)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.max_idle)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.max_hover)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.min_idle)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.min_hover)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.cross_hover)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            this.pictureBox1.Location = new System.Drawing.Point(49, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(272, 10);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // pictureBox2
            // 
            this.pictureBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
            this.pictureBox2.Location = new System.Drawing.Point(318, 0);
            this.pictureBox2.Name = "pictureBox2";
            this.pictureBox2.Size = new System.Drawing.Size(50, 10);
            this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.pictureBox2.TabIndex = 1;
            this.pictureBox2.TabStop = false;
            // 
            // pictureBox3
            // 
            this.pictureBox3.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox3.Image")));
            this.pictureBox3.Location = new System.Drawing.Point(0, 0);
            this.pictureBox3.Name = "pictureBox3";
            this.pictureBox3.Size = new System.Drawing.Size(50, 10);
            this.pictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.pictureBox3.TabIndex = 2;
            this.pictureBox3.TabStop = false;
            // 
            // cross_idle
            // 
            this.cross_idle.Image = ((System.Drawing.Image)(resources.GetObject("cross_idle.Image")));
            this.cross_idle.Location = new System.Drawing.Point(358, 0);
            this.cross_idle.Name = "cross_idle";
            this.cross_idle.Size = new System.Drawing.Size(10, 10);
            this.cross_idle.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.cross_idle.TabIndex = 3;
            this.cross_idle.TabStop = false;
            this.cross_idle.MouseEnter += new System.EventHandler(this.cross_idle_MouseEnter);
            // 
            // max_idle
            // 
            this.max_idle.Image = ((System.Drawing.Image)(resources.GetObject("max_idle.Image")));
            this.max_idle.Location = new System.Drawing.Point(349, 0);
            this.max_idle.Name = "max_idle";
            this.max_idle.Size = new System.Drawing.Size(10, 10);
            this.max_idle.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.max_idle.TabIndex = 4;
            this.max_idle.TabStop = false;
            this.max_idle.MouseEnter += new System.EventHandler(this.max_idle_MouseEnter);
            // 
            // max_hover
            // 
            this.max_hover.Image = ((System.Drawing.Image)(resources.GetObject("max_hover.Image")));
            this.max_hover.Location = new System.Drawing.Point(267, 0);
            this.max_hover.Name = "max_hover";
            this.max_hover.Size = new System.Drawing.Size(10, 10);
            this.max_hover.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.max_hover.TabIndex = 5;
            this.max_hover.TabStop = false;
            this.max_hover.Visible = false;
            this.max_hover.MouseLeave += new System.EventHandler(this.max_hover_MouseLeave);
            // 
            // min_idle
            // 
            this.min_idle.Image = ((System.Drawing.Image)(resources.GetObject("min_idle.Image")));
            this.min_idle.Location = new System.Drawing.Point(340, 0);
            this.min_idle.Name = "min_idle";
            this.min_idle.Size = new System.Drawing.Size(10, 10);
            this.min_idle.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.min_idle.TabIndex = 6;
            this.min_idle.TabStop = false;
            this.min_idle.MouseEnter += new System.EventHandler(this.min_idle_MouseEnter);
            // 
            // min_hover
            // 
            this.min_hover.Image = ((System.Drawing.Image)(resources.GetObject("min_hover.Image")));
            this.min_hover.Location = new System.Drawing.Point(237, 0);
            this.min_hover.Name = "min_hover";
            this.min_hover.Size = new System.Drawing.Size(10, 10);
            this.min_hover.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.min_hover.TabIndex = 7;
            this.min_hover.TabStop = false;
            this.min_hover.Visible = false;
            this.min_hover.MouseLeave += new System.EventHandler(this.min_hover_MouseLeave);
            // 
            // cross_hover
            // 
            this.cross_hover.Image = ((System.Drawing.Image)(resources.GetObject("cross_hover.Image")));
            this.cross_hover.Location = new System.Drawing.Point(302, 0);
            this.cross_hover.Name = "cross_hover";
            this.cross_hover.Size = new System.Drawing.Size(10, 10);
            this.cross_hover.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.cross_hover.TabIndex = 8;
            this.cross_hover.TabStop = false;
            this.cross_hover.Visible = false;
            this.cross_hover.Click += new System.EventHandler(this.cross_hover_Click);
            this.cross_hover.MouseLeave += new System.EventHandler(this.cross_hover_MouseLeave);
            // 
            // Title_Bar
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.cross_hover);
            this.Controls.Add(this.min_hover);
            this.Controls.Add(this.min_idle);
            this.Controls.Add(this.max_hover);
            this.Controls.Add(this.max_idle);
            this.Controls.Add(this.cross_idle);
            this.Controls.Add(this.pictureBox3);
            this.Controls.Add(this.pictureBox2);
            this.Controls.Add(this.pictureBox1);
            this.Name = "Title_Bar";
            this.Size = new System.Drawing.Size(368, 10);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.cross_idle)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.max_idle)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.max_hover)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.min_idle)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.min_hover)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.cross_hover)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.PictureBox pictureBox2;
        private System.Windows.Forms.PictureBox pictureBox3;
        private System.Windows.Forms.PictureBox cross_idle;
        private System.Windows.Forms.PictureBox max_idle;
        private System.Windows.Forms.PictureBox max_hover;
        private System.Windows.Forms.PictureBox min_idle;
        private System.Windows.Forms.PictureBox min_hover;
        private System.Windows.Forms.PictureBox cross_hover;
    }
}

我已放入所有图片框,但没有任何反应!

1 个答案:

答案 0 :(得分:1)

设计器文件中的

this.cross_hover.Visible设置为false,调用Show不会更改该值。因此,PictureBox永远不会显示。

不要拨打Show(),而是将Visible设置为true

private void cross_idle_MouseEnter(object sender, EventArgs e)
{
    this.cross_hover.Visible = true;
}