Spread中能够设置每个单元格能够显示的字符长度,但是没有提供能够输入字符的的限制,所以需要自己添加一些代码来实现限制用户输入的功能:
- private void Form1_Load(object sender, EventArgs e)
- {
- TextCellType tc = new TextCellType();
- //设置单元格能够显示的字符长度
- tc.MaxLength = 1;
- this.fpSpread1.ActiveSheet.Columns[0].CellType = tc;
- }
- private void fpSpread1_EditModeOn(object sender, EventArgs e)
- {
- if (fpSpread1.Sheets[0].ActiveRowIndex == 0)
- {
- GeneralEditor geditor = ((GeneralEditor)(fpSpread1.EditingControl));
- //在进入编辑模式时对输入的字符进行检查
- geditor.KeyPress -= new KeyPressEventHandler(geditor_KeyPress);
- geditor.KeyPress += new KeyPressEventHandler(geditor_KeyPress);
- }
- }
- void geditor_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar != 'A' && e.KeyChar != 'a' && e.KeyChar != '\b')
- {
- e.Handled = true;
- }
- }
复制代码 |