1

Can anyone help me use a message box to display the random number and the square in two columns with a label for each?

    const int NUM_ROWS = 10;
    const int NUM_COLS = 2;

        int[,] randint = new int [NUM_ROWS,NUM_COLS];
        Random randNum = new Random();

        for (int row = 0; row < randint.GetLength(0); row++)
        {
            randint[row,0] = randNum.Next(1,100);
            randint[row,1] = randint[row,0]*randint[row,0];

        Console.Write(string.Format("{0,5:d} {1,5:d}\n", randint[row,0], randint[row,1]));
Syed Osama Maruf
  • 1,895
  • 2
  • 20
  • 37
KBS
  • 13
  • 1
  • 2
  • I have only worked in the console...can you point me to where I have to go? I have messed around with forms, but I don't know how to embed the code behind the form – KBS Mar 28 '15 at 04:20
  • @utility We can add a message box to a console app.@KBS See my answer. – Syed Osama Maruf Mar 28 '15 at 07:39

3 Answers3

3

I have achieved it by adding reference of System.Windows.Forms to my console application and got the result you desired. Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            const int NUM_ROWS = 10;
            const int NUM_COLS = 2;

            int[,] randint = new int[NUM_ROWS, NUM_COLS];
            Random randNum = new Random();

            for (int row = 0; row < randint.GetLength(0); row++)
            {
                randint[row, 0] = randNum.Next(1, 100);
                randint[row, 1] = randint[row, 0] * randint[row, 0];

                Console.Write(string.Format("{0,5:d} {1,5:d}\n", randint[row, 0], randint[row, 1]));

                MessageBox.Show(string.Format("{0,5:d} {1,5:d}\n", randint[row, 0], randint[row, 1]));
                Console.ReadKey();
            }
        }
    }
}  

My output:
Output Also though this is not asked but just in case to add reference to System.Windows.Form look right click on the references in your solution explorer and select .Net tab and then press ok after selecting the desired dll. Cheers!

Adding ref Selecting desired reference

Syed Osama Maruf
  • 1,895
  • 2
  • 20
  • 37
1

You can do it like this.

MessageBox.Show(string.Format("{0,5:d} {1,5:d}\n", randint[row, 0], randint[row, 1]), "Message Box",
                                     MessageBoxButtons.YesNo,
                                     MessageBoxIcon.Question);

If you place this line inside the for loop a message box will be displayed for every iteration. If you click Yes each time, a new message box with the old and new values will be displayed.

If you want to display the entire array then it will be something like this.

string data = "";

for (int row = 0; row < randint.GetLength(0); row++)
{
     randint[row, 0] = randNum.Next(1, 100);
     randint[row, 1] = randint[row, 0] * randint[row, 0];
     data += string.Format("{0,5:d} {1,5:d}\n", randint[row, 0], randint[row, 1]);
}

MessageBox.Show(data, "Data", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
Ayan
  • 797
  • 1
  • 11
  • 18
  • This is where i get lost, i am getting MessageBox, icons buttons do not exist in current context. I heard what @Utility said, but it feels like i'm working backwards. So, I should create the form first? or can i just dump this array behind the form? uggh plz help. I know how to create the form, its getting the form to display my content – KBS Mar 28 '15 at 05:06
  • Yes you can dump the array behind the form. Actually it depends on what you are trying to do. You can create a form with a button and by clicking the button you could display the message box. you will just need to dump the entire code in the button's onClick event handler function. Or you could simply display the array in the form using a TextBox. There are many ways you could accomplish this. – Ayan Mar 28 '15 at 05:19
  • 1
    @Ayan this can be achieved from a console app. See my answer. – Syed Osama Maruf Mar 28 '15 at 07:47
  • Yeah I completely forgot about that. It can be done by adding the Forms reference. Thanks! – Ayan Mar 28 '15 at 07:59
0

Start you project in :

Windows Forms Application -> C#

You can use MessageBox to help you solve your display content.

Tharif
  • 13,794
  • 9
  • 55
  • 77