How to Display a List of Points in WPF
- 1). Open Visual Studio 2010 by clicking on its program icon. When it loads, select "File" followed by "New" and "Project." In the column furthest to the left, check "Visual C#.," In the right-hand side of the application, select "WPF application." A new WPF Application project is created, and a blank form appears in the main editor window.
- 2). Locate the panel labeled "Toolbox," which can be on either the left-hand or right-hand side of the screen. The "Toolbox" contains a list of many different graphical user interface components.
- 3). Click and drag the component labeled "TextBox" from the "Toolbox" onto the blank form. Release the mouse to place the "TextBox" onto the form.
- 4). Click on the menu item labeled "View" from the menu at the top of the main editor window. A sub-menu appears. Select on "Code" to view the code associated with this project.
- 5). Locate the text labeled "MainWindow()." There is a set of curly brackets immediately following this text, which is where all the code for this project will go.
- 6). Create a list of "Point" objects by writing the following line of code:
List<Point> points = new List<Point>(); - 7). Add some "Point" objects to the list. Each "Point" object can be given X and Y coordinates when it is created. For example, to create three points with the coordinates (1,1), (2,2), and (3,3), write the following lines:
points.Add(new Point(1,1));
points.Add(new Point(2, 2));
points.Add(new Point(3, 3)); - 8). Iterate through the list of "Points" using a "foreach" loop. Each "Point" object will be visited once, allowing you to access the data it holds. Write the following statement to iterate through all the items in the list:
foreach (Point current in points)
{} - 9). Display the coordinates of the currently visited "Point." You can display the values of the "Point" in the "TextBox" created in Step 3. Write the following statements between the curly brackets of the "foreach" loop:
textBox1.Text += "X Coordinate: " + current.X;
textBox1.Text += "Y Coordinate: " + current.Y;
textBox1.Text += "\n"; - 10
Execute the program by pressing the green "Play" button located at the top of the window. A WPF form appears, and it displays all of the coordinates from the list of "Points."