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

发布于 2024-12-17 04:22:26 字数 901 浏览 0 评论 0 原文

我有一个 TreeView 和一个多行文本框。当用户单击树视图中的节点时,其 Text 属性将添加到 TextBox。

默认情况下,每次单击节点(或在树视图获得焦点时按 Enter 键等),文本框将被清除,并添加所选节点的新 Text

但我正在寻找一种方法,当用户按住修饰键(ctrl 或 shift...)时,文本框将不会被清除,并且新选择的节点的文本将被添加到文本框中而不清除任何内容。

我正在考虑有一个布尔值,每当按下修饰键时,它就会变为 false,而当释放该键时,它会恢复为 true。

public bool ClearBox = true;

稍后:

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;
   }
}

节点选择事件:

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);
}

I have a TreeView and a multiline TextBox. When user clicks on a node in the treeview, it's Text property will be added to the 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.

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.

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.

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俏︾媚 2024-12-24 04:22:26

您可以询问表单的属性 ModifierKeys 按下了哪个修饰键。在你的代码中你必须这样做:

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

You can ask the property ModifierKeys of the form which modifier key is pressed. 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); 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文