Schedule > Commanding > Command Bars > Schedule Command Bars via Code |
If you are building a cross-platform NOV application, you can't use the Visual Studio designer, so you should customize the command bars via code.
To hide an command bar or a command bar item use the corresponding Get... method of the NScheduleViewWithCommandBars or NScheduleViewWithCommandBarsControl class and set its Visibility property to Collapsed:
The following code snippet hides the "View" menu and the "View" toolbar:
Hide menus and toolbars |
Copy Code
|
---|---|
// Hide the "View" menu drop down nScheduleViewWithCommandBarsControl1.GetMainMenuBar() .FindMenuDropDownByText(NScheduleCommandBarBuilder.MenuViewName).Visibility = ENVisibility.Collapsed; // Hide the "View" toolbar nScheduleViewWithCommandBarsControl1.GetToolBar( NScheduleCommandBarBuilder.ToolbarViewName).Visibility = ENVisibility.Collapsed; |
![]() |
Note that instead of using string literals like "View", it is better to use the string constants defined in the NScheduleCommandBarBuilder class to get menu and toolbar names as shown in the code snippet above. |
To remove all widgets associated with specific commands from the commanding UI, use the RemoveCommands method of the schedule view with command bars:
Remove commands |
Copy Code
|
---|---|
// Create an array of commands to remove NCommand[] commandsToRemove = new NCommand[] { NScheduleView.OpenCommand, NScheduleView.SaveAsCommand, NScheduleView.SaveCommand }; NScheduleViewWithCommandBars scheduleViewWithCommandBars = new NScheduleViewWithCommandBars(); scheduleViewWithCommandBars.RemoveCommands(commandsToRemove); |
Always use the RemoveCommands item to remove individual menu and toolbar items, unless the menu item has sub items. In that case use the GetMainMenu method of the menu bar, then FindMenuDropDownByText to get the menu drop down and finally Items.FindByText to get the item with sub items.
If you want to remove a given toolbar or menu from the builders, you can easily do so by using the Remove method of the ToolBarBuilders and MenuDropDownBuilders collection of the schedule command bar builder respectively. You can get or remove builders by name.
The names of all schedule menu and toolbar builders are exposed as constants in the NScheduleCommandBarBuilder class. For example, to remove the "File" menu and the "Standard" toolbar of a command bars based schedule UI, you can use the following piece of code:
Remove Toolbar and Menu Builders |
Copy Code
|
---|---|
commandBarBuilder.MenuDropDownBuilders.Remove(NScheduleCommandBarBuilder.MenuFileName); commandBarBuilder.ToolBarBuilders.Remove(NScheduleCommandBarBuilder.ToolbarStandardName); |
In order to add or insert your own toolbar or menu drop down builder, you can use the Add or Insert methods of the corresponding builder collection respectively.
Add Custom Toolbar and Menu Builders |
Copy Code
|
---|---|
commandBarBuilder.MenuDropDownBuilders.Insert(1, new CustomMenuBuilder()); commandBarBuilder.ToolBarBuilders.Insert(0, new CustomToolBarBuilder()); |
To create a custom menu builder, you should create a class, which inherits NMenuDropDownBuilder:
Create custom menu drop down builder |
Copy Code
|
---|---|
public class CustomMenuBuilder : NMenuDropDownBuilder { public CustomMenuBuilder() : base("Custom Menu") { } protected override void AddItems(NMenuItemCollection items) { // Add the "Add Appointment" menu item items.Add(CreateMenuItem(Nevron.Nov.Schedule.NResources.Image_Edit_AddAppointment_png, NScheduleView.AddAppointmentCommand)); // Add the custom command menu item items.Add(CreateMenuItem(NResources.Image_Ribbon_16x16_smiley_png, CustomCommand)); } } |
To create a custom toolbar builder, you should create a class, which inherits the NToolBarBuilder:
Create custom toolbar builder |
Copy Code
|
---|---|
public class CustomToolBarBuilder : NToolBarBuilder { public CustomToolBarBuilder() : base("Custom Toolbar") { } protected override void AddItems(NCommandBarItemCollection items) { // Add the "Add Appointment" button items.Add(CreateButton(Nevron.Nov.Schedule.NResources.Image_Edit_AddAppointment_png, NScheduleView.AddAppointmentCommand)); // Add the custom command button items.Add(CreateButton(NResources.Image_Ribbon_16x16_smiley_png, CustomCommand)); } } |
To replace the original "Standard" toolbar builder with the custom one, use the following code:
Replace the "Standard" toolbar builder |
Copy Code
|
---|---|
NScheduleCommandBarBuilder commandBarBuilder = new NScheduleCommandBarBuilder(); int toolbarIndex = commandBarBuilder.ToolBarBuilders.IndexOf(NScheduleCommandBarBuilder.ToolbarStandardName); commandBarBuilder.ToolBarBuilders[toolbarIndex] = new CustomStandardToolbar(); |