简体   繁体   English

如何在“ textBox1_KeyDown”功能下使用“ textBox1_TextChanged”?

[英]How To Use “textBox1_TextChanged” Under “textBox1_KeyDown” Function?

I am using two functions on a textBox1 that are textBox1_TextChanged and textBox1_KeyDown as my desired code is to run after hitting some keys and on text change and working fine as below. 我在textBox1上使用了两个函数,分别是textBox1_TextChangedtextBox1_KeyDown ,这是因为我想要的代码将在敲击某些键并进行文本更改并按如下所示正常工作后运行。

private void textBox1_TextChanged(object sender, EventArgs e)
{
    TextChanged();
}

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    KeysPressed(sender, e);
}

But ٰI want to run my both function under textBox1_KeyDown() only using the below concept. 但是,我只想使用以下概念在textBox1_KeyDown()下运行我的两个函数。

private string currentText = "";
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{    
    if (currentText != textBox1.Text)
    {
        TextChanged();
    }
    currentText = textBox1.Text;
    KeysPressed(sender, e);
}

My problem is that my TextChanged(); 我的问题是我的TextChanged(); in upper code runs after adding two characters. 在上层代码中添加两个字符后运行。 I want it to run on every character and on add/delete/edit/paste/replace in textBox1 so any ideas to make it work on first hit on the textBox1_KeyDown() function? 我希望它可以在textBox1每个字符上以及添加/删除/编辑/粘贴/替换上运行,所以有什么想法可以使其在textBox1_KeyDown()函数的第一次命中时起作用?

Explained: 解释:

I understand my problem as when I use textBox1_KeyDown() event then it run the inner code TextChanged(); 我理解我的问题,因为当我使用textBox1_KeyDown()事件时,它会运行内部代码TextChanged(); first then it work for its own work like I press a character then it will run TextChanged(); 首先,它按自己的工作方式工作,例如按一个字符,然后它将运行TextChanged(); then will type that character in the textBox1 so that's why my TextChanged(); 然后在textBox1键入该字符,这就是为什么我的TextChanged(); is not able to detect any changed in text. 无法检测到文本中的任何更改。 So how to fix this...??? 那么如何解决这个问题呢?

From your question i assumed that your problem that at first hit for textBox1_KeyDown TextChanged(); 根据您的问题,我认为您遇到的问题首先是针对textBox1_KeyDown TextChanged(); is not called. 不被调用。 Only at second hit it will be called successfully. 仅在第二次命中时,它将被成功调用。 If my understanding is right then i think the problem is with your if statement as at startup both textBox1 text and currentText will be equals as both are empty string. 如果我的理解是正确的,那么我认为问题出在启动时,如果if语句是textBox1 text和currentText都将是相等的,因为它们都是空字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM