0

I am writing WinForm application in C# .NET and I need to add dashed/dotted or any other type of border to any of UI components of application when the user clicks on it. I would like to get something like WinForm GUI editor in Visual Studio.

I am new in .NET so I don't know well what is possible via native methods and properties and what I need to implement myself. I have tried to find something on the net and here but I am not sure what to search, there are different approaches. For example it is possible to draw the border artificially, I mean using graphics. But I guess there should be easier approach.

What can you advice? What is the best practice in this situation? Please provide some portions of code.

haynar
  • 5,961
  • 7
  • 33
  • 53
  • [This](http://stackoverflow.com/questions/42460/custom-titlebars-chrome-in-a-winforms-app) is very close to what you want. – mbm Aug 28 '12 at 07:09
  • Do you need the border on UI elements of your application or on any application? – Oliver Aug 28 '12 at 07:10
  • @Oliver on my own application – haynar Aug 28 '12 at 07:17
  • @mbm that post is about the window and not the controls inside. Do you mean that there isn't easier way than drawing to achieve what I want? – haynar Aug 28 '12 at 07:28
  • @haynar1658 I'm not sure if there is an easier way than drawing it your self. In your case, maybe it is possible to create a lightweight overlay control so you don't need to override all controls. – mbm Aug 28 '12 at 08:05
  • @mbm do you mean creating a transparent click-through panel element with custom drawings over the element? – haynar Aug 28 '12 at 08:08
  • @haynar1658 yes. Though, I haven't tried it myself. – mbm Aug 28 '12 at 08:10
  • @mbm thanks, I will wait for a day to see maybe there are another solutions, if not I will use that solution – haynar Aug 28 '12 at 08:11

1 Answers1

3

Every Control has a Paint event. You have to subscribe to this event and look into the given arguments. The sender is the current control that should be painted. You can cast it within your method to Control. Now you can check the control if it focused by checking control.Focused and if it is true simply do whatever you like within the graphics object of the PaintEventArgs. This can furthermore be encapsulated in an extension method which would make the usage fairly easy.

public static void DrawBorderOnFocused(this Control control)
{
    if(control == null) throw new ArgumentNullException("control");
    control.Paint += OnControlPaint;
}

public static void OnControlPaint(object sender, PaintEventArgs e)
{
    var control = (Control)sender;

    if(control.Focused)
    {
       var graphics = e.Graphics;
       var bounds = e.Graphics.ClipBounds;

       // ToDo: Draw the desired shape above the current control
       graphics.DrawLine(Pens.BurlyWood, new PointF(bounds.Left, bounds.Top), new PointF(bounds.Bottom, bounds.Right));
    }
}

The usage within the code would then be something like:

public MyClass()
{
    InitializeComponent();

    textBox1.DrawBorderOnFocused();
    textBox2.DrawBorderOnFocused();
}
Oliver
  • 43,366
  • 8
  • 94
  • 151
  • nice solution, but this seems working only for buttons, it doesn't work for example for text fields or trees – haynar Aug 28 '12 at 08:43
  • I guess `PaintEventArgs` is accessible only in `Paint` event? – haynar Aug 28 '12 at 08:47
  • 1
    @haynar1658: The event args: Yes. The graphics object could also be access through `control.CreateGraphics()` but you would end up in race conditions when the control would paint itself and when you would paint on it. – Oliver Aug 28 '12 at 09:57
  • is it possible to avoid such conditions but still use `CreateGraphics`? and one more question: as I understood `control.CreateGraphics()` makes possible drawings only in the control's area, does it mean that `form.CreateGraphics()` does the same for the whole form area? – haynar Aug 28 '12 at 10:45
  • I experimented a little bit with `CreateGraphics` and creating overlays and I came up with solution. I checking your answer as a solution as it helped me to understand what stands behind this events and graphics – haynar Aug 29 '12 at 14:30