Question

I am working on a custom control that I want to add instances of using the right click context menu item click event.

the only trouble I'm having is setting the position of control on the form here is what I have so far.

    public partial class frmMain : Form
    {
        List<GenericNode> nodeTree;
        GenericNode node;
        public frmMain()
        {
            InitializeComponent();
        }

        private void testItemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //place node item in a list with ID so that I can add many unique items
            node = new GenericNode();
            nodeTree = new List<GenericNode>();
            nodeTree.Add(node);
            node.Name = "node" + nodeTree.Count;
            //set the Location of Control based on Mouse Position

if I try to set the value of node.Location.X orY it tells me that I can't assign the value to it because it's a return value.

            //add item from list to form
            this.Controls.Add(nodeTree.ElementAt(nodeTree.Count -1));

       }
   }

Ok I've come back to this and saw nobody had any answers so I came up with a quick dirty one in case you were all scratching your heads.

    public partial class frmMain : Form
{
    List<GenericNode> nodeTree;
    GenericNode node;
    Point location;
    public frmMain()
    {
        InitializeComponent();
    }

    private void testItemToolStripMenuItem_Click(object sender, EventArgs e)
    {

        //place node item in a list with ID so that I can add many unique items
        node = new GenericNode();
        nodeTree = new List<GenericNode>();
        nodeTree.Add(node);
        node.Name = "node" + nodeTree.Count;
        //place the current object on the form at given location
        location = new Point(MousePosition.X, MousePosition.Y);
        node.Location = location;
        this.Controls.Add(nodeTree.ElementAt(nodeTree.Count - 1));
    }
}

I still can't get it to line up with my mouse though, I've tried the offsets, but It is consistently off by just a little bit.

Was it helpful?

Solution

The MousePosition you are using will be in screen coordinates, so you need to translate them to the Form's coordinates:

node.Location = PointToClient(MousePosition);

Note that the MousePosition you will see at the time the menu click event fires will be the location of the mouse when you click the menu item, not the location when right click to open the context menu.

A couple of other comments:

Don't create a new nodeTree in the click event - that will remove the previous tree and the count will never be over 1. Create it in the constructor or in the line where you define the class member.

Don't create class member variables for "node" or "location" they are only used in the click event handler, and should be defined there.

In the last line, you don't need to retrieve the node from the list, you already have a reference to it.

public partial class MainForm : Form
{
    private List<GenericNode> nodeTree;

    public MainForm()
    {
        InitializeComponent();

        nodeTree = new List<GenericNode>();
    }

    private void testitemToolStripMenuItem_Click(object sender, EventArgs e)
    {
        GenericNode node = new GenericNode();
        nodeTree.Add(node);
        node.Name = "node" + nodeTree.Count;
        node.Location = PointToClient(MousePosition);
        this.Controls.Add(node);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top