Articles in this section
Category / Section

How to define custom shortcuts for BarItem in WinForms XPToolBar?

2 mins read

Define custom shortcuts for BarItem

In XPToolBar, user can define set of pre-defined Short cut keys to its child items like Control, Shift etc. and it will help to select child items programmatically, when defined Key is pressed.

But there is no option to provide custom key combinations and it can be achieved by handling KeyDown event and using PerformClick function.

For Example, here we have created an example for Control + Key Up / Key Down key combinations.

C#

/// <summary>
/// KeyDown overrides
/// </summary>
protected override void OnKeyDown(KeyEventArgs e)
{
  if(e.KeyCode == Keys.Up && e.Modifiers == Keys.Control)
  {
    // To invoke Control + Key Up Baritem to be clicked programatically. 
    barItem1.PerformClick();
  }
  else if(e.KeyCode == Keys.Down && e.Modifiers == Keys.Control)
  {
    // To invoke Control + Key Down Baritem to be clicked programatically. 
    barItem2.PerformClick();
  }
  else if(e.KeyCode == Keys.Enter && e.Modifiers == Keys.Control)
  {
    // To invoke Control + Key Enter Baritem to be clicked programatically. 
    barItem3.PerformClick();
  }
  base.OnKeyDown(e);
}
 
/// <summary>
/// Control + Key Up BarItem
/// </summary>
private void barItem1_Click(object sender, EventArgs e)
{
  MessageBox.Show("Control + Key Up is clicked");
}
 
/// <summary>
/// Control + Key Down BarItem
/// </summary>
private void barItem2_Click(object sender, EventArgs e)
{
  MessageBox.Show("Control + Key Down is clicked");
}
 
/// <summary>
/// Control + Enter BarItem
/// </summary>
private void barItem3_Click(object sender, EventArgs e)
{
  MessageBox.Show("Control + Enter Key is clicked");
}

VB

''' <summary>
''' KeyDown overrides
''' </summary>
Protected Overrides Sub OnKeyDown(ByVal e As KeyEventArgs)
  If e.KeyCode = Keys.Up AndAlso e.Modifiers = Keys.Control Then
    ' To invoke Control + Key Up Baritem to be clicked programatically. 
    barItem1.PerformClick()
  ElseIf e.KeyCode = Keys.Down AndAlso e.Modifiers = Keys.Control Then
    ' To invoke Control + Key Down Baritem to be clicked programatically. 
    barItem2.PerformClick()
  ElseIf e.KeyCode = Keys.Enter AndAlso e.Modifiers = Keys.Control Then
    ' To invoke Control + Key Enter Baritem to be clicked programatically.
    barItem3.PerformClick()
  End If
  MyBase.OnKeyDown(e)
End Sub
 
''' <summary>
''' Control + Key Up BarItem
''' </summary>
Private Sub barItem1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles barItem1.Click
  MessageBox.Show("Control + Key Up is clicked")
End Sub
 
''' <summary>
''' Control + Key Down BarItem
''' </summary>
Private Sub barItem2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles barItem2.Click
  MessageBox.Show("Control + Key Down is clicked")
End Sub
 
''' <summary>
''' Control + Enter BarItem
''' </summary>
Private Sub barItem3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles barItem3.Click
  MessageBox.Show("Control + Enter Key is clicked")
End Sub

Screenshot

Show the custom shortcuts for BarItem

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied