DropDown style RibbonButton and Click event

Feb 7, 2014 at 9:39 AM
Hello, i have a problem with the RibbonButton set with DropDown style. I need to dinamically fill the DropDownItemCollection when the user clicks the button, because the list of items can change each time depending on the context. I thought to put the code to fill the list into the RibbonButton click event, but i discovered that if i set the RibbonButton with the DropDown style, the click event does not raise. I think to understand why, but i need a way to fill the list dinamically. Has anyone had the same need? Somebody knows some workaround? I tried to use the MouseDown Event, but it raise too late! Some ideas? Thanks to all!
Feb 8, 2014 at 2:20 PM
You have many ways (events) to achieve what you want.
  1. RibbonButton.DropDownShowing
  2. RibbonComboBox.DropDownShowing
  3. RibbonPopup.Opening
Choose whichever event suits best your needs. Via the sender you can do anything you need.

Hope that helps!
Feb 11, 2014 at 3:38 PM
I'm using RibbonButton but, unfortunately, DropDownShowing event raise too late for me. Example:
Private Sub RibbonButton1_DropDownShowing(sender As Object, e As EventArgs) Handles RibbonButton1.DropDownShowing
    RibbonButton1.DropDownItems.Add(New System.Windows.Forms.RibbonButton("one"))
    RibbonButton1.DropDownItems.Add(New System.Windows.Forms.RibbonButton("two"))
    RibbonButton1.DropDownItems.Add(New System.Windows.Forms.RibbonButton("three"))
End Sub
When i click the button, i would like to see a list with three elements, but it does not work.
Any suggestions?
Thanks a lot!
Feb 16, 2014 at 6:11 PM
Implementation in ComboBox and Button is different. Sou if you do it in the Button analogous to the ComboBox it should work.

In RibbonButton.ShowDropDown()
            OnDropDownShowing(EventArgs.Empty);  // fire event before the drop down is created!

            DropDown.MouseEnter += new EventHandler(DropDown_MouseEnter);
            DropDown.Closed += new EventHandler(DropDown_Closed);
            DropDown.ShowSizingGrip = DropDownResizable;
            DropDown.DrawIconsBar = _iconsBar;

            RibbonPopup canvasdd = Canvas as RibbonPopup;
            Point location = OnGetDropDownMenuLocation();
            Size minsize = OnGetDropDownMenuSize();

            if (!minsize.IsEmpty) DropDown.MinimumSize = minsize;

            //OnDropDownShowing(EventArgs.Empty);  // thats too late!
Feb 17, 2014 at 9:15 AM
Thank you toAtWork, actually modifying the code into the button implementation it does work. I initially tried to solve my problem without changing the code into the library, but at this point i think it is the only way. I also made another little change, because i need to fire OnDropDownShowing event also if the list items contains 0 elements. So i changed the initial test:
if (Style == RibbonButtonStyle.Normal || DropDownItems.Count == 0)
removing the second condition.

What about including these features into next release? Do you think it should be possible?
Thanks a lot!
Feb 17, 2014 at 10:08 AM
Hopefully on weekend I get some time to test this a little more and then commit it to the repository.
Feb 17, 2014 at 2:11 PM
Great, thanks again!
Feb 23, 2014 at 11:28 AM
Now also click event of dynamically created items is working: