我有一个 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);
}
发布评论
评论(1)
您可以询问表单的属性
ModifierKeys
按下了哪个修饰键。在你的代码中你必须这样做:You can ask the property
ModifierKeys
of the form which modifier key is pressed. In your code you would have to do like this: