views:

442

answers:

3

Hi,

i need a textbox with a bottom-line such like inputfields used in forms.

I had looked for a functionality like single border on bottom, or something like this. But i think the only way is to draw a single line in a textbox.

The following code doesn't work:

private void textEdit1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
    {
        if (sender is TextBox)
        {
            TextBox tmp = (TextBox)sender;
            Graphics g = CreateGraphics();
            Pen p = new Pen(System.Drawing.Color.Red, 8);
            g.DrawLine(p, tmp.Location.X, tmp.Location.Y, (tmp.Location.X + tmp.Width), tmp.Location.Y);
            p.Dispose();
            g.Dispose();
        }
    }

Hope someone could help! Thanks!

A: 

How about using a RichtextBox control and underlining the text?

Shoban
+2  A: 

Please look at Owner-drawing a Windows.Forms TextBox article which describes customization process.

sashaeve
Thanks this works!
A: 

I suppose it's because the TextBox is really drawn by the OS itself. You'd have to subclass TextBox and handle WM_PAINT messages (possibly also WM_NC_PAINT for the borders).

EDIT
sash's answer provides a link that shows you how to do what I said here.

EDIT 2
For your sample case, would it be sufficient to set Border to none and add the TextBox to a panel? You could then paint the panel to have a bottom line and as long as the TextBox does not fill the panel, it would also be visible. This, however, only works for single-line TextBoxes.

Thorsten Dittmar