简体   繁体   English

按键改变布尔? 最佳做法是什么?

[英]key press to change a bool? what is the best practice?

I have a TreeView and a multiline TextBox. 我有一个TreeView和一个多行TextBox。 When user clicks on a node in the treeview, it's Text property will be added to the TextBox. 当用户单击树视图中的节点时,其Text属性将添加到TextBox中。

By default, everytime that a node is clicked (or hitting enter when treeview has focus and that sort of stuff...), the TextBox will be cleared and the new Text of the selected node will be added. 默认情况下,每次单击一个节点(或在树视图具有焦点和诸如此类的东西时单击回车...),都会清除文本框并添加所选节点的新Text

But I am looking for a way, that when user holds a modifier key (ctrl or shift...) the textbox will not be cleared and the newly selected node's text will be added to the textbox without clearing anything. 但是我正在寻找一种方式,当用户按住修改键(Ctrl或Shift ...)时,不会清除文本框,并且新选择的节点的文本将被添加到文本框而不清除任何内容。

I am thinking about having a boolean, and whenever the modifier key is pressed, it changes to false and when the key is released it gets back to true. 我正在考虑使用一个布尔值,每当按下修饰键时,它就会更改为false,当释放该键时,它将恢复为true。

public bool ClearBox = true;

Later: 后来:

private void AddText(string text)
{
   if(ClearBox == true) //by default it is true
   {
      textBox.Clear();
      textBox.Text = text;
   }
   else //user is holding a modifier key so the ClearBox is false now
   {
      textBox += Environment.NewLine + text;
   }
}

Node select event: 节点选择事件:

private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
{
   //How to check here if a modifire key is being pressed (holded) ?
   this.AddText(treeView.SelectedNode.Text);
}

You can ask the property ModifierKeys of the form which modifier key is pressed. 您可以要求表单的属性ModifierKeys被按下。 In your code you would have to do like this: 在您的代码中,您必须这样做:

private void treeView_AfterSelect(object sender, TreeViewEventArgs e) 
{
    if (ModifierKeys == Keys.ShiftKey)
    {
        this.AddText(treeView.SelectedNode.Text); 
    }
}

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

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