Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 设置运行时控件的位置_C#_.net_Winforms_Runtime - Fatal编程技术网

C# 设置运行时控件的位置

C# 设置运行时控件的位置,c#,.net,winforms,runtime,C#,.net,Winforms,Runtime,我在应用程序中添加了一个包含列表和toolStrip的列表框。这些用于选择要在面板中添加的运行时的控件。(我添加了一个图像。toolStrip和listBox分别位于左侧和右侧。)现在我使用这些控件所做的是 1-我从toolStrip或listBox中选择一个项目 2-生成运行时控件并在窗体上显示 3-在鼠标悬停事件中,控件设置在面板上(如果在面板中) 现在的问题是,如图所示,运行时控件不在光标的顶端。我希望这些运行时控件位于光标的顶端 下面是图片。 编辑:下面是代码: private voi

我在应用程序中添加了一个包含列表和toolStrip的列表框。这些用于选择要在面板中添加的运行时的控件。(我添加了一个图像。toolStrip和listBox分别位于左侧和右侧。)现在我使用这些控件所做的是

1-我从toolStrip或listBox中选择一个项目

2-生成运行时控件并在窗体上显示

3-在鼠标悬停事件中,控件设置在面板上(如果在面板中)

现在的问题是,如图所示,运行时控件不在光标的顶端。我希望这些运行时控件位于光标的顶端

下面是图片。

编辑:下面是代码:

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;
}
谢谢。

i)。我不知道你们为什么不使用Panel_MoveUP事件,因为你们想把它放在面板上

(ii)。在Panel_moveUp中,您必须通过点击和跟踪方法将一点点位置更改为光标位置,我不确定为什么会出现这种差异

    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);

        }
    }
i) 。我不知道你们为什么不使用Panel_MoveUP事件,因为你们想把它放在面板上

(ii)。在Panel_moveUp中,您必须通过点击和跟踪方法将一点点位置更改为光标位置,我不确定为什么会出现这种差异

    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);

        }
    }
i) 。我不知道你们为什么不使用Panel_MoveUP事件,因为你们想把它放在面板上

(ii)。在Panel_moveUp中,您必须通过点击和跟踪方法将一点点位置更改为光标位置,我不确定为什么会出现这种差异

    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);

        }
    }
i) 。我不知道你们为什么不使用Panel_MoveUP事件,因为你们想把它放在面板上

(ii)。在Panel_moveUp中,您必须通过点击和跟踪方法将一点点位置更改为光标位置,我不确定为什么会出现这种差异

    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);

        }
    }

您必须相对地将运行时添加控件的
位置
设置为父级,您可以像这样使用
PointToClient
方法:

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

  globalLabel1.Location = lPanel.PointToClient(Cursor.Position);
}

您必须相对地将运行时添加控件的
位置
设置为父级,您可以像这样使用
PointToClient
方法:

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

  globalLabel1.Location = lPanel.PointToClient(Cursor.Position);
}

您必须相对地将运行时添加控件的
位置
设置为父级,您可以像这样使用
PointToClient
方法:

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

  globalLabel1.Location = lPanel.PointToClient(Cursor.Position);
}

您必须相对地将运行时添加控件的
位置
设置为父级,您可以像这样使用
PointToClient
方法:

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

  globalLabel1.Location = lPanel.PointToClient(Cursor.Position);
}


您必须更改光标以将其显示在光标尖端。正如您所看到的图像,有一个标签标记为“ddd:ddd”,我希望该标签位于光标尖端。不低于它。而且我不认为我需要改变光标。你是想让鼠标位置显示什么吗?然后选中,您必须使用
PointToClient
方法来获取指定控件的相对位置。可能重复的控件必须更改光标以将其显示在光标尖端。正如您看到的图像,有一个标签标记为“ddd:ddd”,我希望此标签位于光标尖端。不低于它。而且我不认为我需要改变光标。你是想让鼠标位置显示什么吗?然后选中,您必须使用
PointToClient
方法来获取指定控件的相对位置。可能重复的控件必须更改光标以将其显示在光标尖端。正如您看到的图像,有一个标签标记为“ddd:ddd”,我希望此标签位于光标尖端。不低于它。而且我不认为我需要改变光标。你是想让鼠标位置显示什么吗?然后选中,您必须使用
PointToClient
方法来获取指定控件的相对位置。可能重复的控件必须更改光标以将其显示在光标尖端。正如您看到的图像,有一个标签标记为“ddd:ddd”,我希望此标签位于光标尖端。不低于它。而且我不认为我需要改变光标。你是想让鼠标位置显示什么吗?然后检查,您必须使用
PointToClient
方法来获取指定控件的相对位置。此方法的可能重复非常有帮助。谢谢。这很有帮助。谢谢。这很有帮助。谢谢。这很有帮助。谢谢,天哪。为什么我没有使用Panel_MouseUp事件。第二件事,为什么我应该在那里使用-50和-100?使用-50,-100不是一个好方法,你必须使用上面解决方案中提到的PointToClient。哦,上帝。为什么我没有使用Panel_MouseUp事件。第二件事,为什么我应该在那里使用-50和-100?使用-50,-100不是一个好方法,你必须使用上面解决方案中提到的PointToClient。哦,上帝。为什么我没有使用Panel_MouseUp事件。第二件事,为什么我应该在那里使用-50和-100?使用-50,-100不是一个好方法,你必须使用上面解决方案中提到的PointToClient。哦,上帝。为什么我没有使用Panel_MouseUp事件。第二件事,为什么我应该在那里使用-50和-100?使用-50,-100不是一个好方法,您必须使用上述解决方案中提到的PointToClient。