添加从文本文件读取位置数据的动态控件(PictureBox)
本文关键字:动态控件 PictureBox 置数据 位置 文本 文件 读取 添加 | 更新日期: 2023-09-27 18:10:58
我有一个文本文件,其中有一行。该行看起来像这样:
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
。目前,您只创建了一个实例,并为每个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);
}
}