This project has moved and is read-only. For the latest updates, please go here.

contextMenuStrip1 Events

Jul 9, 2013 at 4:06 PM
Hello, mr. Kurt
Please tell how to fix my problem?
I want to translate ContexMenuStrip Items to other language but i have some problems with events...
in documentation is: menuItem.Click += new EventHandler((_sender, _eventArgs) => itemDef.Execute(itemDef, project1))
But i have an error message "there is no command to redone"
Please help me with this...
Sorry for my english...
Jul 10, 2013 at 7:14 AM
Can you provide me some more code and/or a demo program?
A step-by-step description of what you are doing / what happens before the error message occurs would also be handy.

The error message comes from the project component and is used when Project.History.Redo() is called but there are no redo commands in the queue, e.g. when you open a new NShape project and call Project.History.Redo() without calling Project.History.Undo() before.
Jul 10, 2013 at 5:20 PM
I'm doing just like in docunebtation:
private void myContextMenuStrip_Opening(object sender, CancelEventArgs e) {
// Get index of item where to insert
int insertPos = myContextMenuStrip.Items.IndexOf(toolStripMenuItemSeparator2);
// Process all MenuItemDefs provided by the IDisplayPresenter
foreach (MenuItemDef itemDef in display1.GetMenuItemDefs()) {
     // Skip separators, prohibited items and items with sub items (for simplicity)
     if (itemDef is SeparatorMenuItemDef) continue;
     if (!itemDef.IsGranted(project1.SecurityManager)) continue;
     if (itemDef.SubItems != null) continue;

     // Get wanted items (all layer commands in this simple case)

             // Build ToolStripMenuItem from MenuItemDef
             ToolStripMenuItem menuItem = new ToolStripMenuItem(itemDef.Title);
             // Add event handler (implemented as lambda expression in this example)
             menuItem.Click += new EventHandler((_sender, _eventArgs) => itemDef.Execute(itemDef, project1));
             // Set menu item properties
             menuItem.Tag = itemDef;
             menuItem.Name = itemDef.Name;//Here i'g trying to change name, translate it
             menuItem.Enabled = (itemDef.IsFeasible && itemDef.IsGranted(project1.SecurityManager));
             menuItem.ToolTipText = itemDef.Description;
             // Set icon and the background color rendered as transparent
             menuItem.Image = itemDef.Image;
             menuItem.ImageTransparentColor = itemDef.ImageTransparentColor;

             // Insert the created menu item into your ContextMenuStrip
             myContextMenuStrip.Items.Insert(insertPos, menuItem);
}
}

But when i try to execute command like Cut or Copy a have an error...
Jul 10, 2013 at 6:57 PM
And please tell one more thing.
I add shapes from code not from tools. For Example:
Shape = (Circle)project1.ShapeTypes["Circle"].CreateInstance();
Shape.Text = "f";
Shape.Diameter = diam;
Shape.X = e.X;Shape.Y = e.Y;
Shape.FillStyle = project1.Design.FillStyles["Transparent"];
d.Shapes.Add(Shape);
project1.Repository.Insert((Dataweb.NShape.Shape)Shape, d);
display1.Diagram = d;
Why i have a NullArgumentExpression on operations like display.Cut???
Jul 10, 2013 at 7:26 PM
Sorry fo too many questions...
But tell please what is wrong...
I can't save as... file correctly? i mean without ArgumentNullExpression... Only rewrite them...
With Opening files same thing...
Explain Please how create a project correctly...
Project create
xmlStore1.DirectoryName = Application.StartupPath;
            xmlStore1.FileExtension = "nspj";
            project1.Name = "N1";
            project1.LibrarySearchPaths.Add("Library");
            project1.Open();
            d=new Diagram("D1");
            display1.Diagram=d;
            display1.ShowDefaultContextMenu = false;
            CreateTools();
