所以基本上我试图在一个窗体中做一个项目,我确定顶点的数量,它为每个顶点分配一个坐标,并在另一个窗体上输出。出于某种原因,任何人都不能正常工作吗?
的MainForm
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace assignment
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
public List<Vertex> vertexList = new List<Vertex>();
private void Exit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Create_Click(object sender, EventArgs e)
{
int vertices = 0, edges = 0;
try
{
vertices = (int)Convert.ToInt32(txtVertices.Text);
edges = (int)Convert.ToInt32(txtEdges.Text);
if (vertices < 0)
MessageBox.Show("Vertices cannot be less than 0", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
if (edges < 0 || edges > 1)
MessageBox.Show("Edges have to be 0..1", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
//MessageBox.Show("Graph successfully created", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
for (int i = 0; i < vertices; i++)
{
Vertex vertex = new Vertex();
Vertex vertex1 = vertex.CreateVertex(vertices);
vertexList.Add(vertex1);
}
VertexDisplay display = new VertexDisplay();
display.DisplayVertices(vertexList);
display.Show();
}
}
catch(Exception x)
{
MessageBox.Show("Vertices or edges Cannot be left out", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
顶点类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace assignment
{
public class Vertex
{
public int x;
public int y;
public int z;
public int vertexID;
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
public int Z
{
get { return z; }
set { z = value; }
}
public int VertexID
{
get { return vertexID; }
set { vertexID = value; }
}
public Vertex CreateVertex(int noOfVertex)
{
Random rnd = new Random();
Vertex vertex = new Vertex(x, y, z, vertexID);
MainForm mainform = new MainForm();
vertex.x = rnd.Next(0, 100);
vertex.y = rnd.Next(0, 100);
vertex.z = rnd.Next(0, 100);
vertex.vertexID = rnd.Next(0, noOfVertex);
return(vertex);
}
public Vertex()
{
}
public Vertex(int x, int y, int z, int id)
{
this.x = x;
this.y = y;
this.z = z;
this.vertexID = id;
}
}
}
显示自
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace assignment
{
public partial class VertexDisplay : Form
{
public VertexDisplay()
{
InitializeComponent();
MainForm mainform = new MainForm();
DisplayVertices(mainform.vertexList);
}
public void DisplayVertices(List<Vertex>vertexList)
{
int x, y, z, id;
foreach(Vertex vertexDetails in vertexList)
{
x = vertexDetails.X;
y = vertexDetails.Y;
z = vertexDetails.Z;
id = vertexDetails.VertexID;
txtVertexDisplay.Text = string.Join(Environment.NewLine, " ID: " + id + " x: " + x + " y: " + y + " z: " + z);
}
}
}
}
答案 0 :(得分:0)
您正在Foreach循环中设置txtVertexDisplay.Text属性,因此您每次循环迭代都会设置此属性。它不是连接它总是被设定为完全的东西。如果要连接输出 - 使用一些临时字符串属性和+ =运算符。见下文:
public void DisplayVertices(List<Vertex>vertexList)
{
String displayText = string.Empty;
int x, y, z, id;
foreach(Vertex vertexDetails in vertexList)
{
x = vertexDetails.X;
y = vertexDetails.Y;
z = vertexDetails.Z;
id = vertexDetails.VertexID;
displayText += string.Join(Environment.NewLine, " ID: " + id + " x: " + x + " y: " + y + " z: " + z);
}
txtVertexDisplay.Text = displayText;
}