Applies to: Nevron Diagram for .NET
How to replace / override the commands in command bars manager?
The easiest way to replace/override the commands in command bars manager (NDiagramCommandBarsManager) is to simply replace / override the command in the diagram commander like this:
[C#]
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
private
void
Form1_Load(
object
sender, EventArgs e)
{
// create document
NDrawingDocument document =
new
NDrawingDocument();
// create drawing view
NDrawingView view =
new
NDrawingView();
view.Document = document;
view.Dock = DockStyle.Fill;
Controls.Add(view);
// create command bars manager
NDiagramCommandBarsManager manager =
new
NDiagramCommandBarsManager();
manager.ParentControl =
this
;
manager.View = view;
NDiagramCommander commander = manager.Commander;
// replace the New Drawing Command
NNewDrawingCommand newDrawingCmd = (NNewDrawingCommand)commander.Commands.GetCommandFromId((
int
)DiagramCommand.NewDrawing);
int
index = commander.Commands.IndexOf(newDrawingCmd);
commander.Commands.RemoveAt(index);
commander.Commands.Insert(index,
new
NMyNewDrawingCommand());
}
public
class
NMyNewDrawingCommand : NNewDrawingCommand
{
public
override
void
Execute()
{
MessageBox.Show(
"Execute my new command"
);
}
public
override
bool
Enabled
{
get
{
return
true
;
}
}
}
}
[VB.NET]
Public
Partial
Class
Form1
Inherits
Form
Public
Sub
New
()
InitializeComponent()
End
Sub
Private
Sub
Form1_Load(sender
As
Object
, e
As
EventArgs)
' create document
Dim
document
As
New
NDrawingDocument()
' create drawing view
Dim
view
As
New
NDrawingView()
view.Document = document
view.Dock = DockStyle.Fill
Controls.Add(view)
' create command bars manager
Dim
manager
As
New
NDiagramCommandBarsManager()
manager.ParentControl =
Me
manager.View = view
Dim
commander
As
NDiagramCommander = manager.Commander
' replace the New Drawing Command
Dim
newDrawingCmd
As
NNewDrawingCommand =
DirectCast
(commander.Commands.GetCommandFromId(
CInt
(DiagramCommand.NewDrawing)), NNewDrawingCommand)
Dim
index
As
Integer
= commander.Commands.IndexOf(newDrawingCmd)
commander.Commands.RemoveAt(index)
commander.Commands.Insert(index,
New
NMyNewDrawingCommand())
End
Sub
Public
Class
NMyNewDrawingCommand
Inherits
NNewDrawingCommand
Public
Overrides
Sub
Execute()
MessageBox.Show(
"Execute my new command"
)
End
Sub
Public
Overrides
ReadOnly
Property
Enabled()
As
Boolean
Get
Return
True
End
Get
End
Property
End
Class
End
Class
Now when the New Drawing is selected it will show a message box.
Article ID: 48, Created On: 10/5/2010, Modified: 11/15/2010