0

我是 c# 新手,我正在尝试创建一个 ButtonImage 控件。我已经设法让大部分代码工作,包括 TextAlignment 和图像资源选择,但图像不会显示。我尝试添加 TextImageRelation 属性以及 ImageAlignment 属性无效 - 甚至不确定我是否已正确完成所有操作。我花了几个小时搜索 MSDN 和互联网,请帮助,tnx。继承人的代码:

namespace ImageButton
{
    //[System.ComponentModel.DefaultBindingProperty("ButtonText")]
    public partial class ImageButton : UserControl
    {
        private String name = "btn1";
        private String btnText = "Button1";
        private TextImageRelation textImage = TextImageRelation.Overlay;
        private ContentAlignment alignmentValue = ContentAlignment.MiddleRight;
        private ContentAlignment imageAlignmentValue = ContentAlignment.MiddleLeft;

        public ImageButton()
        {
            InitializeComponent();
        }
        //
        // Properties
        //
        [Description("Sets the Text Label"),
         Category("Custom")]
        public String ButtonText
        {
            get
            {
                return btnText;
            }
            set
            {
                btnText = value;
                btn1.Text = btnText;
            }
        }
        [Description("Sets the Button Image"),
         Category("Custom")]
        public Image Image
        {
            get;
            set;
        }
        [Description("Specifies the relationship of text to Image."),
         Category("Custom")]
        public TextImageRelation TextImageRelation { 
            get{
                return textImage;
            }
            set{
                textImage =value;
                btn1.TextImageRelation = textImage;
            }
        }
        [Category("Custom"),
         Description("Specifies the alignment of text.")]
        public ContentAlignment TextAlignment
        {
            get
            {
                return alignmentValue;
            }
            set
            {
                alignmentValue = value;
                btn1.TextAlign = alignmentValue;
                //Invalidate();
            }
        }
        [Category("Custom"),
         Description("Specifies the alignment of text.")]
        public ContentAlignment ImageAlignment
        {
            get
            {
                return imageAlignmentValue;
            }
            set
            {
                imageAlignmentValue = value;
                btn1.ImageAlign = imageAlignmentValue;
            }
        }    
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            StringFormat style = new StringFormat();
            style.Alignment = StringAlignment.Far;
            switch (alignmentValue)
            {
                case ContentAlignment.MiddleLeft:
                    style.Alignment = StringAlignment.Near;
                    break;
                case ContentAlignment.MiddleRight:
                    style.Alignment = StringAlignment.Far;
                    break;
                case ContentAlignment.MiddleCenter:
                    style.Alignment = StringAlignment.Center;
                    break;
            }
            // Call the DrawString method of the System.Drawing class to write   
            // text. Text and ClientRectangle are properties inherited from
            // Control.
            e.Graphics.DrawString(
                Text,
                Font,
                new SolidBrush(ForeColor),
                ClientRectangle, style);
        }
    }
}
4

1 回答 1

0

解决了:

代替:

    public Image Image{ get; set;}

正如其他人所建议的那样,我使用了

    public Image Image{
        get { return image; }
        set
        {
            image = value;
            btn1.Image = image;
        }
    }
于 2012-08-03T05:01:28.123 回答