【问题标题】:c# How do i display this array in a messagebox (console application)c#如何在消息框中显示这个数组(控制台应用程序)
【发布时间】:2015-03-28 04:03:55
【问题描述】:

谁能帮我用一个消息框在两列中显示随机数和正方形,每列都有一个标签?

    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]));

【问题讨论】:

  • 我只在控制台工作过...你能指出我必须去的地方吗?表单弄乱了,但不知道如何在表单后面嵌入代码
  • @utility 我们可以在控制台应用程序中添加一个消息框。@KBS 看我的回答。

标签: c# arrays messagebox


【解决方案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 后按确定。干杯!

【讨论】:

  • @KBS PS 虽然您可以删除 Console.ReadKey();行:)
  • @KBS 如果这是您想要的,请务必将其标记为答案 :)
【解决方案2】:

你可以这样做。

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);

【讨论】:

  • 这是我迷路的地方,我得到了 MessageBox,图标按钮在当前上下文中不存在。我听到@Utility 说了什么,但感觉就像我在倒退。那么,我应该先创建表单吗?或者我可以把这个数组转储到表格后面吗? ugh请帮助。我知道如何创建表单,它让表单显示我的内容
  • 是的,您可以将数组转储到表单后面。实际上,这取决于您要做什么。您可以创建一个带有按钮的表单,然后单击该按钮可以显示消息框。您只需将整个代码转储到按钮的 onClick 事件处理函数中。或者您可以简单地使用 TextBox 在表单中显示数组。有很多方法可以做到这一点。
  • @Ayan 这可以通过控制台应用程序实现。看我的回答。
  • 是的,我完全忘记了这一点。可以通过添加表单引用来完成。谢谢!
【解决方案3】:

开始你的项目:

Windows Forms Application -> C#

您可以使用MessageBox来帮助您解决您的显示内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多