Programmatically Add RibbonButtons Into Ribbon


This tip is contributed by Franziska.
http://www.codeproject.com/Messages/4454699/Re-Programmatically-add-Buttons-to-a-Panel-or-set-.aspx

 

To add a ribbon button programmatically into a ribbon, after create the Item/Button or whatever you want to add to the RibbonPanel, add it immediately after creating it, then set the Text/Images an DO NOT use SetBounds, the it measures the required size for itself.


Example:

This is correct

RibbonButton rbBtn =  new RibbonButton("Button1");
// below line must be added here
ribbonPanel2.Items.Add(rbBtn);
// Then, you can change any properties after that
// Examples of properties changes:
rbBtn.Image = global::RibbonTest.Properties.Resources.fileopen;
rbBtn.SmallImage = global::RibbonTest.Properties.Resources.fileopen;
rbBtn.Text = "My Dynamic Added Button";
// Don't add the button into panel here
// ribbonPanel2.Items.Add(rbBtn);

This is wrong

RibbonButton rbBtn =  new RibbonButton("Button1");
rbBtn.Image = global::RibbonTest.Properties.Resources.fileopen;
rbBtn.SmallImage = global::RibbonTest.Properties.Resources.fileopen;
rbBtn.Text = "My Dynamic Added Button";
ribbonPanel2.Items.Add(rbBtn);

Last edited Dec 18, 2012 at 11:17 AM by adriancs, version 4

Comments

No comments yet.