在C#中创建精美的标签文本输出 [英] Create nicely tabbed text output in C#

查看:87
本文介绍了在C#中创建精美的标签文本输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法改变了我的代码,现在它运行得更好,我唯一的问题是我不能每天/每周使用两次。

我希望它输出如下:http:/ /i.stack.imgur.com/LZu7u.jpg[^]



这是我的代码:





I managed to change my code up and now it is running better, my only issue is that i cant use day/week twice.
I want it to output looking like this: http://i.stack.imgur.com/LZu7u.jpg[^]

Here is my code:


int[,] toys = new int[5, 4];
            for (int week = 0; week <= 3; week++)
            {
                for (int day = 0; day <= 4; day++)
                {
                    int tempVal = 0;
                    if (int.TryParse(Microsoft.VisualBasic.Interaction.InputBox("Please enter value for Day " + (day + 1) + " in week " + (week + 1) + "."), out tempVal))
                    {
                        toys[day, week] = tempVal;
                    }
                    else
                    {
                        MessageBox.Show("Invalid entry");
                        day--;
                        continue;
                    }

txtOutput.Text += "\t\t" + "Mon" + "\t" + "Tue" + "\t" + "Wed" + "\t" + "Thu" + "\t" + "Fri" + "\t" + "\r\n";
     //here it says "A local variable named 'week' cannot be declared in this scope because it would give a different meaning to 'week', which is already used in a 'parent or current' scope to denote something else" For day & week
              
for (int week = 0; week <= 3; week++)//foreach week
                   {
                       //construct the line of text which represents the week's data
                       txtOutput.Text += "\tWeek " + (week + 1) + "\t";
                       for (int day = 0; day <= 4; day++)
                       {
                           txtOutput.Text += toys[day, week];
                           if (day != 4)
                           {
                               //so long as it is not the last day, then it tabs over
                               txtOutput.Text += "\t";
                           }
                       }
                       //moving over to the next line
                       txtOutput.Text += "\r\n\r\n";
                   }

推荐答案

看一下string.Format ...



这是一个简单的版本:



Take a look at string.Format...

Here's an easy version of it:

string headerFormat = "\t\tMon\tTue\tWed\tThur\tFri\n";
string lineFormat = "Week {0}\t{1}\t{2}\t{3}\t{4}\t{5}\n";

textbox1.Text = headerFormat;
for (int week = 0; week < 4; week++)
{
    textbox1.AppendText(string.Format(lineFormat,
        week, toys[week, 0], toys[week, 1], toys[week, 2] toys[week, 3]
        toys[week, 4]));
}





这是最简单的方法,您可以使用每个字段的格式字符串来制作它们如果他们没有排队。



Thats the easiest way to do it, you can play around with the format strings for each field to make them line up if they don't.


这篇关于在C#中创建精美的标签文本输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