Nighil
Nighil

Reputation: 4129

Border for Dynamically created buttons in Windows Application C#.net

Hai all, I am dynamically creating button at runtime in a c#.net Windows Application, I want to draw a border for the dynamically created button how can i do it, i tried this

 private void DrawBorder(Button bt)
        {
            Graphics g = bt.CreateGraphics();
            ControlPaint.DrawBorder(g, bt.DisplayRectangle, Color.FromArgb(229, 227, 227), ButtonBorderStyle.Solid);
            g.Dispose();
        }

but it is not showing any error but i can't see any border

Please Help

Upvotes: 0

Views: 1062

Answers (1)

Ran
Ran

Reputation: 6159

If you want a button that draws differently, you should probably create a new class inheriting from Button, and in it you would override OnPaint and/or OnPaintBackground and implement your drawing logics, such as drawing a border.

The reason your current solution is not working probably depends on when you are calling your DrawBorder method. If your drawing code is not executed as part of handling an OnPaint event, then the graphics you draw will be drawn-over in the next paint.

But anyway, instead of drawing the border yourself, can't you set the Border proerpty on the Button object?

Upvotes: 2

Related Questions