datagridview C#中的Keypress事件 [英] Keypress event in datagridview C#

查看:763
本文介绍了datagridview C#中的Keypress事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有没有人对下面的场景有所了解。

我有一个datagridview,我在其中处理了第一个datagridviewtextboxcolumn的keypress事件。在我的按键中,我允许使用字符,数字但没有空格。如何不允许用户将数字作为第一个输入用于例如他们不能将

1abc作为输入但是可以作为abc123给出。如何实现。请帮助我进一步帮助我





提前致谢

解决方案

 私有  Sub  txtValue2_KeyPress( ByVal  sender  As  System。 Object  ByVal  e  As  System.Windows.Forms.KeyPressEventArgs)句柄 txtValue2.KeyPress 
如果((AscW(e.KeyChar)> 47 AscW(e.KeyChar)< 58 (AscW( e.KeyChar)= 8 (AscW(e.KeyChar)= 46 ))然后
e.Handled = False
其他
e.Handled = True
MsgBox( 仅允许使用数字字符
结束 如果
结束 Sub







这个代码就是一个例子,按照你的意愿行事。



快乐编码


我在点击按钮时尝试了这个根据你的要求尝试



添加此正则表达式并尝试 [A-Za-z] \w *

  string  strText = textBox2.Text; 
Regex reg = new 正则表达式( ^ [A-ZA-Z] + \\d {0,9}

);
if (reg.IsMatch(textBox2.Text))
{
}
else
{
MessageBox.Show( 无效);
}





或者你可以写一些像这样的东西



  if (textBox2.length> 1)

{
txt = textBox2.substring ( 0 1 );
if (!((txt > = ' a'&& txt < = ' z')||(txt > = ' A'&& txt < = ' Z')))
}





  private   void  button1_Click( object  sender,EventArgs e)
{
string strText = textBox2。文本;
Regex reg = new 正则表达式( ^ [A-ZA-Z] + \\w *);
if (reg.IsMatch(textBox2.Text))
{
MessageBox.Show( 有效);
}
else
{
MessageBox.Show( 无效);
}
}


Hi,
Do anyone have an idea about the following scenario.
I have a datagridview in which I've handled keypress event for the first datagridviewtextboxcolumn. In my keypress I've allowed characters, digits but no spaces. How to not allow users to give number as first input For eg they cant give
1abc as input but can give as abc123. How to achieve.pls do assist me further


Thanks in advance

解决方案

Private Sub txtValue2_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtValue2.KeyPress
        If ((AscW(e.KeyChar) > 47 And AscW(e.KeyChar) < 58) Or (AscW(e.KeyChar) = 8) Or (AscW(e.KeyChar) = 46)) Then
            e.Handled = False
        Else
            e.Handled = True
            MsgBox("Only numeric charecter allowed")
        End If
    End Sub




This code is an example follow this and do as you want.

happy coding


I tried this in button click try as per you requirement

Add this regex and make a try [A-Za-z]\w*

string strText = textBox2.Text;
 Regex reg = new Regex("^[A-Za-z]+\\d{0,9}


"); if (reg.IsMatch(textBox2.Text)) { } else { MessageBox.Show("Invalid"); }



or you can write some thing like this

if(textBox2.length>1)

    {
    txt = textBox2.substring(0,1);
    if(!((txt >= 'a' && txt <= 'z') || (txt >= 'A' && txt <= 'Z')))
    }



private void button1_Click(object sender, EventArgs e)
        {
            string strText = textBox2.Text;
            Regex reg = new Regex("^[A-Za-z]+\\w*");
            if (reg.IsMatch(textBox2.Text))
            {
                MessageBox.Show("Valid");
            }
            else
            {
                MessageBox.Show("Invalid");
            }
        }


这篇关于datagridview C#中的Keypress事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