Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 来自foreach循环和IF语句的意外输出_C#_If Statement_Foreach_Arraylist - Fatal编程技术网

C# 来自foreach循环和IF语句的意外输出

C# 来自foreach循环和IF语句的意外输出,c#,if-statement,foreach,arraylist,C#,If Statement,Foreach,Arraylist,我在格式化foreach循环的输出时遇到问题。我应该如何格式化输出,如下面的代码所示?我目前正在使用以下代码: foreach (AddEntry list in addedEntry) { // Displaying and formating the output in text box in MainWindow. mainWindow.ChangeTextBox += lis

我在格式化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;
            }  
第一个输出:

然后添加另一个条目

第二输出:

第二个输出应为:

希望得到一些提示


注意。

将DisplayType=cmdType.SelectedIndex添加到附录中

是否尝试使用字符串生成器

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

请注意,每次向输出中添加更多文本时,都会使用temp变量。将结果添加到StringBuilder并显示其输出

StringBuilder sb = new StringBuilder(); sb.Append("your text"); sb.Append("more text"); MainWindow.ChangeTextBox = sb.ToString()
也许是个愚蠢的问题,但是:您是否验证了cmbType.SelectedIndex!=1?@Jonathan Newmuis:更正我已经仔细阅读了你的问题。我没有。这会导致问题吗?你真的应该在这里解决这个问题,而不是在同一件事上发布另一个问题。人们把他们的时间花在自由思考你的问题上来帮助你。@Adam Tuliper:我在这里得到的回答没有解决我的问题,甚至不接近。好吧,首先这里的细节相当糟糕,你没有编辑和清理这个问题,而是放弃了这个问题,转到了一个新的问题,而这里有几个人在利用他们的时间帮助你解决问题。未来问题的糟糕形式。这仍然带来相同的结果。仍然相同的结果。除了使用IF语句外,还有其他选项吗?当从不同类型的组合框中添加另一个条目时,它仍然会添加不需要的行。@HelpNeeder-umm。。你能给我看更多的代码吗。我认为你的问题在于别的where@HelpNeeder-每次添加另一个条目时,您都会阅读所有文本,这意味着第二次在cmbType周围。SelectedIndex为1。显示URL。您可以在AddEntry类中添加另一个属性,该类保存cmdType.SelectedIndex的状态。根据经验,它和使用对象本身并没有太大区别。在我看来,问题出现了,当我选择SelectedIndex时,所有条目都会切换到显示不同字段内容的新条目,而不是保留它们的旧值。
PC Password
User Name: a
Password: b

Web Site Password
URL: www.
User Name: www
Password: www
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();
StringBuilder sb = new StringBuilder(); sb.Append("your text"); sb.Append("more text"); 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;
}