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);
}
You can ask the property
ModifierKeysof the form which modifier key is pressed. In your code you would have to do like this: