How to put the result in a graphical interface?

I wrote my code to make Magic Square on a console window, but now I want to display my results in a graphical interface. Like this:

enter image description here

Basically, I'm trying to add 2D graphics to the magic square code that I wrote. So that the numbers and the grid are shown as the image above.

How can I do it? I am not familiar with the GUI yet. Any tips or suggestions on how to get this code started?

This is my code for the magic square that I wrote for the console window:

int n = 0; // initializing to zero Console.WriteLine("\t\t\t\t~Magic Square~\n"); Console.Write("Enter an Odd Number for the Magic Square: "); n = Convert.ToInt32(Console.ReadLine()); // users' input for "n" Console.WriteLine(""); // space int squareSizeN = n * n; int row = 0; int column = (n / 2); int[,] magicSquare = new int[n, n]; // [rows, columns] for (int i = 1; i <= squareSizeN; i++) { magicSquare[row, column] = i; row--; column++; if (i % n == 0) { row += 2; column -= 1; } else { column = (column + n) % n; row = (row + n) % n; } } for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { Console.Write(Convert.ToString(magicSquare[j,k]).PadLeft((Convert.ToString(squareSizeN).Length), ' ') + " "); } Console.WriteLine("\n"); } 
0
source share
1 answer

This is a very broad question. I will try to guide you as best as possible

  • Run the WinForms Project
  • In the Designer view, drag a DataGridView (from Toolbox) into the form
  • Right click on DataGridView, select "Add Column" and add 3 columns
  • Back in Designer, click on your form1 (outside your dataGridView),
  • Look at the bottom right “Properties”, click on the “Lightning” symbol (“Events”), find “Download” and double-click on the empty box except for it.
  • It should lead you to the code view and add the Form1_Load stub method
  • You should see something like this:

     public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } } 
  • In the Form1_Load method, add your code from the console application.

  • In the method Form1_Load add 3 rows to dataGridView1

  • Put your results from your code in dataGridView1

      private void Form1_Load(object sender, EventArgs e) { //Copy or call your Console Application code here dataGridView1.Rows.Add(3); //Add 3 rows to dataGridView1 dataGridView1.Rows[0].Cells[0].Value = 1; //your row 1, column 1 result dataGridView1.Rows[0].Cells[1].Value = 2; //your row 1, column 2 result //... etc } 
  • Compile and run! (and good luck:)

+1
source

Source: https://habr.com/ru/post/1236997/


All Articles