C# 文本输入限制类型,datagridview单元格输入验证
1.只能输入double类型
private void textBoxX6_KeyPress(object sender, KeyPressEventArgs e)
{
{
//数字0~9所对应的keychar为48~57,小数点是46,Backspace是8
e.Handled = true;
//输入0-9和Backspace del 有效
if ((e.KeyChar >= 47 && e.KeyChar <= 58) || e.KeyChar == 8)
{
e.Handled = false;
}
if (e.KeyChar == 46) //小数点
{
if (textBoxX6.Text.Length <= 0)
e.Handled = true; //小数点不能在第一位
else
{
float f;
if (float.TryParse(textBoxX6.Text + e.KeyChar.ToString(), out f))
{
e.Handled = false;
}
}
}
}
2.只能输入数字
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
// 允许输入:数字、退格键(8)、全选(1)、复制(3)、粘贴(22)
if (!Char.IsDigit(e.KeyChar) && e.KeyChar != 8 &&
e.KeyChar != 1 && e.KeyChar != 3 && e.KeyChar != 22)
{
e.Handled = true;
}
}
3.
ESC 27 7 55
SPACE 32 8 56
! 33 9 57
" 34 : 58
# 35 ; 59
$ 36 < 60
% 37 = 61
& 38 > 62
' 39 ? 63
( 40 @ 64
) 41 A 65
* 42 B 66
+ 43 C 67
' 44 D 68
- 45 E 69
. 46 F 70
/ 47 G 71
0 48 H 72
1 49 I 73
2 50 J 74
3 51 K 75
4 52 L 76
5 53 M 77
6 54 N 78
O 79 g 103
P 80 h 104
Q 81 i 105
R 82 j 106
S 83 k 107
T 84 l 108
U 85 m 109
V 86 n 110
W 87 o 111
X 88 p 112
Y 89 q 113
Z 90 r 114
[ 91 s 115
\ 92 t 116
] 93 u 117
^ 94 v 118
_ 95 w 119
` 96 x 120
a 97 y 121
b 98 z 122
c 99 { 123
d 100 | 124
e 101 } 125
f 102 ~ 126
75=<e.char<=122 为字符
48=<e.char<=56 为字符
2.单元格输入验证
this.dGV.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dGV_EditingControlShowing);
}
void dGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (this.dGV.CurrentCell.ColumnIndex == 4)
{
e.Control.KeyPress -= new KeyPressEventHandler(TextBoxDec_KeyPress);
e.Control.KeyPress += new KeyPressEventHandler(TextBoxDec_KeyPress);
}
} private void TextBoxDec_KeyPress(object sender, KeyPressEventArgs e)
{
if (this.dGV.CurrentCell.ColumnIndex ==4)
{
if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
} }
要点:在 EditingControlShowing 的事件中,判断单元格,所在列,调取文本输入事件
C# 文本输入限制类型,datagridview单元格输入验证的更多相关文章
- winform的datagridview单元格输入限制和右键单击datagridview单元格焦点跟着改变
在datagridview的EditingControlShowing事件里面添加代码: if (this.dgv_pch.Columns[dgv_pch.CurrentCell.ColumnInde ...
- WinForm中DataGridView验证单元格输入的是数字
转载:http://www.cnblogs.com/ganqiyin/archive/2013/02/18/2915491.html 事件:DataGridView验证单元格输入的是数字,DataGr ...
- Winform Datagridview 单元格html格式化支持富文本
Winform Datagridview 单元格html格式化支持富文本 示例: 源码:https://github.com/OceanAirdrop/DataGridViewHTMLCell 参考: ...
- 设置DataGridView单元格的文本对齐方式
实现效果: 知识运用: DataGridViewCellStyle类的Alignment属性 //获取或设置DataGridView单元格内的单元格内容的位置 public DataGridV ...
- DataGridView 单元格自动填充
在DataGridView单元格中,当输入指定字符时,自动完成填充. 通过 TextBox实现 AutoCompleteMode AutoCompleteMode.Suggest: AutoCompl ...
- 【Winform-自定义控件】DataGridView 单元格合并和二维表头
DataGridView单元格合并和二维表头应用: //DataGridView绑定数据 DataTable dt = new DataTable(); dt.Columns.Add("); ...
- WinForm笔记1:TextBox编辑时和DataGridView 单元格编辑时 的事件及其顺序
TextBox 编辑框 When you change the focus by using the mouse or by calling the Focus method, focus event ...
- winform中dataGridView单元格根据值设置新值,彻底解决绑定后数据类型转换的困难
// winform中dataGridView单元格在数据绑定后,数据类型更改困难,只能迂回实现.有时候需要将数字变换为不同的文字描述,就会出现int32到string类型转换的异常,借助CellFo ...
- DataGridView单元格合并
本文章转载:http://www.cnblogs.com/xiaofengfeng/p/3382094.html 图: 代码就是如此简单 文件下载:DataGridView单元格合并源码 也可以参考: ...
随机推荐
- CodeReview是开发中的重要一个环节,整理了一些关于jupiter for java
什么是代码评审(CodeReview)? 代码评审也称代码复查,是指通过阅读代码来检查源代码与编码标准的符合性以及代码质量的活动. Jupiter提供了代码行级别的评审批注功能,方便评审参与人了解具体 ...
- java中绘图-----那个鼠标等的监听我还是不太会,,好苦恼啊。不知道这些监听事件是怎么区分的
总结::监听到底该怎么用 事件的区分是靠判断还是 package com.a.b; //我想实现,当我点击一个按钮时,这个frame里可以画出实心的矩形 import java.awt.Color; ...
- Android BindService中遇到的一个小问题
今天在使用BindService的时候遇到个小问题,我希望通过Bindservice获取到这个服务,然后执行服务内的某个自定义方法,如下: if(bindService==null){ Intent ...
- 03-CSS中的选择器
高级选择器分为: 后代选择器 子代选择器 并集选择器 交集选择器 后代选择器 使用空格表示后代选择器 , 顾名思义 父元素的后代(包括儿子,孙子,重孙子) 中间空格隔开 是后代 .container ...
- krpano之背景音乐
步骤: 1.添加音乐控制插件 <!-- START:音乐控制 --> <plugin name="soundinterface" url="%SWFPA ...
- JS判断IE,FF,Opera,Safari等浏览器类型
第一种,只区分浏览器,不考虑版本 function myBrowser(){ var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 var ...
- Tornado 高并发源码分析之四--- HTTPServer 与 TCPServer 对象
主要工作: 服务器启动的时候做的事: 1.把包含了各种配置信息的 application 对象封装到了 HttpServer 对象的 request_callback 字段中,等待被调用 2.TCPS ...
- The centos disc was not found in any of your drives.Please insert the centos disc and press OK to retry
查看虚拟机设置中关于CDROM的选项,将CDROM的状态改为已连接,不要奇怪,勾选上之后再按下OK就好了
- 从SQL Server中清除msdb备份和恢复记录
正如我在前面的技巧“您的数据库上次恢复是什么时候呢?”中提到的,SQL Server使msdb数据库内系统表中的备份和恢复记录保持激活状态.没有正常的维护,这些系统表将变得很大,从而导致对于msdb数 ...
- Cfree clion windows c语言 socket 网络编程
server.c #include <stdio.h> #include <winsock2.h> #define SERVER_PORT 5208 //侦听端口 int ma ...