【发布时间】:2011-12-20 00:55:43
【问题描述】:
我在格式化 foreach 循环的输出时遇到问题。我应该怎么做才能格式化输出,如下所示?我目前正在使用以下代码:
foreach (AddEntry list in addedEntry)
{
// Displaying and formating the output in text box in MainWindow.
mainWindow.ChangeTextBox += list.Type + Environment.NewLine;
if (cmbType.SelectedIndex == 1)
mainWindow.ChangeTextBox += "URL: " + list.URL + Environment.NewLine;
if (cmbType.SelectedIndex == 2)
mainWindow.ChangeTextBox += "Software Name: " + list.SoftwareName + Environment.NewLine;
if (cmbType.SelectedIndex == 2)
mainWindow.ChangeTextBox += "Serial Code: " + list.SerialCode + Environment.NewLine;
if (cmbType.SelectedIndex == 0 || cmbType.SelectedIndex == 1)
mainWindow.ChangeTextBox += "User Name: " + list.UserName + Environment.NewLine;
if (cmbType.SelectedIndex == 0 || cmbType.SelectedIndex == 1)
mainWindow.ChangeTextBox += "Password: " + list.Password + Environment.NewLine;
mainWindow.ChangeTextBox += Environment.NewLine;
}
第一个输出:
PC Password
User Name: a
Password: b
然后添加另一个条目...
第二次输出:
PC Password
URL: e // this should not be here
User Name: a
Password: b
Web Site Password
URL: www.
User Name: www
Password: www
第二个输出应该是:
PC Password
User Name: a
Password: b
Web Site Password
URL: www.
User Name: www
Password: www
希望得到一些提示。
问候。
【问题讨论】:
-
也许是个愚蠢的问题,但是:你验证过
cmbType.SelectedIndex != 1吗? -
@Jonathan Newmuis:更正我已经阅读了你的问题。我没有。这会导致问题吗?
-
你真的应该在这里解决这个问题,而不是在同一件事上发布另一个问题。人们将他们的时间(免费提醒您)投入到您的问题中来帮助您。
-
@Adam Tuliper:我在这里得到的回复并没有解决我的问题,甚至没有接近。
-
好吧 - 首先这里的细节相当糟糕,而不是编辑和清理这个你放弃了这个问题并去一个新的问题,而你有几个人在这里利用他们的时间来帮助你解决你的问题.未来问题的错误形式。
标签: c# if-statement foreach arraylist