[英]c# How do i display this array in a messagebox (console application)
誰能幫助我使用消息框在兩列中分別顯示隨機數和正方形並分別帶有標簽?
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]));
通過將對System.Windows.Forms的引用添加到我的控制台應用程序中,並獲得所需的結果,可以實現此目的。 這是我的代碼:
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();
}
}
}
}
我的輸出: 另外,雖然不要求這樣做,但以防萬一要添加對System.Windows.Form的引用,請右鍵單擊解決方案資源管理器中的引用,然后選擇.Net選項卡,然后在選擇所需的dll之后按OK。 干杯!
您可以這樣做。
MessageBox.Show(string.Format("{0,5:d} {1,5:d}\n", randint[row, 0], randint[row, 1]), "Message Box",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
如果將此行放在for
循環中,則每次迭代都會顯示一個消息框。 如果每次單擊“是”,將顯示一個包含舊值和新值的新消息框。
如果要顯示整個數組,則將是這樣。
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);
在以下位置開始您的項目:
Windows Forms Application -> C#
您可以使用MessageBox
幫助您解決顯示內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.