【问题标题】:Undesired output from foreach loop and IF statementforeach 循环和 IF 语句的不需要的输出
【发布时间】: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


【解决方案1】:

将 DisplayType = cmdType.SelectedIndex 添加到 AddEntry

尝试使用字符串生成器?

StringBuilder sb = new StringBuilder(mainWindow.ChangeTextBox);
foreach (AddEntry list in addedEntry)
{
    sb.AppendLine(list.Type);

    if (list.DisplayType == 1) 
        sb.AppendLine("URL: " + list.URL);

    if (list.DisplayType == 0 || list.DisplayType == 1) {
        sb.AppendLine("User Name: " + list.UserName);
        sb.AppendLine("Password: " + list.Password);
    }

    if (list.DisplayType == 2) {
        sb.AppendLine("Software Name: " + list.SoftwareName);
        sb.AppendLine("Serial Code: " + list.SerialCode);
        sb.AppendLine("Software Name: " + list.SoftwareName);
    }

    sb.AppendLine();
}  

mainWindow.ChangeTextBox = sb.ToString();

注意临时变量

【讨论】:

  • 还是一样的结果。除了使用 IF 语句还有其他选择吗?
  • 当从不同类型的组合框中添加另一个条目时,它仍然会添加不需要的行。
  • @HelpNeeder - 嗯..你能显示更多的代码吗?我认为您的问题出在其他地方
  • @HelpNeeder - 每次添加另一个条目时,您都会阅读所有文本,这意味着第二次 cmbType.SelectedIndex 为 1。显示 URL。您可以在包含 cmdType.SelectedIndex 状态的 AddEntry 类中添加另一个属性。并改用它。
  • 根据经验,它与使用对象本身没有太大区别。正如我所看到的,问题出现了,当我选择 SelectedIndex 时,所有条目而不是保留它们的旧值,它们会切换到显示不同字段内容的新值。
【解决方案2】:

每个循环都向输出添加更多文本。将结果添加到 StringBuilder 并显示其输出。

StringBuilder sb = new StringBuilder(); sb.Append("你的文字"); sb.Append("更多文字"); MainWindow.ChangeTextBox = sb.ToString()

还有为什么不将您的代码包含在一个块中:

if (cmbType.SelectedIndex == 2) { //string builder recommended instead but.... mainWindow.ChangeTextBox += "Software Name: " + list.SoftwareName + Environment.NewLine; mainWindow.ChangeTextBox += "Serial Code: " + list.SerialCode + Environment.NewLine; }

【讨论】:

  • 这仍然带来相同的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 2019-12-12
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
相关资源
最近更新 更多