如何自动检测我正在使用的控制油漆事件?

时间:2013-12-26 09:04:24

标签: c# winforms

我有例如Form1绘制事件:

private void Form1_Paint(object sender, PaintEventArgs e)
{
     TextDrawer draw = new TextDrawer(e.Graphics,this,8.25);
}

在新类TextDrawer中,我需要输入控件名称,在这种情况下是form1,所以我输入:

这是新课程:

class TextDrawer
{
        private readonly Graphics g;
        private readonly Control c ;
        private readonly double font_size;

        public TextDrawer(Graphics g,Control c,
                          double font_size)
        {
            this.g = g;
            this.c = c;
            this.font_size = font_size;
        }

        public void DrawText(string text,Color pen_color,Color brushes_color, Point point1, Point point2, Point point3)
        {
            c.Font = new Font(c.Font.FontFamily.Name, (float)font_size);
            SolidBrush brush = new SolidBrush(brushes_color);
            using (Pen pen = new Pen(pen_color, 10f))
            {
                Point pt1 = point1;
                Point pt2 = point2;
                g.DrawLine(pen, point1, point2);
            }

            g.DrawString(text,
                    c.Font, brush, point3);
        }
    }

我想以某种方式做出一旦我在任何控件的paint事件中键入它的form1 pictureBox1标签任何具有paint事件的控件,一旦我为该类创建一个新实例,例如:

TextDrawer draw = new TextDrawer(e.Graphics,8.25);

新类将检测/查找自动控件名称,以便用户无需键入:this或pictureBox1或label1 ...

有什么办法吗?

1 个答案:

答案 0 :(得分:2)

例如,在TextBox的TextChanged事件中,您可以将sender参数作为控件传递。

private void YourTextBoxOne_TextChanged(object sender, EventArgs e)
{
    TextBoxTextChanged((TextBox)sender);
}

private void YourTextBoxTwo_TextChanged(object sender, EventArgs e)
{
    TextBoxTextChanged((TextBox)sender);
}

private void TextBoxTextChanged(TextBox tb)
{
    var draw = new TextDrawer(tb, 8.25);
    //Do something
}