Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Textbox padding

I've searched through the internet, I must be using the wrong keywords because I can't find anything. I want to create a textbox that has text starting from a little far from the left.

http://dab.biz/images/screenie/2011-02-04_1316.png

Just like that.

like image 292
dab Avatar asked Feb 04 '11 21:02

dab


People also ask

Can you change the margins inside a text box?

Adjust the text box margins Click the outer edge of the text box to select it. On the Format menu, click Shape. In the left pane of the Format Shape dialog box, click Text Box. Under Internal Margin, adjust the measurements to increase or decrease the distance between the text and the outer border of the text box.

How do I change the margins of a textbox in Word?

Right-click the selection rectangle of the shape or text box you want to change. On the shortcut menu, click Format <object type>, and then click the Text Box tab. Under Text Box Margins, adjust the measurements to increase or decrease the distance between the text and the outer border of the text box or a shape.

How do I change the margins of a textbox in Powerpoint?

Select a text box, right-click and select Format Shape. In the Formatting text pane, click the Text Options. Click the text box icon. Adjust the Left/Right/Top/Bottom margins exactly as needed.


2 Answers

As you have most likely discovered, Winforms Textboxes do not have a padding property. Since Panels do expose a Padding property, one technique would be to:

  1. Create a Panel
  2. Set its border to match a Textbox (e.g., Fixed3D)
  3. Set its background color to match a Textbox (e.g., White or Window)
  4. Set its padding to your satisfaction (e.g., 10,3,10,3)
  5. Add a Textbox inside the panel
  6. Set the Textbox's border to none
  7. Play with the Textbox's Dock and Anchor properties do get desired effect

This should get you started. You could also create a custom control that does the same thing as mentioned above.

In case you were talking about Textboxes in asp.net, just use CSS:
input[type="text"] {padding: 3px 10px}

like image 146
Nimrod Avatar answered Sep 22 '22 19:09

Nimrod


OK, here is a proper solution. First of all set Multiline of your TextBox control to true.

Needed using statements:

using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; 

Code:

private const int EM_SETRECT = 0xB3;  [DllImport(@"User32.dll", EntryPoint = @"SendMessage", CharSet = CharSet.Auto)] private static extern int SendMessageRefRect(IntPtr hWnd, uint msg, int wParam, ref RECT rect);  [StructLayout(LayoutKind.Sequential)] private struct RECT {     public readonly int Left;     public readonly int Top;     public readonly int Right;     public readonly int Bottom;      private RECT(int left, int top, int right, int bottom)     {         Left = left;         Top = top;         Right = right;         Bottom = bottom;     }      public RECT(Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom)     {     } } public void SetPadding(TextBox textBox, Padding padding) {     var rect = new Rectangle(padding.Left, padding.Top, textBox.ClientSize.Width - padding.Left - padding.Right, textBox.ClientSize.Height - padding.Top - padding.Bottom);     RECT rc = new RECT(rect );     SendMessageRefRect(textBox.Handle, EM_SETRECT, 0, ref rc); } 

Now call like so:

SetPadding(myTextBox, new Padding(5, 5, 5, 5)); 

Of course, best is to create your own TextBox control which can automatically set Multiline to true and stop unwanted lines breaks in the text etc..

like image 38
user990827 Avatar answered Sep 22 '22 19:09

user990827