There is a method that creates text fields with specified parameters.

private void SetTextBox(int positionX, int positionY, string txtText) { TextBox txt = new TextBox(); txt.Location = new Point(positionX, positionY); txt.Size = new Size(200, 15); txt.BorderStyle = BorderStyle.None; txt.ForeColor = Color.FromArgb(176, 178, 183); txt.BackColor = Color.FromArgb(240, 240, 240); FontFamily fontFamaly = new FontFamily(System.Drawing.Text.GenericFontFamilies.SansSerif); txt.Font = new Font("Verdana", 16.5f, FontStyle.Italic); Pen pen = new Pen(Color.FromArgb(57,54,56)); txt.CreateGraphics().DrawLine(pen, Convert.ToSingle(positionX), Convert.ToSingle(positionY + txt.Size.Height), Convert.ToSingle(positionX + txt.Size.Width), Convert.ToSingle(positionY + txt.Size.Height)); txt.Text = txtText; Controls.Add(txt); } 

In simple words, it is a text field that coincides in color with the color of the form window and contains an informational text on it. You need to draw a line on the lower border of the Textbox, above which will be this information text. I tried to draw immediately using

 txt.CreateGraphics().DrawLinе... 

but nothing comes out (the line does not appear).

Can anyone give good advice?

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

The line appears, but is immediately overwritten.

You need to paint on the surface of the controls in the Paint event - then with each redraw everything will be displayed anew.

The problem is that this event does not work for the textbox.

Judging by the words "informational text" - it will not be edited by the user. I propose to use Label instead of TextBox, since this event works for it.

 private void SetLabel(int x, int y, string text) { var label = new Label(); label.Location = new Point(x, y); label.Size = new Size(200, 15); label.ForeColor = Color.FromArgb(176, 178, 183); label.Font = new Font("Verdana", 16.5f, FontStyle.Italic); label.Text = text; Controls.Add(label); label.Paint += Label_Paint; } 

By default, the label has no border, so you can omit None . The color of its background defaults to the color of the form, so you can also omit it.

And the event handler:

 private void Label_Paint(object sender, PaintEventArgs e) { var label = (Label)sender; Pen pen = new Pen(Color.FromArgb(57, 54, 56)); e.Graphics.DrawLine(pen, 0, label.Height - 1, label.Width, label.Height - 1); } 

If, however, the Label not suitable, since the user is supposed to edit the text, write, think.


We do this: put a panel on the form, and already in it a textbox. Draw the lower border on this panel. The height of the panel, respectively, should be slightly larger.

 private void SetTextBox(int x, int y, string text) { var textBox = new TextBox(); textBox.BorderStyle = BorderStyle.None; textBox.Size = new Size(200, 25); textBox.ForeColor = Color.FromArgb(176, 178, 183); textBox.BackColor = SystemColors.Control; textBox.Font = new Font("Verdana", 16.5f, FontStyle.Italic); textBox.Text = text; var panel = new Panel(); panel.Location = new Point(x, y); panel.Size = new Size(200, 28); panel.Controls.Add(textBox); Controls.Add(panel); panel.Paint += Panel_Paint; } private void Panel_Paint(object sender, PaintEventArgs e) { var panel = (Panel)sender; Pen pen = new Pen(Color.FromArgb(57, 54, 56)); e.Graphics.DrawLine(pen, 0, panel.Height - 1, panel.Width, panel.Height - 1); } 

Here's a similar question: Border on TextBox

  • Yes, Label doesn’t fit, because I’ll still edit the field. - Roman Timokhov
  • @RomanTimokhov - made another option. - Alexander Petrov