Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
361 views
in Technique[技术] by (71.8m points)

c# - Undesired output from foreach loop and IF statement

I am having a problem with formatting the output from foreach loop. What should I do to format output as showed below the code? I am currently using following code:

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

First output:

PC Password
User Name: a
Password: b

Then adding another entry...

Second output:

PC Password
URL: e // this should not be here
User Name: a
Password: b

Web Site Password
URL: www.
User Name: www
Password: www

Second output should be:

PC Password
User Name: a
Password: b

Web Site Password
URL: www.
User Name: www
Password: www

Hope for some tips.

Regards.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add DisplayType = cmdType.SelectedIndex to the AddEntry

Tried using a String Builder?

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

Notice the temp variable


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...