0

The other solutions on this site haven't worked for me so I'm trying to figure out what I'm doing wrong:

This is WinForm App

private void dk_buff_box_Paint(object sender, PaintEventArgs e)
    {
        Console.WriteLine("INSIDE!!");
        ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Red, ButtonBorderStyle.Solid);
        GroupBox box = sender as GroupBox;
        DrawGroupBox(box, e.Graphics, Color.Red, Color.Black);
    }
    private 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));
        }
    }

This code never fires the "INSIDE!!" to my console output and the border around the groupbox (called dk_buff_box) is always grey and very light. (i'm presuming that is default??)

What do I need to do to get this border to change color? I have a couple .cs files that are working together (controls) The main .cs file is form1.cs. The code above is on a seperate .cs file called darkknightinfo.cs

Should the code be on the main form? or should it be on the .cs file that has the actual groupbox on it?

what do I need to do to get the _Paint() to properly activate and run the code to change the groupbox border color?

user
  • 6,897
  • 8
  • 43
  • 79
DerekConlon
  • 353
  • 1
  • 5
  • 17
  • Why don't you add the Border in WPF XAML? – Alexander Bell Mar 23 '16 at 15:35
  • Is it WPF or WinForm app? Please clarify. – Alexander Bell Mar 23 '16 at 15:42
  • WinForm App is what I am using – DerekConlon Mar 23 '16 at 15:49
  • Where do you subscribe something to `dk_buff_box_Paint` ? In designer? Check `form.Designer.cs` file if you are really subscribed with this event handler. If you override `OnPaint` somewhere else (e.g. custom control), then make sure to call `base.OnPaint` there, otherwise `Paint `event is never fired. – Sinatr Mar 23 '16 at 15:54
  • Put `dk_buff_box_Paint.Paint += dk_buff_box_Paint_Paint;` in your form's constructor. Your DrawBorder won't work because your are trying to draw on the form's client space but your graphic is only for the GroupBox. – LarsTech Mar 23 '16 at 16:02
  • @Sinatr I don't really understand what you just asked me. – DerekConlon Mar 23 '16 at 16:02
  • @LarsTech I tried to put that in both form1.cs and darkknightinfo.cs and it just red underlines and says "dk_buff_box_Paint is a method and its not valid in the given context" – DerekConlon Mar 23 '16 at 16:08
  • I can't see where you placed it. I specified the form's constructor, clearly you didn't put it in there. – LarsTech Mar 23 '16 at 16:10
  • public Form1() { InitializeComponent(); dkControl.dk_buff_box_Paint.Paint += dk_buff_box_Paint_Paint; Is that not the constructor? I apologize, I'm new to this. I also did: public darkknightinfo() { InitializeComponent(); dk_buff_box_Paint.Paint += dk_buff_box_Paint_Paint; } – DerekConlon Mar 23 '16 at 16:13
  • The dk_buff_box control is on the darkknightinfo.cs User Control that gets added to the ArcticMUD User Control which is added to a Panel on the Main Form1 application (these controls are added after it reads a certain text in a log file) So when you say "add it to the forms contstructor" i'm not sure what the constructor is but from what I've read I think its what I think it is and i'm not sure if you want it on the main form .cs file or the user control .cs file – DerekConlon Mar 23 '16 at 16:15
  • If the GroupBox is in a UserControl, then you have to use the UserControl's constructor. – LarsTech Mar 23 '16 at 16:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107155/discussion-between-derekconlon-and-larstech). – DerekConlon Mar 23 '16 at 16:18
  • See here for a description of [hooking up events!](http://stackoverflow.com/questions/33275763/copy-datagridview-values-to-textbox/33276161?s=1|0.1690#33276161) – TaW Mar 24 '16 at 08:33

1 Answers1

0

If you're making a winForm application the console must be opened programmatically, it can't be opened with just Console.WriteLine. You're also using PaintEventArgs which is used for (as the name implies) Paint, bitmap etc.

Remco1250
  • 85
  • 13
  • It is a winForm application. I am using PaintEventArgs (as you can see at the top of my code above) and Console.WriteLine is being used as it has been used all over my project and works fine. The problem is the event isn't being called and that's what my question is. – DerekConlon Mar 23 '16 at 15:46