Articles in this section
Category / Section

How to know which BarItem was clicked on a toolbar using mouse down point in WinForms XPToolBar?

1 min read

Handle MouseDown event

For doing this, first it is necessary to get hold of the BarControlInternal object which is associated with each XPMenus.Bar and then handle the MouseDown event of that particular BarControl to get the BarItem. Please refer the below code snippet which illustrates this:


CommandBarExt cbe1 = mainFrameBarManager1.GetBarControl(bar1) as CommandBarExt;
//Retrieving BarControlInternal object for the BarControl
BarControlInternal bcint = cbe1.BarControl as BarControlInternal;
bcint.MouseDown += new MouseEventHandler(bcint_MouseDown);
void bcint_MouseDown(object sender, MouseEventArgs e)
   BarControlInternal bari = sender as BarControlInternal;
   //Get the MousePoint related to the BarControlInternal object
   Point pt = bari.PointToScreen(new Point(e.X, e.Y));
   //Using the MousePoint to retrieve the BarItem
   BarItem bitem = bari.HitTest(pt.X, pt.Y) as BarItem;
   MessageBox.Show("The Clicked BarItem is:"+"\t"+bitem.Text);


Dim cbe1 As CommandBarExt = CType(IIf(TypeOf mainFrameBarManager1.GetBarControl(bar1) Is CommandBarExt, mainFrameBarManager1.GetBarControl(bar1), Nothing), CommandBarExt)
'Retrieving BarControlInternal object for the BarControl
Dim bcint As BarControlInternal = CType(IIf(TypeOf cbe1.BarControl Is BarControlInternal, cbe1.BarControl, Nothing), BarControlInternal)
AddHandler bcint.MouseDown, AddressOf bcint_MouseDown
Private Sub bcint_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
    Dim bari As BarControlInternal = CType(IIf(TypeOf sender Is BarControlInternal, sender, Nothing), BarControlInternal)
    'Get the MousePoint related to the BarControlInternal object
    Dim pt As Point = bari.PointToScreen(New Point(e.X, e.Y))
    'Using the MousePoint to retrieve the BarItem
    Dim bitem As BarItem = CType(IIf(TypeOf bari.HitTest(pt.X, pt.Y) Is BarItem, bari.HitTest(pt.X, pt.Y), Nothing), BarItem)
    MessageBox.Show("The Clicked BarItem is:" & Constants.vbTab & bitem.Text)
End Sub


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