user682417
user682417

Reputation: 1518

changing the text box border style in windows form - c#

I have a text box and it is in square form now I want to convert that square into oval shape i am using win forms application

can any one tell any idea about this

Upvotes: 1

Views: 8305

Answers (4)

alisabzevari
alisabzevari

Reputation: 8126

You can use the SetWindowRgn API Function to change the shape of a window. This function - as you can see here - gets three arguments:

  1. Window Handle: That can be your TextBox Handle and you can get it by Handle Property.
  2. A Window RGN: That you can create it by calling CreateRoundRectRgn (or another RGN creator functions that you can find them here)
  3. A Boolean to determine Redraw: That it is better to be true.

You can subclass from TextBox and create a Oval shaped TextBox by using this functions in OnHandleCreated Method. The class can be something like this:

class OvalTextBox : TextBox
{
    [DllImport("user32.dll")]
    static extern int SetWindowRgn(IntPtr hWnd, IntPtr hRgn, bool bRedraw);

    [DllImport("gdi32.dll")]
    static extern IntPtr CreateRoundRectRgn(int x1, int y1, int x2, int y2, int cx, int cy);

    public OvalTextBox()
    {
        base.BorderStyle = System.Windows.Forms.BorderStyle.None;
    }

    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        SetWindowRgn(this.Handle, CreateRoundRectRgn(0, 0, this.Width, this.Height, 20, 20), true);
    }
}

Upvotes: 5

Icemanind
Icemanind

Reputation: 48686

If you are doing this using Windows Forms, the only way is to create a new user control and inherit your control from TextBox. Then you must override the OnPaint method and implement your own drawing code.

IF its possible for you to use WPF though, its much easier. If you can make your application a WPF application, then all you need to do is drop a textbox onto your form and then put in a custom template for the shape.

Upvotes: 0

agent-j
agent-j

Reputation: 27913

Use the OvalShape control and put a textbox inside it. The textbox should have an an invisible.

Alternatively, create a customcontrol and override OnPaint so that it draws the oval with graphics.DrawElipse.

Upvotes: 1

hungryMind
hungryMind

Reputation: 6999

create ur class inherited by textbox, override OnPaint. Use your custom textbox in winforms.

Upvotes: 1

Related Questions