如何在进行自定义控制时允许空格? [英] How to allow spaces while making custom control?

查看:97
本文介绍了如何在进行自定义控制时允许空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!!

在我的学费结构项目中,我需要文本框包含字符串或整数值,所以为了这个目的,我为数字&对于字符,它工作正常,但它没有在两个字符串之间占用空间...

我的代码

hello!!
In my school fees structure project i need text boxes either contain string or int value so for that purpose i make my custom controls for numeric & for characters it is working properly but it is not taking space between two string...
my code

public class Class1 : TextBox
    {
        protected override void OnKeyPress(KeyPressEventArgs e)
        {


            if (char.IsNumber(e.KeyChar) == false)
            {
                if ((e.KeyChar == (char)Keys.Back)||(e.KeyChar==(char)Keys.Space))
                {
                    e.Handled = false;

                }
                else
                    e.Handled = true;
            }
        }
    }


    public class TextBoxAlphabetic : TextBox
    {
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (char.IsLetter(e.KeyChar) == false)
            {
                if ((e.KeyChar == (char)Keys.Back)||(e.KeyChar==(char)Keys.Space))
                    e.Handled = false;
                else
                    e.Handled = true;
            }
        }
    }





我应该使用&& oprator ???



should i use && oprator???

推荐答案

我应该使用&& oprator ???

否。

你不能取代||用和&& - 否则测试将始终失败,因为KeyChar值不能同时是Key.Back和Key.Space - 出于同样的原因,它不能同时是''X''和''7''时间!



在每个处理程序的开头放置一个断点,并按照它进行操作 - 看看当你按下空格键并尝试从那里开始工作时会发生什么。



BTW:不要这样写:

"should i use && oprator??? "
No.
You can''t replace the "||" with and "&&" - otherwise teh test will always fail as teh KeyChar value cannot both be Key.Back and Key.Space at the same time - for the same reason it can''t be ''X'' and ''7'' at the same time!

Put a breakpoint at the start of each handler, and follow it through - see what happens when you press the space bar and try to work it out from there.

BTW: Don''t write it like this:
if (char.IsNumber(e.KeyChar) == false)

不过,请使用:

INstead, use this:

if (!char.IsNumber(e.KeyChar))

一旦你习惯它,它会更紧凑,更明显!

It''s a lot more compact and more obvious once you get used to it!


这篇关于如何在进行自定义控制时允许空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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