Search code examples
c#.netwindows.net-3.5

strings results into multiline textbox


I am been trying to get multiple results in my TextBox and I can't get it right. Could someone please show me an example of displaying this array into a textbox?

public ArrayList GetUserGroups(string sUserName)
{
    textBox1.Multiline = true;
    ArrayList myItems = new ArrayList();
    UserPrincipal oUserPrincipal = GetUser(sUserName);

    PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups();
    textBox1.Multiline = true;
    foreach (Principal oResult in oPrincipalSearchResult)
    {
        myItems.Add(oResult.Name);
        textBox1.Text = oResult.Name.ToString();
    }
    return myItems;
}

Solution

  • This line

    textBox1.Text = oResult.Name.ToString();
    

    overrides text in the textbox each time it is executed. What you really might want to do is to append each new string to the text that is already in the textbox:

    textBox1.Text += oResult.Name.ToString() + Environment.NewLine;
    

    Furthermore, if number of found principals is relatively large, making use of StringBuilder might give you better performance:

    StringBuilder text = new StringBuilder();
    foreach (Principal oResult in oPrincipalSearchResult)
    {
        myItems.Add(oResult.Name);
        text.Append(oResult.Name);
        text.AppendLine();
    }
    
    textBox1.Text = text.ToString();