user3117033
user3117033

Reputation: 195

How can I automatically detect what control paint event I'm using?

I have for example Form1 paint event:

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

In the new class TextDrawer I need to enter the control name in this case its form1 so I type:

This is the new class:

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

I want to make somehow that once i type inside a paint event of any control is its form1 pictureBox1 label any control that have a paint event once i make a new instance for the class for example:

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

And the new class will detect/find automatic the control name so the user wont need to type in: this or pictureBox1 or label1...

Is there any way to do it ?

Upvotes: 0

Views: 143

Answers (1)

Abbas
Abbas

Reputation: 14432

For example in the TextChanged event of the TextBox you can pass the sender argument as the Control.

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
}

Upvotes: 2

Related Questions