c# - Display brings up only one object of the list and list isn't adding properly -
so trying project in windows form determine number of vertices , assigns each vertex co-ordinate , outputs on form. reason isn't working take look?
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 less 0", "error", messageboxbuttons.ok, messageboxicon.error); else if (edges < 0 || edges > 1) messagebox.show("edges have 0..1", "error", messageboxbuttons.ok, messageboxicon.error); else { //messagebox.show("graph created", "info", messageboxbuttons.ok, messageboxicon.information); (int = 0; < 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 left out", "error", messageboxbuttons.ok, messageboxicon.error); } } } }
vertex class
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 { { return x; } set { x = value; } } public int y { { return y; } set { y = value; } } public int z { { return z; } set { z = value; } } public int vertexid { { 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; } } }
display from
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); } } } }
you setting txtvertexdisplay.text property inside foreach loop setting each time loop iterate. not concatenation it's beeing set something. if want concatenate output - use temporary string property , += operator. see below:
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; }
Comments
Post a Comment