[英]How to capture the spacebar press event using KeyeventHandler?
I have a Form with a rich text box in which i want to do the following: 我有一个带有富文本框的表单,我要在其中执行以下操作:
When user presses the spacebar button (Currently i am doing it with keydown event but want to use key press event but it doesn't provide e.keycode), a function should be called in which this logic is to be implemented: 当用户按下空格键时(当前我正在执行keydown事件,但想使用按键事件,但它不提供e.keycode),应调用要在其中实现此逻辑的函数:
last written word is to be fetched and is to be looped through the text of rich text box in order to find its number of occurrences in a rich text box. 最后一个单词将被提取并在富文本框的文本中循环显示,以便在富文本框中找到其出现的次数。
What i have done so far is: 到目前为止,我所做的是:
private void textContainer_rtb_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
String abc = this.textContainer_rtb.Text.Split(' ').Last();
chkWordRepeat(abc);
}
}
public void chkWordRepeat(String lastWordToFind)
{
int count = new Regex(lastWordToFind).Matches(this.textContainer_rtb.Text.Split(' ').ToString()).Count;
MessageBox.Show("Word: " + lastWordToFind + "has come: " + count + "times");
}
Please let me know if the above mentioned logic is correct or not And how can i attach this logic with key press event for spacebar? 请让我知道上述逻辑是否正确,如何将这个逻辑与空格键的按键事件附加在一起? If not then please help me implementing!
如果没有,请帮助我实施!
Thanks in advance. 提前致谢。
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == ' ')
MessageBox.Show("space pressed");
}
My opinion is : 我的意见是:
public Dictionary<string, int> data;
private void textContainer_rtb_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
String abc = this.textContainer_rtb.Text.Split(' ').Last();
chkWordRepeat(abc);
}
}
public void chkWordRepeat(string wrd)
{
bool present = false;
foreach (string key in data.Keys)
if (wrd == key)
{
present = true;
data[wrd]++;
}
if (!present)
data.Add(wrd, 1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.