简体   繁体   中英

Reuse eventhandler in multiple forms

I am using C# with Windows Forms. In several of my forms I have code such as this:

public myForm()
{
    InitializeComponent();
    groupBox1.Paint += new PaintEventHandler(groupBox_Paint);
    groupBox2.Paint += new PaintEventHandler(groupBox_Paint);            
}

Each of these forms have a groupBox_Paint method inside them. However, I would like these forms to reuse this method from one location so I don't have copies of it in different forms. I have tried making the method public static in a form and accessing it from the event handler but I received an error because of the code this.BackColor :

Keyword 'this' is not valid in a static property, static method, or static field initializer

What would be a good way of doing this? Thank you.

The code for groupBox_Paint and associated method is below:

public static void groupBox_Paint(object sender, PaintEventArgs e)
{
    GroupBox box = sender as GroupBox;
    DrawGroupBox(box, e.Graphics, Color.Black, Color.Black);
}

public static void DrawGroupBox(GroupBox box, Graphics g, Color textColor, Color borderColor)
{
    if (box != null)
    {
        Brush textBrush = new SolidBrush(textColor);
        Brush borderBrush = new SolidBrush(borderColor);
        Pen borderPen = new Pen(borderBrush);
        SizeF strSize = g.MeasureString(box.Text, box.Font);
        Rectangle rect = new Rectangle(box.ClientRectangle.X,
                                       box.ClientRectangle.Y + (int)(strSize.Height / 2),
                                       box.ClientRectangle.Width - 1,
                                       box.ClientRectangle.Height - (int)(strSize.Height / 2) - 1);

        // Clear text and border
        g.Clear(this.BackColor);

        // Draw text
        g.DrawString(box.Text, box.Font, textBrush, box.Padding.Left, 0);

        // Drawing Border
        //Left
        g.DrawLine(borderPen, rect.Location, new Point(rect.X, rect.Y + rect.Height));
        //Right
        g.DrawLine(borderPen, new Point(rect.X + rect.Width, rect.Y), new Point(rect.X + rect.Width, rect.Y + rect.Height));
        //Bottom
        g.DrawLine(borderPen, new Point(rect.X, rect.Y + rect.Height), new Point(rect.X + rect.Width, rect.Y + rect.Height));
        //Top1
        g.DrawLine(borderPen, new Point(rect.X, rect.Y), new Point(rect.X + box.Padding.Left, rect.Y));
        //Top2
        g.DrawLine(borderPen, new Point(rect.X + box.Padding.Left + (int)(strSize.Width), rect.Y), new Point(rect.X + rect.Width, rect.Y));
    }
}

The problem lies in this line

g.Clear(this.BackColor);

this is a reference to an instance of Form, it cannot be used inside a static method. You can pass the background of the form as a parameter.

public static void DrawGroupBox(GroupBox box, Graphics g, 
    Color textColor, Color borderColor, Color backgroundColor) 
{
    ...
    // Clear text and border
    g.Clear(backgroundColor);
    ...
}

And use it like this

private void groupBox_Paint(object sender, PaintEventArgs e)
{
    GroupBox box = sender as GroupBox;

    //assuming DrawGroupBox is defined in another class `CommonUtil`
    CommonUtil.DrawGroupBox(box, e.Graphics, Color.Black, Color.Black, this.BackColor);     
}

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