34

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
dab
  • 817
  • 1
  • 12
  • 23

6 Answers6

50

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}

Nimrod
  • 1,316
  • 9
  • 14
  • 4
    Only trouble is that for many newer Windows skins, Fixed3D on a TextBox looks different than for a Panel. – KeithS Feb 04 '11 at 22:40
  • 3
    It may be a good idea to set your panel to have a click event that focuses in the textbox. Since this new padding area will appear to be a part of the textbox, it may throw the user off if they click it and never get focus on the textbox. – Adam Plocher Jun 27 '14 at 23:10
  • @AdamPlocher In case of asp.net :) – Anirudha Gupta Feb 28 '18 at 09:00
14

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..

tkefauver
  • 491
  • 5
  • 19
user990827
  • 553
  • 1
  • 5
  • 10
  • 1
    Does this solution work with the Windows 10 TextBox styles? On my Windows 10 installation the code in this answer has no visible effect regardless of the chosen BorderStyle. – silviubogan May 25 '18 at 17:22
7

Well, you can TrimLeft, then concatenate with 5 spaces. Or, you can set up a custom UserControl with a borderless TextBox as the actual entry element, overlaying another TextBox that doesn't have a tabstop and will shift focus to the borderless one when it's focused.

KeithS
  • 70,210
  • 21
  • 112
  • 164
4

This question has recommended answer already. Anyway I would like to put alternative answer. To add padding to the textbox in c#,you can use "padLeft" method. Hope this help to someone.

textBox1.Text = "Hello";
textBox1.Text = textBox1.Text.PadLeft(textBox1.Text.Length + 5);

or

textBox1.Text = textBox1.Text.PadLeft(textBox1.Text.Length + 5, '*');
Leo
  • 157
  • 1
  • 1
  • 1
    This, unfortunately, has some bad drawbacks. For example, one can backspace through the left padding. :( – Christian Apr 26 '15 at 13:03
  • 2
    I found this solution incredibly useful as i'm using a readonly textbox so backspacing cannot occur. – Anya Hope May 04 '16 at 15:43
  • 2
    @Christian In this case you could use `TextChanged()` to set `PadLeft` on every change. This way it couldnt get below it. – C4d Jun 22 '16 at 09:53
1

I know this is a bit old. But here is a solution. For the initial text, add a space at the beginning. Then, you can override the OnKeyPress event and add the following code so that you can't backspace.

protected override void OnKeyPress (KeyPressEventArgs e) {
   base.OnKeyPress (e);
   if (e.KeyChar == (char)Keys.Back && Text.Length == 1) e.Handled = true;
   else e.Handled = true;
}

You can replace the 1 with number of spaces to pad.

kakkarot
  • 478
  • 1
  • 9
  • 24
0

Expanding upon a response above and a perceived drawback of being able to backspace through the padded value. The SelectionStart property of the textbox can be used to determine where to position cursor when the TextChanged event fires.

In this example, the textbox is padded at the beginning with 2 spaces so that the information displayed will align with other non-input controls where the padding property is used.

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        int pad = 2;
        int cursorPos = textBox1.SelectionStart;
        textBox1.Text = textBox1.Text.Trim().PadLeft(textBox1.Text.Trim().Length + pad);
        textBox1.SelectionStart = (cursorPos > pad ? cursorPos : pad);
    }
Galactic
  • 400
  • 4
  • 14