将标签添加到图片框控制后,事件不会触发-C#

时间:2018-11-12 23:45:48

标签: c# events label picturebox invoke

我在创建一个从PictureBox继承的类时出现了奇怪的行为;

该类的行为类似于按钮(基本上我关心的是鼠标进入,鼠标离开,鼠标按下,鼠标向上事件)。

一切正常,直到我将TextLabel(下面的代码)添加到自定义类的控件中为止。它显示标签,居中和我想要的所有内容,但是我之前提到的事件(以及该事件中的其他所有事件)被禁用,由于某种原因,它不会触发它们。

想知道这种行为的原因是什么,以及是否有解决办法。

public class RoundedButton : PictureBox
{
    private readonly Image r_BasicImage =  RoundedButtonCheck2.Properties.Resources.basicRoundedButtonIcon as Image;
    private readonly Image r_HoverImage = RoundedButtonCheck2.Properties.Resources.hoverRoundedButtonIcon as Image;
    private readonly Image r_ClickImage = RoundedButtonCheck2.Properties.Resources.clickRoundedButtonIcon as Image;

    private string m_Text;
    private Font m_Font;
    public Color m_TextColor;
    private Label LabelText = new Label();


    public RoundedButton()
    {
        this.Width = 130;
        this.Height = 40;
        this.Image = r_BasicImage;
        this.BackColor = Color.Transparent;
        this.SizeMode = PictureBoxSizeMode.StretchImage;
        this.MouseDown += RoundedButton_MouseDown;
        this.MouseUp += RoundedButton_MouseUp;
        this.MouseEnter += RoundedButton_MouseEnter;
        this.MouseLeave += RoundedButton_MouseLeave;
        LabelText.Font = ButtonFont;
        ButtonTextColor = Color.Black;
        //PROBLEMATIC CODE HERE:
        ***********this.Controls.Add(LabelText);***************
    }

    public Color ButtonTextColor
    {
        get
        {
            return m_TextColor;
        }
        set
        {
            m_TextColor = value;
            LabelText.ForeColor = m_TextColor;
        }
    }
    public Font ButtonFont
    {
        get
        {
            if (m_Font == null)
            {
                m_Font = new Font("Calibri Light", 12);
            }
            return m_Font;
        }
        set
        {
            m_Font = value;
            LabelText.Font = ButtonFont;
            adjustLabel();
        }
    }

    public string ButtonText
    {
        get
        {
            return m_Text;
        }
        set
        {

            m_Text = value;
            LabelText.Text = m_Text;
            adjustLabel();
        }
    }
    private void adjustLabel()
    {
        const int MARGIN = 10;
        LabelText.AutoSize = true;//needed for autosize calculation of the label;
        Size newSize = new Size(LabelText.Size.Width, LabelText.Size.Height); ;
        LabelText.AutoSize = false;//after auto-calculated size of the label, set to false in order for centering label works well.
        this.MinimumSize = newSize;
        this.Size = new Size(newSize.Width + MARGIN, newSize.Height + MARGIN);
        LabelText.TextAlign = ContentAlignment.MiddleCenter;
        LabelText.Dock = DockStyle.Fill;
    }

    private void RoundedButton_MouseLeave(object sender, EventArgs e)
    {
        RoundedButton hoveredButton = sender as RoundedButton;
        if (hoveredButton != null)
        {
            hoveredButton.Image = r_BasicImage;
            hoveredButton.SizeMode = PictureBoxSizeMode.StretchImage;
        }
    }

    private void RoundedButton_MouseEnter(object sender, EventArgs e)
    {
        RoundedButton hoveredButton = sender as RoundedButton;
        if (hoveredButton != null)
        {
            hoveredButton.Image = r_HoverImage;
            hoveredButton.SizeMode = PictureBoxSizeMode.StretchImage;
        }
    }

    private void RoundedButton_MouseUp(object sender, MouseEventArgs e)
    {
        RoundedButton clickedButton = sender as RoundedButton;
        if (clickedButton != null)
        {
            clickedButton.Image = r_BasicImage;
            clickedButton.SizeMode = PictureBoxSizeMode.StretchImage;

        }
    }

    private void RoundedButton_MouseDown(object sender, MouseEventArgs e)
    {
        RoundedButton clickedButton = sender as RoundedButton;
        if (clickedButton != null)
        {
            clickedButton.Image = r_ClickImage;
            clickedButton.SizeMode = PictureBoxSizeMode.StretchImage;

        }
    }
}

感谢先进的碳粉。

0 个答案:

没有答案