Project Save
using (SaveFileDialog dlg = new SaveFileDialog())
        {
            dlg.CreatePrompt = false;
            dlg.Filter = fileFilter;
            if (!string.IsNullOrEmpty(xmlStore1.DirectoryName))
                dlg.InitialDirectory = xmlStore1.DirectoryName;
            dlg.FileName = project1.Name + (string.IsNullOrEmpty(xmlStore1.FileExtension) ? ".xml" : xmlStore1.FileExtension);
            if (dlg.ShowDialog(this) == DialogResult.OK)
            {
                project1.Name = Path.GetFileNameWithoutExtension(dlg.FileName);
                xmlStore1.DirectoryName = Path.GetDirectoryName(dlg.FileName);
                xmlStore1.FileExtension = Path.GetExtension(dlg.FileName);
            }
        }
        if (!string.IsNullOrEmpty(project1.Name)
            && !string.IsNullOrEmpty(xmlStore1.DirectoryName))
            project1.Repository.SaveChanges();
Project Open
if (project1.IsOpen) project1.Close();
                string projectsDirectory = Path.Combine(Path.GetDirectoryName(Application.StartupPath), "Demo Projects");
                openFileDialog1.FileName = string.Empty;
                openFileDialog1.InitialDirectory = Path.GetFullPath(projectsDirectory);
                if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    xmlStore1.DirectoryName = Path.GetDirectoryName(openFileDialog1.FileName);
                    xmlStore1.FileExtension = "nspj";
                    project1.Name = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
                    project1.Open();                       
                    CreateTools();     
                }
Jul 10, 2013 at 7:43 PM
if i save as... Project i have an ArgumentNullException and dispite of it .nspj file is created and when i revrite this file everithing is ok
Jul 11, 2013 at 2:25 PM
Edited Jul 11, 2013 at 2:35 PM
Problem 1: Modifying the Context Menu

The code of the lambda expression does not work as expected:
I'm not sure why, but the argument "itemDef" used inside the lambda expression does not change while iterating through the collection.
Because of this, all menu items execute the code of the first menu item - which is "Redo" by default.

We will update the documentation.
Here is the correct lambda exprassion:
    // Add event handler (implemented as lambda expression in this example)
    menuItem.Click += new EventHandler((_sender, _eventArgs) => {
        MenuItemDef mnuItemDef = (MenuItemDef)((ToolStripMenuItem)_sender).Tag;
        mnuItemDef.Execute(mnuItemDef, project1);
    });
Alternatively, you can also use normal event handlers here.

If you only want to translate the menu items, you do not need to change anything but the text.
Set the "ShowDefaultContextMenu" to "True" (adds all default menu items to the context menu) and simply apply the translated text:
private void myContextMenuStrip_Opening(object sender, CancelEventArgs e) {
    ContextMenuStrip menuStrip = (ContextMenuStrip)sender;
    foreach (ToolStripItem item in menuStrip.Items) {
        ToolStripMenuItem menuItem = item as ToolStripMenuItem;
        if (menuItem != null)
            menuItem.Text = String.Format("ToDo: Translate Item '{0}'", menuItem.Name);
    }
}
Jul 11, 2013 at 2:43 PM
Problem 2: NullReferenceException after Creating Shapes

I assume, that variable "d" holds a new diagram, right?
d.Shapes.Add(Shape);
If the assumption above is correct, then you will get a CachedRepositoryException ("Diagram not found in repository.") when calling Repository.Insert:
project1.Repository.Insert((Dataweb.NShape.Shape)Shape, d);
You have to insert a diagram before the shapes.
For convinience, you can call
project1.Repository.InsertAll(d)
which will insert the diagram including all its contents into the repository.

Do you call this code in the constructor or the Form_Load event of a Winforms window? These will swallow all exceptions.
Please put a try/catch block around this code and set a breakpoint so you will get notified when something went wrong.
Jul 11, 2013 at 2:51 PM
Edited Jul 11, 2013 at 2:52 PM
Problem 3: How to create a Project

