在TextChange事件中调用KeyPress事件 [英] Call KeyPress Event in TextChange Event

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

问题描述

我的代码:

My Code:

private void txtCategoryCode_TextChanged(object sender, EventArgs e)
        {
            
            txtCategoryCode.KeyPress += new KeyPressEventHandler(MyKeyPress);
        }

        void MyKeyPress(object sender, KeyPressEventArgs e)
        {
            TextBox txt = sender as TextBox;
            if (txt.Text.Length > 3)
            {
                e.Handled = true;
                MessageBox.Show("Category Code should be in 3 letters.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }



此代码不起作用.请告诉我我必须在代码中进行更改的地方才能工作?



This code is not working. Please tell me where I have to change in my code to do work?

推荐答案

事件处理程序不应显式调用.每当事件发生时就会调用它们.

您只需要在设计时为文本框设置MaxLength属性即可使用.
Event handlers should not be called explicitly. They are called when ever the event is raised.

You just need to set the MaxLength property for the textbox at design time and it will work.


尝试以下操作:-
首先在txtCategoryCode按键事件中编写代码,然后在txtCategoryCode textChenged事件中编写以下代码:-
Try this:-
first write the code in txtCategoryCode Key press event and in txtCategoryCode textChenged event write the following code:-
this.txtCategoryCode.Keypress +=new EventHandler(txtCategoryCode_KeyPress);


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

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