添加动态控件(PictureBox),从文本文件中读取位置数据

时间:2015-09-02 18:45:13

标签: 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 = 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)
{

}

1 个答案:

答案 0 :(得分:0)

您需要在for循环中移动代码,以创建多个PictureBox。目前,您只创建一个实例,并为每个blastHole重用它。

尝试这样的事情:

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