【问题标题】:How can I add a property to a UserControl?如何将属性添加到 UserControl?
【发布时间】:2016-05-07 13:24:59
【问题描述】:

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

整个想法是制作一个 UserControl 来在它上面拉绳,这样它就不会闪烁。如果我在 form1 绘制事件中拉绳,则使用计时器时文本会闪烁。

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

替换 label1.Text 并将变量字体替换为全局字体,以便用户可以在 form1 设计器中拖动控件后将其设置为。 因此,在 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);
        }
    }
}

【问题讨论】:

  • 好的,我找到了方法。现在正是我想要的,但它正在工作。现在将用我到目前为止所做的事情更新我的问题。
  • 请从问题中删除答案部分并将其作为答案发布。这样,问题将更具可读性,答案将对未来的读者更有用。

标签: c# .net winforms


【解决方案1】:

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

在 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 和字体变量一起使用。

【讨论】:

    【解决方案2】:

    在您的PaintDrawingControl 类中使用实例属性(未标有static 关键字):

    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";
    

    【讨论】:

      猜你喜欢
      • 2014-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      相关资源
      最近更新 更多