如何向UserControl添加属性?

时间:2016-05-07 13:25:00

标签: c# .net winforms

当我稍后将此UserControl拖到我的form1设计器时,我希望能够键入例如PaintDrawingControl1。并且在具有所有属性的点之后还有属性字体和文本而不是那些已经存在但是我需要在代码中而不是font和label1.Text所以用户可以输入任何文本到抽象字符串和任何类型的字体。 我的意思是在这一行:PaintDrawingControl.font = ....或PaintDrawingControl.text =“绘制此文本”

整个想法是让UserControl对它进行抽绳,这样它就不会闪烁。如果我在form1 paint事件中抽取字符串,则在使用计时器时文本将闪烁。

e.Graphics.DrawString(label1.Text,
                font, Brushes.Black, 10, 10);

要替换label1.Text并将变量字体替换为全局变量,以便用户可以在form1 designer中拖动控件后将其设置为。 所以在form1代码中我将能够做到例如:

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 DateCounter
{
    public partial class PaintDrawingControl : UserControl
    {
        public PaintDrawingControl()
        {
            InitializeComponent();
        }

        private void PaintDrawingControl_Load(object sender, EventArgs e)
        {

        }

        private void PaintDrawingControl_Paint(object sender, PaintEventArgs e)
        {
            if (DesignMode) return;

            e.Graphics.DrawString(label1.Text,
            font, Brushes.Black, 10, 10);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

这是我到目前为止所做的:

在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 DateCounter
{
    public partial class PaintDrawingControl : UserControl
    {
        public static string texttodraw;
        public static Font font;

        public PaintDrawingControl()
        {
            InitializeComponent();
        }

        private void PaintDrawingControl_Load(object sender, EventArgs e)
        {

        }

        private void PaintDrawingControl_Paint(object sender, PaintEventArgs e)
        {
            if (DesignMode) return;

            e.Graphics.DrawString(texttodraw,
            font, Brushes.Black, 10, 10);
        }
    }
}

添加了两个全局公共静态变量,一个用于texttodraw,另一个用于字体。

在form1方面,我将其更改为与计时器一起使用:

private void CounterToDate()
        {
            endTime = new DateTime(2016, 10, 21, 0, 0, 0);
            Timer t = new Timer();
            t.Interval = 500;
            t.Tick += new EventHandler(t_Tick);
            TimeSpan ts = endTime.Subtract(DateTime.Now);
            PaintDrawingControl.texttodraw = ts.ToString("d' Days 'h' Hours 'm' Minutes 's' Seconds'");
            paintDrawingControl1.Invalidate();
            t.Start();

        }

        void t_Tick(object sender, EventArgs e)
        {
            TimeSpan ts = endTime.Subtract(DateTime.Now);
            PaintDrawingControl.texttodraw = ts.ToString("d' Days 'h' Hours 'm' Minutes 's' Seconds'");
            paintDrawingControl1.Invalidate();
        }

到目前为止,我没有看到任何闪烁。 到目前为止,它正在发挥作用。

如果我可以将其称为问题,唯一的问题是在form1中我使用:

PaintDrawingControl设置texttodraw和字体变量,我想使用paintDrawingControl1所以我想知道如何做到这一点只是为了学习如何使用paintDrawingControl1和texttodraw以及字体变量。

答案 1 :(得分:0)

static课程中使用实例属性(未标有PaintDrawingControl关键字):

public partial class PaintDrawingControl : UserControl
{
    public string TextToDraw { get; set; }

    // You do not need the define a Font property.
    // As it is already defined in 'Control' class

    // UserControl -> ContainerControl -> ScrollableControl -> Control

    //  Here comes the rest.
}

现在您可以使用控件实例的属性:

paintDrawingControl1.Font = System.Drawing.SystemFonts.GetFontByName("Arial");
paintDrawingControl1.TextToDraw = "Something";