简体   繁体   中英

How can I add a property to a UserControl?

When I drag this UserControl later to my form1 designer I want to be able to type for example PaintDrawingControl1. and after the point to have with all the properties also the property font and text not those are already exist but this I need in the code instead font and label1.Text so the user can enter any text to drawstring and any type of font. I mean on this line: PaintDrawingControl.font =.... or PaintDrawingControl.text = "draw this text"

The whole idea is to make a UserControl to drawstring over it so it will not flicker. If I drawstring in form1 paint event the text will blink when using a timer.

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

To replace the label1.Text and to replace the variable font with something global so the user can set it to after dragging the control in form1 designer. So in the form1 code I will be able to do for example:

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);
        }
    }
}

This is what I did so far :

In the UserControl side:

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);
        }
    }
}

Added two global public static variables one for the texttodraw and one the font.

In form1 side I changed it to use it with the timer:

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();
        }

So far I don't see any flickers. And so far it's working.

The only problem if I can call it a problem is that in form1 I use the:

PaintDrawingControl to set the texttodraw and the font variables and I wanted to use the paintDrawingControl1 so I wonder how to do it just for learning how ot use also the paintDrawingControl1 with the texttodraw and the font variables.

Use instance properties (not marked with static keyword) in your PaintDrawingControl class:

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.
}

Now you can use the properties of the control instance:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM