Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

Windows Ribbon for WinForms v2.5 Released – New Events Mechanism

4.90/5 (3 votes)
27 Dec 2010Ms-PL2 min read 21.8K  
Windows Ribbon for WinForms v2.5 Released – New Events Mechanism

The Windows Ribbon for WinForms is a managed wrapper around Windows Ribbon Framework, a free ribbon control that Microsoft provides in Windows 7 and Vista.
More details on this project can be found in the project site at windowsribbon.codeplex.com.

I've just released another update to the project.

Note: This release contains breaking changes, only take it if you are starting a new project, or you don't mind the needed updates to your code.

Basically, I've changed how events are exposed in the library, made it a little more .NET friendly.

The benefits of these changes are:

  • You now get the control which generated the event as the sender of the event.
    For example, this allows registering the same callback function to different buttons and having the ability to know which button raised the event.
  • You can now use the Windows Ribbon for WinForms library in languages that can work only with proper .NET event handlers. The first request for this feature was from someone who wanted to use it in Progress ABL… There is a world beyond C#, C++ and VB.NET…

How to Upgrade the Existing Code?

If you already have code that uses the previous versions of the project, follow these guidelines to do the upgrade.

Upgrading C# Code

The event names and signatures changed, thus, you need to change code like this:

C#
_buttonNew.OnExecute += new OnExecuteEventHandler(_buttonNew_OnExecute);
C#
_richFont.OnPreview += new OnPreviewEventHandler(_richFont_OnPreview);
C#
_richFont.OnCancelPreview += new OnCancelPreviewEventHandler(_richFont_OnCancelPreview);

into code like this:

C#
_buttonNew.ExecuteEvent += new EventHandler<ExecuteEventArgs>(_buttonNew_OnExecute);
C#
_richFont.PreviewEvent += new EventHandler<ExecuteEventArgs>(_richFont_OnPreview);
C#
_richFont.CancelPreviewEvent += new EventHandler<ExecuteEventArgs>
				(_richFont_OnCancelPreview);

And change the method signatures from this:

C#
void _buttonNew_OnExecute(PropertyKeyRef key, PropVariantRef 
		currentValue, IUISimplePropertySet commandExecutionProperties)

to this:

C#
void _buttonNew_OnExecute(object sender, ExecuteEventArgs e)

Note that the ExecuteEventArgs class holds the data that was previously passed to the execute method directly.

Upgrading VB.NET Code

The event names and signatures changed, thus, you need to change code like this:

VB.NET
AddHandler _buttonNew.OnExecute, AddressOf _buttonNew_OnExecute
VB.NET
AddHandler _richFont.OnPreview, AddressOf _richFont_OnPreview
VB.NET
AddHandler _richFont.OnCancelPreview, AddressOf _richFont_OnCancelPreview

into code like this:

VB.NET
AddHandler _buttonNew.ExecuteEvent, AddressOf _buttonNew_ExecuteEvent
VB.NET
AddHandler _richFont.PreviewEvent, AddressOf _richFont_PreviewEvent
VB.NET
AddHandler _richFont.CancelPreviewEvent, AddressOf _richFont_CancelPreviewEvent

And change the method signatures from this:

VB.NET
Private Sub _buttonNew_OnExecute(ByVal key As PropertyKeyRef, _
	ByVal currentValue As PropVariantRef, _
	ByVal commandExecutionProperties As IUISimplePropertySet)

to this:

VB.NET
Private Sub _buttonNew_ExecuteEvent(ByVal sender As Object, ByVal e As ExecuteEventArgs)

Note that the ExecuteEventArgs class holds the data that was previously passed to the execute method directly.

That’s it for now,
Arik Poznanski.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)