Monday, September 22, 2014

Add a Ribbon panal into a Windows Forms Application (C#)

Reminder: Please note that this ribbon does not work on .Net 3.5 Client Profile and .NET 4.0 Client Profile. You have to switch the target framework to .NET 3.5 or .NET 4.0. When you first create a project, Visual Studio might initially set the target framework to Client Profile

If the project is using Client Profile, you might receive this error while you are trying to build the solution:
Error 3 The type or namespace name 'Ribbon' does not exist in the namespace 'System.Windows.Forms' (are you missing an assembly reference?) 

1. Get System.Windows.Forms.Ribbon35.dll from download.
2. Create a blank WinForms project.

3. Add Ribbon into Visual Studio Toolbox.
Right Click on Toolbox > Add Tab.

Give the new tab a name "Ribbon".

Right Click on the New Tab [Ribbon] > Choose Items...

[Browse...] Where are you? System.Windows.Forms.Ribbon35.dl?

There you are... Gotcha... Select it...

Only [Ribbon] can be dragged into Form. Others, as the picture below said, they are not needed to exist in toolbox. However, its not going to harm your computer or project if you select all the items belongs to ribbon (by default). Its up to you.

And finally, what you're going to do is just...

Another Way
Manually code it behind.
You can add the ribbon into WinForm too with code behind.
Add a reference of  System.Windows.Forms.Ribbon35.dll into your project. Build the the solution.

Open the designer of Main Form. In this example, Form1.Designer.cs.

Add these three lines of code
private System.Windows.Forms.Ribbon ribbon1;
ribbon1 = new System.Windows.Forms.Ribbon();
this.Controls.Add(ribbon1); 
into Form1.Designer.cs 
private void InitializeComponent()
{
    ribbon1 = new System.Windows.Forms.Ribbon();
    this.components = new System.ComponentModel.Container();
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.Text = "Form1";
    this.Controls.Add(ribbon1);
}
private System.Windows.Forms.Ribbon ribbon1; 

Save and Close Form1.Designer.cs
Double click and open Form1.cs, and now the Ribbon control is added into the main form.


Lets continue...

4. Click on the Ribbon and click Add Tab. 

5. Click on the newly added RibbonTab, then click Add Panel.

6. Click on the newly added RibbonPanel, go to Properties. You will see a set of available controls that can be added to the RibbonPanel.

You might not able to see the extra command links of "Add Button", "Add ButtonList", "Add ItemGroup"... etc at the Properties Explorer.

Right click at the Properties Explorer and Tick/Check the [Commands]. 


7. Try to add some buttons into the RibbonPanel.
8. Click on the RibbonButton, go to Properties.
9. Let's try to change the image and the label text of the button.

10. This is how your ribbon looks like now.
11. Now, create the click event for the buttons. Click on RibbonButton, go to Properties, modify the Name of the button.

12. Click on the RibbonButton, go to properties > Click on Events > Double Click on event of Click

13. Events created.
public Form1()
{
    InitializeComponent();
}

void cmdNew_Click(object sender, EventArgs e)
{
    MessageBox.Show("Button \"New\" Clicked.");
}

void cmdSave_Click(object sender, EventArgs e)
{
    MessageBox.Show("Button \"Save\" Clicked.");
}
14. Press F5 to run the application. Done.

15. You might want to inherit your Main Form into a RibbonForm to have extra features. Such as:
Note: Inherit the Main Form to RibbonForm will have some compatibility problems with some of the System.Windows.Forms controls. (especially MDI Client Control)   This problem is solved in released version 10 May 2013.

16. In the code for Form1.cs, change inheritance of Form this line:
public partial class Form1 : Form
to RibbonForm 
public partial class Form1 : RibbonForm

Part 3: Caution While Using With Visual Studio 2010 

... deleted ....

Part 4: Using this Ribbon with an MDI Enabled WinForm 



The following guide will show how to apply this ribbon with an MDI (Multi Document Interface) enabled WinForm.
Note: In previous version of Ribbon, inheritance of RibbonForm is not supported well with MDI Enabled WinForm. This problem is solved in released version of 10 May 2013

Start  

1.  Lets design a ribbon winform something like this as example. In the properties window, set IsMdiContainer to True.

2. Create another simple another form that will be loaded into the MDI Container of MainForm.

3. At code behind of Form1, add in the below codes:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this.ControlBox = false;
        this.WindowState = FormWindowState.Maximized;
        this.BringToFront();
    }
}  
4. At code behind of MainForm, create the click events for RibbonButton at MainForm:
Note: In previous version of Ribbon, inheritance of RibbonForm is not supported well with MDI Enabled WinForm. This problem is solved in released version of 10 May 2013.
public partial class MainForm : RibbonForm
{
    public MainForm()
    {
        InitializeComponent();
    }

    private void ribbonButton_Form1_Click(object sender, EventArgs e)
    {
        // Load Form1
    }

    private void ribbonButton_Close_Click(object sender, EventArgs e)
    {
        // Close All Forms
    }
}  
5. Codes for loading Form1 into MDI:
private void ribbonButton_Form1_Click(object sender, EventArgs e)
{
    foreach (Form f in this.MdiChildren)
    {
        if (f.GetType() == typeof(Form1))
        {
            f.Activate();
            return;
        }
    }
    Form form1 = new Form1();
    form1.MdiParent = this;
    form1.Show();
} 
6. Codes for closing all opened form in MDI:
private void ribbonButton_Close_Click(object sender, EventArgs e)
{
    while (this.ActiveMdiChild != null)
    {
        this.ActiveMdiChild.Close();
    }
} 
7. That's it. Enjoy.

No comments:

Post a Comment