4

In WinForms how do I draw a frame the same as in a System.Windows.Forms.TextBox control?

I tried to use VisualStyleRenderer with the VisualStyleElement.TextBox.TextEdit.Normal parameter but it draws a strange gray line.

VisualStyleElement.Button.PushButton.Normal for a button works correctly. It draws the face just as the button but for TextBox it draws a gray line border.

I used the following code:

private void panel1_Paint (object sender, PaintEventArgs e)
{
    VisualStyleRenderer vsr = new VisualStyleRenderer(VisualStyleElement.TextBox.TextEdit.Normal);
    vsr.DrawBackground (e.Graphics, new Rectangle (500, 12, 100, 20));

    vsr = new VisualStyleRenderer (VisualStyleElement.Button.PushButton.Normal);
    vsr.DrawBackground (e.Graphics, new Rectangle (500, 47, 100, 23));
}

Look at the screenshot.

On the left are Edit and Button controls.

On the right is what gets drawn by VisualStyleRenderer.DrawBackground

enter image description here

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
DmitryB
  • 455
  • 1
  • 5
  • 18
  • 3
    Try `var renderer = new VisualStyleRenderer("Edit", 6, 1);` followed by `renderer.DrawBackground(e.Graphics, rect);` See [TextBoxRenderer is just drawing gray rectangles](https://social.msdn.microsoft.com/Forums/windows/en-US/46e66852-b52e-420d-8fe0-b717f86ecb18/textboxrenderer-is-just-drawing-gray-rectangles?forum=winforms) – LarsTech Jun 16 '16 at 17:53
  • 1
    Use 6, 3 to get the blue border. – Hans Passant Jun 16 '16 at 17:59
  • @HansPassant Oh, wunderbar, or for you, would that be wonderlijk... – LarsTech Jun 16 '16 at 18:05
  • 1
    @Lars - Hehe, not quite, means "mystifying" in Dutch. Well, could be what you meant, how they fumbled VSR is kinda mystifying :) – Hans Passant Jun 16 '16 at 18:13
  • VisualStyleRenderer("Edit", 6, 1) works. Thank you Lars. – DmitryB Jun 17 '16 at 14:26

0 Answers0