-2

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.

Dori
  • 915
  • 1
  • 12
  • 20
HelpNeeder
  • 6,383
  • 24
  • 91
  • 155
  • Perhaps a stupid question, but: have you verified that `cmbType.SelectedIndex != 1`? – Jon Newmuis Nov 02 '11 at 05:21
  • @Jonathan Newmuis: Correction I have over read your question. I don't have it. Would that cause the problem? – HelpNeeder Nov 02 '11 at 06:29
  • 2
    you really should've resolved this here rather than post another question on the same thing. people put their time (for free mind you) into your question here to help you. – Adam Tuliper Nov 02 '11 at 14:22
  • @Adam Tuliper: The responses I got here did not solve my problem, wasn't even close. – HelpNeeder Nov 03 '11 at 05:57
  • 1
    well - first the detail here was fairly bad,rather than edit and clean this up you dropped this question and went to a new one while you had several individuals here using their time to help you solve your problem. bad form for future questions. – Adam Tuliper Nov 03 '11 at 16:28

2 Answers2

0

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

Chris Kolenko
  • 1,020
  • 17
  • 32
  • Still same result. Is there other option than using IF statement? – HelpNeeder Nov 02 '11 at 05:40
  • It still adds unneeded line when another entry is added from different type of combo box. – HelpNeeder Nov 02 '11 at 05:50
  • @HelpNeeder - umm.. could you show more of the code. I think your problem lies else where – Chris Kolenko Nov 02 '11 at 06:03
  • @HelpNeeder - Every time you add another entry you readd all the text, meaning on the second time around the cmbType.SelectedIndex is 1. Which is showing the URL. You could add another Property inside the AddEntry class which holds the state of cmdType.SelectedIndex. and use that instead. – Chris Kolenko Nov 02 '11 at 06:24
  • By experience it doesn't much different than using object itself. The problem arises, as I see it, when I choose SelectedIndex then all entries instead keeping their old value they switch to new which display content of different fields. – HelpNeeder Nov 02 '11 at 06:31
  • look at my OP. I have found an answer on another post. – HelpNeeder Nov 02 '11 at 07:31
0

Every loop you are adding more text to the output. Add your results to a StringBuilder and thren display its output.

StringBuilder sb = new StringBuilder();
sb.Append("your text");
sb.Append("more text");
MainWindow.ChangeTextBox = sb.ToString()

also why not include your code in a block:


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


Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71