想要中断第一个char形式零 [英] want to interrupt first char form zero

查看:95
本文介绍了想要中断第一个char形式零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理文本框。我使用了一个keydown事件来处理按下的键。现在我需要在我的keydown事件中中断第一个来自0的字符。假设在我的文本框中我按了779.现在如果我在779之前按0,那么我想禁用0表示e.handled = true。如果我知道那个时候按键的长度位置,我可以解决它。我的代码如下:

  private   void  txtBianry_KeyDown( object  sender,KeyEventArgs e)
{
if (按键 的第一个长度和 0 来自文字全文)
e.Handled = true ;

// 继续......
}



如何解决这个问题?

提前谢谢。

解决方案

查看TextBox.SelectionStart属性 - 它给出了当前的插入位置。如果它为零,并且SelectionLength为零,则光标位于开头且未选择任何文本。



[edit]

BTW:你不想在KeyDown中使用它 - 你想要它在KeyPress中:

  private   void  myTextBox_KeyPress( object  sender,KeyPressEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb!= null
{
if (tb.SelectionStart == 0 && tb.SelectionLength == 0 && e.KeyChar == ' 0'
{
e.Handled = true ;
}
}
}



[/ edit]


< blockquote>我会使用NumericUpDown。这里至少有一篇文章展示了如何做到这一点。如果不这样做,你不能只检查现有值的长度吗?


I am working on a textbox. I used a keydown event for handling pressed key. Now I need to do in my keydown event to interrupt the first char from 0. Suppose in my textbox I pressed 779. Now if I press 0 before 779, then I want to disable 0 that means e.handled=true. I could solved it if I will know length position of press key in that time. My code will like the following:

private void txtBianry_KeyDown(object sender, KeyEventArgs e)
{
    if (pressed key is of first length and 0 from full of text)
         e.Handled = true;

    // continue...
}


How can I solve this?
Thanks in advance.

解决方案

Look at the TextBox.SelectionStart property - it gives the current caret position. If it is zero, and the SelectionLength is zero, the cursor is at the start and no text is selected.

[edit]
BTW: You don''t want this in KeyDown - you want it in KeyPress:

private void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
    TextBox tb = sender as TextBox;
    if (tb != null)
        {
        if (tb.SelectionStart == 0 && tb.SelectionLength == 0 && e.KeyChar == '0')
            {
            e.Handled = true;
            }
        }
    }


[/edit]


I''d use a NumericUpDown. And There is at least one article on here showing how to do this. Failing that, can''t you just check the Length of the existing value?


这篇关于想要中断第一个char形式零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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