Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.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# 添加从文本文件读取位置数据的动态控件(PictureBox)_C#_Visual Studio - Fatal编程技术网

C# 添加从文本文件读取位置数据的动态控件(PictureBox)

C# 添加从文本文件读取位置数据的动态控件(PictureBox),c#,visual-studio,C#,Visual Studio,我有一个只有一行的文本文件。这行代码如下所示: 100 300 200 400 658 487 2636 254 245 527 这些数字表示点的X和Y坐标,第一个和第二个是点N1的X和Y,第三个和第四个是点N2的X和Y 我读取文件并将其放入数组中。 我的下一步是在容器面板中绘制图片框。 问题是面板仅显示带有最后一个坐标的控件 private void CreateBlastHole(string[] pointCoordinate) { PictureBox blastHole =

我有一个只有一行的文本文件。这行代码如下所示:

100 300 200 400 658 487 2636 254 245 527
这些数字表示点的X和Y坐标,第一个和第二个是点N1的X和Y,第三个和第四个是点N2的X和Y

我读取文件并将其放入数组中。 我的下一步是在容器面板中绘制图片框。 问题是面板仅显示带有最后一个坐标的控件

private void CreateBlastHole(string[] pointCoordinate)
{
    PictureBox blastHole = new PictureBox();

    blastHole.Height = 15;
    blastHole.Width = 15;
    blastHole.BackColor = Color.Blue;

    for (int i = 0; i < pointCoordinate.Length; i++)
    {
        blastHole.Left = int.Parse(pointCoordinate[i]);
        i = i + 1;
        blastHole.Top = int.Parse(pointCoordinate[i]);

        drawingPanel.Controls.Add(blastHole);
    }

    blastHole.Click += new EventHandler(BlastHole_Click);

    ToolTip tooltip1 = new ToolTip();

    // Set up delays for the tooltip
    tooltip1.AutoPopDelay = 5000;
    tooltip1.InitialDelay = 1000;
    tooltip1.ReshowDelay = 500;

    // Force the tooltip text to be displayed whether or not the form is active.
    tooltip1.ShowAlways = true;

    // Set up the tooltip text for the controls
    int axisX = blastHole.Location.X;
    int axisY = blastHole.Location.Y;
    string coordinates = "Точка N " + blastHole.Name + "X = " + axisX.ToString() + " Y = " + axisY.ToString();
    tooltip1.SetToolTip(blastHole, coordinates);
}

private void BlastHole_Click(object sender, EventArgs e)
{
    MessageBox.Show(MousePosition.X.ToString(), MousePosition.Y.ToString());
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void openButton_Click(object sender, EventArgs e)
{
    openFileDialogPoints.ShowDialog();
    string name = openFileDialogPoints.FileName;
    File.ReadAllLines(name);
    string[] points = File.ReadAllText(name).Split( );

    CreateBlastHole(points);
}

private void drawingPanel_Paint(object sender, PaintEventArgs e)
{

}

private void buttonDrawHole_Click(object sender, EventArgs e)
{

}

您需要将代码移动到for循环中,以创建多个PictureBox。目前,您只创建一个实例,对每个爆破孔重复使用它

请尝试以下方法:

private void CreateBlastHole(string[] pointCoordinate)
{
    for (int i = 0; i < pointCoordinate.Length; i++)
    {
        PictureBox blastHole = new PictureBox();

        blastHole.Height = 15;
        blastHole.Width = 15;
        blastHole.BackColor = Color.Blue;

        blastHole.Left = int.Parse(pointCoordinate[i]);
        i = i + 1;
        blastHole.Top = int.Parse(pointCoordinate[i]);

        drawingPanel.Controls.Add(blastHole);

        blastHole.Click += new EventHandler(BlastHole_Click);

        ToolTip tooltip1 = new ToolTip();

        // Set up delays for the tooltip
        tooltip1.AutoPopDelay = 5000;
        tooltip1.InitialDelay = 1000;
        tooltip1.ReshowDelay = 500;

        // Force the tooltip text to be displayed whether or not the form is active.
        tooltip1.ShowAlways = true;

        // Set up the tooltip text for the controls
        int axisX = blastHole.Location.X;
        int axisY = blastHole.Location.Y;
        string coordinates = "Точка N " + blastHole.Name + "X = " + axisX.ToString() + " Y = " + axisY.ToString();
        tooltip1.SetToolTip(blastHole, coordinates);
    }
}

您只创建了一个PictureBox。drawingPanel中有什么。for循环完成后的控件?