クリップボードからの貼り付けでは文字が入力できてしまいます。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace NumericTextBox
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ('0' <= e.KeyChar && e.KeyChar <= '9') {
}
else if(e.KeyChar == '\b'){
}
else {
e.Handled = true;
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace NumericTextBox
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (Keys.D0 <= e.KeyCode && e.KeyCode <= Keys.D9) {
e.Handled = false;
}
else if (Keys.NumPad0 <= e.KeyCode && e.KeyCode <= Keys.NumPad9) {
e.Handled = false;
}
else if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back) {
e.Handled = false;
}
else if (e.KeyCode == Keys.Tab) {
e.Handled = false;
}
else if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right
|| e.KeyCode == Keys.Up || e.KeyCode == Keys.Down) {
e.Handled = false;
}
else {
e.Handled = true;
e.SuppressKeyPress = true;
}
}
}
}