关于TextBox的ToolTip和TextChanged事件 [英] Regarding ToolTip and TextChanged Event of TextBox

查看:70
本文介绍了关于TextBox的ToolTip和TextChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在这里,如果我在文本框中输入任何字符串值,则应该在工具提示中提示错误.它已经完成,但是当我在此之后输入Int值时,工具提示仍然可见.
如果在文本框中输入int值,则应该取消交易..
所以这是代码,请按照我的要求更正..

谢谢.

Hello all,

Here if i enter any string value in text box it should prompt an error in Tooltip. It is done but when I enter Int value after that, the Tooltip is still visible.
If enter int value in textbox it should disbale..
So here is the code, pls correct it as i''ve asked for..

Thanks.

private void linenumtxt_TextChanged(object sender, EventArgs e)
        {
            checkText(linenumtxt.Text);
        }

        public void checkText(string value)
        {
            int number;
            bool result;

            result = int.TryParse(value, out number);

            ToolTip tp = new ToolTip();

            if (!result)        // if it is not a number
            {
                if (linenumtxt.Text != "")
                {
                    tp.ToolTipIcon = ToolTipIcon.Error;
                    tp.AutoPopDelay = 500;
                    tp.ToolTipTitle = "Unacceptable Character";
                    tp.Show("You can only type a number here.", linenumtxt);
                    linenumtxt.Clear();
                }
            }
        }

推荐答案

如果您查看 ^ ],它是为您详细说明的:您必须自己隐藏它.您还应该在类级别而不是方法本地创建工具提示:
If you have a look at the MSDN documentation[^], it is spelled out for you: you have to Hide it yourself. You should also create the ToolTip at class level, not locally to the method:
private void linenumtxt_TextChanged(object sender, EventArgs e)
    {
    checkText(linenumtxt);
    }
private ToolTip tp = new ToolTip();

public void checkText(TextBox tb)
    {
    int number;
    bool result;
    result = int.TryParse(tb.Text, out number);
    if (!result)        // if it is not a number
        {
        if (linenumtxt.Text != "")
            {
            tp.ToolTipIcon = ToolTipIcon.Error;
            tp.AutoPopDelay = 500;
            tp.ToolTipTitle = "Unacceptable Character";
            tp.Show("You can only type a number here.", tb);
            linenumtxt.Clear();
            }
        }
    else
        {
        tp.Hide(tb);
        }
    }

我也使它更加灵活...

I have also made it a little more flexible...


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

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