比如说,现在的textbox.width=100,而我要让他认为只有50,前面和后面各空出30,20的空间,在这空间之内不能输入信息,就相当于看上去是100长度的textbox其实只有50长度

解决方案 »

  1.   

     两边各加上足够的空格 取数据时 Trim()下
      

  2.   

    this.textBox1.MaxLength = 5;
    this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;是不是让文本居中呢?是的话设置属性就可以了
      

  3.   

    是不是因为要配合界面的显示才做成这样的
    你的最终目的是什么,大家不是很清楚只能猜,没法帮你想一个最好的办法你可以自己做个控件,画个假的textbox中间放个没边框的textbox...
      

  4.   

    如果是Web的话,设置TextBox的Style就可以了:<asp:TextBox ID="textBox1" runat="server" style="width:100px; left-padding:30px; right-padding: 20px;" Text="textBox1"/>如果是Win的话就麻烦点,自己重画TextBox。
      

  5.   

    winform就是要重画textbox控件,要求只有textbox的一部分是可以输入内容的
      

  6.   

    抱歉,中午回的匆忙,有点问题;如果winform的话,这样就可以了,不一定非得重画的,我写了个demo,如下:using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            TextBoxEx textBox = new TextBoxEx();
                textBox.TextMargin = new TextMargin(30, 20);
                textBox.Width = 100;
                this.Controls.Add(textBox);
            }        public class TextBoxEx : TextBox
            {
                private TextMargin _textMargin = new TextMargin();            public TextMargin TextMargin
                {
                    get
                    {
                        return this._textMargin;
                    }
                    set
                    {
                        if (_textMargin != value)
                        {
                            _textMargin = value;
                            this.UpdateTextMargin();
                        }
                    }
                }            protected override void OnHandleCreated(EventArgs e)
                {
                    base.OnHandleCreated(e);
                    this.UpdateTextMargin();
                }            private void UpdateTextMargin()
                {
                    if (!Form1.TextMargin.IsEmpty(this._textMargin) && this.IsHandleCreated)
                    {
                        SendMessage(this.Handle, EM_SETMARGINS, new IntPtr(EC_LEFTMARGIN | EC_RIGHTMARGIN), this._textMargin.ToWin32TextMargin);
                    }
                }            private const uint EM_SETMARGINS = 0x00D3;            private const uint  EC_LEFTMARGIN =      0x0001;
                private const uint EC_RIGHTMARGIN = 0x0002;            [DllImport("user32.dll")]
                private static extern IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr hParem, IntPtr lParam);
            }        public struct TextMargin
            {
                private int _left;
                private int _right;            public TextMargin(int left, int right)
                {
                    _left = left;
                    _right = right;
                }            public int Left
                {
                    get { return _left; }
                    set { _left = value; }
                }            public int Right
                {
                    get { return _right; }
                    set { _right = value; }
                }            public IntPtr ToWin32TextMargin
                {
                    get { return new IntPtr( this._left | (this._right << 16)); }
                }            public static bool  IsEmpty(TextMargin value)
                {
                    return value== null || ( value._left == 0 && value._right == 0) ;
                }            public static bool operator ==(TextMargin left, TextMargin right)
                {
                    if (left != null && right != null)
                    {
                        return left._left == right._left && left._right == right._right;
                    }                return left == null && right == null;
                }            public static bool operator !=(TextMargin left, TextMargin right)
                {
                    return !(left == right);
                }
            }
        }
    }