Please have a look at the code of "Basic Tutorial 7 - Libraries", method "Form_Load".
But your code should be ok.

Please fix the "Insert" call of Problem 2 and try your app again. This issue seems like a follow-up error to me.
Jul 11, 2013 at 4:35 PM
Thank you wery much! It's help me a lot)
But i have NullReferenceException when try to you commands like display.Cut(false)...
And have some problems with Save... I't save project, but when i try to open it it's open an emty project...
Open void works correctly, i tryed it on other projects...
Open void:
if (project1.IsOpen) project1.Close();
                    string projectsDirectory = Path.Combine(Path.GetDirectoryName(Application.StartupPath), "Demo Projects");
                    openFileDialog1.FileName = string.Empty;
                    openFileDialog1.InitialDirectory = Path.GetFullPath(projectsDirectory);
                    if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
                    {
                        xmlStore1.DirectoryName = Path.GetDirectoryName(openFileDialog1.FileName);
                        xmlStore1.FileExtension = "nspj";
                        project1.Name = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
                        project1.LibrarySearchPaths.Add(Application.StartupPath);
                        project1.Open();
                         List<Diagram> res = new List<Diagram>(project1.Repository.GetDiagrams());
                         CreateTools();
                         d = res[0];
                         d.Height = display1.Height;
                         d.Width = display1.Height;
                         display1.Diagram = d;
                    }
Please explain what is happend???
Jul 11, 2013 at 5:10 PM
If i rewrite Demo project's it's all worked correct... I can't understand( i think all problems in creating project....
Jul 12, 2013 at 8:13 AM
Could you please provide me the stack trace of the NullReferenceException so I can see where it occurs?
Jul 12, 2013 at 6:47 PM
Here it is
Dataweb.NShape.Controllers.DiagramSetController.Cut(Diagram source, IEnumerable1 shapes, Boolean withModelObjects, Point startPos)
Dataweb.NShape.Controllers.DiagramSetController.Cut(Diagram source, IEnumerable
1 shapes, Boolean withModelObjects)
Dataweb.NShape.WinFormsUI.Display.PerformCut(Diagram diagram, IEnumerable`1 shapes, Boolean withModelObjects, Point position)
Dataweb.NShape.WinFormsUI.Display.Cut(Boolean withModelObjects, Point currentMousePos)
Dataweb.NShape.WinFormsUI.Display.Cut(Boolean withModelObjects)
Neuron_Edit.Form1.вырезатьToolStripMenuItem_Click(Object sender, EventArgs e) in row display.cut(false)
Jul 15, 2013 at 1:17 PM
Could you please check whether the Project component is assigned to your DiagramSetController?

If it is and all other NShape components are correctly linked together, could you please send me your project (or a demo where the problem is actually reproducable, e.g. a reduced version of your project)?
Jul 15, 2013 at 3:01 PM
Thank You) I already solve this problem and everything works correctly.
Jul 15, 2013 at 3:03 PM
Can you tell me how can i switch off dragging from toolbox?
Jul 15, 2013 at 3:51 PM
The tool box is optional.
Is you don't need it, you can simply delete the ToolboxController and ToolBoxPresenter components from your form.

If this is not what you want, please read this:
NShape Documentation: "Programmer Tasks > Customizing the Toolbox"

Moreover, you can use the build-in security system to prevent certain users to create/edit/delete/... shapes.
Please have a look on the shipped Demo Program "Security Demo".
Jul 15, 2013 at 4:10 PM
Thank you)
Jul 16, 2013 at 7:52 AM
Andrey_B wrote:
Thank You) I already solve this problem and everything works correctly.
Nice to hear.
May I ask what the problem was?
Jul 16, 2013 at 9:58 AM

Maybe i used not ordinary method but it's all works.
This command call an exception but it work, i used try catch with empty catch block
And it's work without any exception)
С уважением Будылкин А.А.