如何获取自定义按钮的文本和图像属性?

时间:2017-07-20 05:26:03

标签: c# winforms constructor

我创建了一个继承自Button类的自定义按钮。该按钮应该包含以下内容,

它应该是固定大小。(我完成了它,它工作正常) 根据控件大小将文本和图像对齐到控件。 我需要将此控件用于多个应用程序。基于应用程序用户 为按钮提供文本和图像值。

C#

Here, there is four constructor,


public customButton()
        {
            InitializeComponent();
        }


        /// <summary>
        /// This constructor will call when button contains Text property only.
        /// call the base constructor with Image is null.
        /// </summary>
        /// <param name="text"></param>
        public customButton(string text)
        {
            //customButton(text, null);
            InitializeComponent();
        }


        /// <summary>
        /// This constructor will call when button contains Image property only.
        /// call the base constructor with text is null.
        /// </summary>
        /// <param name="icon"></param>
        public customButton(Image icon)
        {
            //customButton(null, icon); //How to call the constructor 
            InitializeComponent();
        }


        /// <summary>
        /// This constructor will call when button contains both image and text values.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="icon"></param>
        public customButton(string text, Image icon)
        {
            InitializeComponent();
            if (icon != null)
            {
                iconAvailable = true;
                this.Image = icon; //Here I Need to get the icon value here,                }
            if (text != string.Empty || text != null)
            {
                textAvailable = true;
                this.Text = text;// // Here Need to get the text value here, Not completed
            }

            //align icon and text on booleans
            setControlAlignment();
        }

如何获取按钮的文本和图像值,基于该值将调用构造函数。

编辑:

如果按钮仅包含文本,则文本应位于中间中心,如果按钮仅包含图像,则图像应位于中间中心。所以我需要根据我需要调用构造函数来检查文本和图像的可用性。如何获取图像和文本值?在构造函数调用中

2 个答案:

答案 0 :(得分:0)

有一种特殊的语法可以做到这一点。需要从专门的构造函数中删除对InitializeComponent的调用,否则将调用两次:

        public customButton() : this(null, null)
        {
        }


        /// <summary>
        /// This constructor will call when button contains Text property only.
        /// call the base constructor with Image is null.
        /// </summary>
        /// <param name="text"></param>
        public customButton(string text) : this(text, null)
        {
        }


        /// <summary>
        /// This constructor will call when button contains Image property only.
        /// call the base constructor with text is null.
        /// </summary>
        /// <param name="icon"></param>
        public customButton(Image icon) : this(null, icon)
        {
        }


        /// <summary>
        /// This constructor will call when button contains both image and text values.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="icon"></param>
        public customButton(string text, Image icon)
        {
            InitializeComponent();
            if (icon != null)
            {
                iconAvailable = true;
                this.Image = icon; //Here I Need to get the icon value here,                }
            if (text != string.Empty || text != null)
            {
                textAvailable = true;
                this.Text = text;// // Here Need to get the text value here, Not completed
            }

            //align icon and text on booleans
            setControlAlignment();
        }

答案 1 :(得分:0)

为什么不创建私有变量来存储传递给构造函数的值,然后您可以在以后使用它们...

即。

    private string _text;
private Image _icon;

public customButton() : this(null, null)
    {
    }


    /// <summary>
    /// This constructor will call when button contains Text property only.
    /// call the base constructor with Image is null.
    /// </summary>
    /// <param name="text"></param>
    public customButton(string text) : this(text, null)
    {

    }


    /// <summary>
    /// This constructor will call when button contains Image property only.
    /// call the base constructor with text is null.
    /// </summary>
    /// <param name="icon"></param>
    public customButton(Image icon) : this(null, icon)
    {

    }


    /// <summary>
    /// This constructor will call when button contains both image and text values.
    /// </summary>
    /// <param name="text"></param>
    /// <param name="icon"></param>
    public customButton(string text, Image icon)
    {
        InitializeComponent();

    _text = this.text;
        _icon = this.icon;

        if (icon != null)
        {
            iconAvailable = true;
            this.Image = this.icon; //Here I Need to get the icon value here,                }
        if (text != string.Empty || this.text != null)
        {
            textAvailable = true;
            this.Text = this.text;// // Here Need to get the text value here, Not completed
        }

        //align icon and text on booleans
        setControlAlignment(this.text, this.icon);
    }

    private void setControlAlignment(string text, Image icon);
    {
       HorizontalAlignment textPos;
       HorizontalAlignment iconPos;

       if (!string.IsNullOrWhiteSpace(text) && this.icon != null)       // If we have both text and icon values
       {
    textPos = HorizontalAlignment.Left;
            iconPos = HorizontalAlignment.Right;
       }
   else if (string.IsNullOrWhiteSpace(this.text) && this.icon != null)
   {
    iconPos = HorizontalAlignment.Center;
       }
   else if (!string.IsNullOrWhiteSpace(this.text) && this.icon == null)
   {
    textPos = HorizontalAlignment.Center;
       }

   // and whatever else you want to do...
    }

线条: this.Image = this.icon; //这里我需要获取图标值, 和 this.Text = this.text; // //这里需要获取文本值,未完成

应该已经作为“text”工作,“icon”将设置为传入的值。

public customButton(字符串文字,图片图标) 所有构造函数都在调用它,所以如果你想在代码中的其他方法中访问它们,我只需要设置一次_text和_icon。

如果您需要,如果您需要在其他位置访问这些变量(当前在setControlAlignment中定义),则可以对位置执行相同操作,即make _textPos和_iconPos全局。

代码不使用_text或_icon(已分配但从未使用过) - 因为我将值传递给setControlAlignment方法。由于您尚未在此处输入完整代码,因此如果您确实需要在其他方法中访问这些值,则只需访问_text或_icon。