首页  编辑  

TextBox设置为只能输入数字

Tags: /C#/界面处理/TextBox_RichEdit/   Date Created:

using System . Runtime . InteropServices;

[ DllImport ( "User32.dll" )]

static extern int SetWindowLong( IntPtr hWnd, int nIndex, int dwNewLong);

[ DllImport ( "User32.dll" )]

static extern int GetWindowLong( IntPtr hWnd, int nIndex);

public const int GWL_STYLE = - 16 ;

public const int ES_NUMBER = 0x2000 ;

private void Form1_Load( object sender , EventArgs e)

{

   SetWindowLong(textBox1 . Handle, GWL_STYLE,

       GetWindowLong(textBox1 . Handle, GWL_STYLE) | ES_NUMBER);

}

// or

private void textBox1_KeyPress( object sender , KeyPressEventArgs e)

{

   e . Handled = "0123456789" . IndexOf(e . KeyChar) < 0 ;

}