0

I have added a listBox containing a list and a toolStrip in my application. These are used to select control to added runtime in a panel. (I have added an image. The toolStrip and listBox are on the left and right side respectively.) Now what I do with these controls is,

1 - I select an item from toolStrip or listBox

2 - A runtime control is made and shown on the form

3 - On Mouse_Up event, the control is set on the Panel (If it is in the panel.)

Now the question is, as shown in the image, the runtime control is not at the tip of the cursor. I want these runtime controls on the tip of the cursor.

Below is the Image. enter image description here

EDIT: Below is the code.:

private void listBox_MouseDown(object sender, MouseEventArgs e)
{
    this.globalLabel1 = new Label();
            this.globalLabel1.Text = this.listBox.SelectedItem.ToString() + " : ";
    //other label properties like, tag, name, events, font etc.
}
private void listBox_MouseMove(object sender, MouseEventArgs e)
{
    if (this.globalLabel1 != null)
        {
            this.globalLabel1.Left = System.Windows.Forms.Cursor.Position.X
                - this.Location.X;

            this.globalLabel1.Top = System.Windows.Forms.Cursor.Position.Y
                - this.Location.Y;

            this.globalLabel1.Show();
            this.lPanel.SendToBack();
        }
}
private void listBox_MouseUp(object sender, MouseEventArgs e)
{
    this.globalLabel1.Parent = this.lPanel;
    this.globalLabel1.Anchor = AnchorStyles.Top | AnchorStyles.Left;

    this.globalLabel1.Left = Cursor.Position.X
            - /*Cursor.Size.Height -*/ this.lPanel.Location.X
            - this.Location.X;
    this.globalLabel1.Top = Cursor.Position.Y
            - /*Cursor.Size.Width -*/ this.lPanel.Location.Y
            - this.Location.Y;
}

Thanks.

DhavalR
  • 1,409
  • 3
  • 29
  • 57
  • you should have to change the cursor to show it on the tip of cursor. – YOusaFZai Dec 10 '13 at 06:29
  • http://stackoverflow.com/questions/6743644/change-mouse-cursor-into-hand-icon – YOusaFZai Dec 10 '13 at 06:31
  • As you can see the image, there is a label texed as "_ddd_: _ddd_", I want this label at the tip of the cursor. Not below it. And don't think I need to change the cursor. – DhavalR Dec 10 '13 at 06:39
  • Are you trying to get mouse position to display something? Then check [this](http://stackoverflow.com/a/1316694/1997232) and you'll have to use `PointToClient` method to get position relative for specified control. – Sinatr Dec 10 '13 at 06:40
  • Possible duplicate of [C# Change the location of an object programatically](http://stackoverflow.com/questions/8369999/c-sharp-change-the-location-of-an-object-programatically) – Jim Fell Jun 10 '16 at 17:14

2 Answers2

2

You have to set the Location of your runtime added control relatively to the parent, you can use the method PointToClient like this:

private void listBox_MouseUp(object sender, MouseEventArgs e) {
  globalLabel1.Parent = lPanel;
  globalLabel1.Anchor = AnchorStyles.Top | AnchorStyles.Left;

  globalLabel1.Location = lPanel.PointToClient(Cursor.Position);
}
King King
  • 61,710
  • 16
  • 105
  • 130
1

i). I don't know why are you not using Panel_MoveUP event, Because you wan to place it on panel.

ii). At Panel_moveUp you have to make little bit position change to cursor position by Hit and trail method, I not sure about this why is this difference coming so.

    private void listBox1_MouseDown(object sender, MouseEventArgs e)
    {
        this.globalLabel1 = new Label();
        this.globalLabel1.Text = this.listBox1.SelectedItem.ToString() + " : ";
        //other label properties like, tag, name, events, font etc.

    }

    //////////////////////////USE PANEL_MOVEUP EVENT//////////////

    private void lPanel_MouseMove(object sender, MouseEventArgs e)
    {
        if (this.globalLabel1 != null)
        {
            this.globalLabel1.Left = System.Windows.Forms.Cursor.Position.X-50 // Change
                - this.Location.X;

            this.globalLabel1.Top = System.Windows.Forms.Cursor.Position.Y-100 //Change
                - this.Location.Y;

            this.globalLabel1.Show();
            this.lPanel.SendToBack();
            this.lPanel.Controls.Add(globalLabel1);

        }
    }
YOusaFZai
  • 698
  • 5
  • 21