简体   繁体   中英

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

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

You are overwriting the text each time through the loop.

Use the Lines property instead of Text for multiline mode. ( MSDN )

In your foreach loop you are resetting your text everytime. So it will hold value of final loop only.

Try doing what Andrei has mentioned or you can append all response to a string / string builder and assign the final text to the textbox.

string str;
foreach(...){
  str += oResult.Name.ToString();
} 

textBox1.Text = str;

OR

StringBuilder sb = new StringBuilder();

foreach(...){
  sb.Append(oResult.Name.ToString());
} 

textBox1.Text = sb.ToString();

You're just repeatedly setting the Text property of the textbox to the last oResult.Name .

Instead, you need to append it. Something like

textBox1.Text = textBox1.Text + oResult.Name + Environment.NewLine;

That said, if you're going to be doing a ton of these, you might consider using a StringBuilder for performance reasons. Something like this:

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

I think your issue is here..

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

In that foreach you are assigning the text box value to just the current looped items value.

Try something like

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

And also, you are doing 2 things in that method, so you are hiding the fact that you are populating the text box within another method.

This line: textBox1.Text = oResult.Name.ToString();

If you are for'ing through each Principal in your collection, you will replace the text with the last Principal. Use concatenation for simplicity: textBox1.Text += '\\n' + oResult.Name.ToString();

And use the StringBuilder class if you are worried about resources or performance issues.

public ArrayList GetUserGroups(string sUserName) {

        ArrayList myItems = new ArrayList();
        UserPrincipal oUserPrincipal = GetUser(sUserName);

        PrincipalSearchResult<Principal> oPrincipalSearchResult = oUserPrincipal.GetGroups();
        StringBuilder text = new StringBuilder();
        foreach (Principal oResult in oPrincipalSearchResult)
        {
            myItems.Add(oResult.Name);
            text.Append(oResult.Name);
            text.AppendLine();
        }
        textBox1.Text = text.ToString();
        return myItems;
    }

First instead of placing multiline programmaticly in designer select the textbox and click the upperright arrow and select multiline and then expand your textbox to the desired size,then inside foreach loop:

    foreach (Principal oResult in oPrincipalSearchResult)
    {
        myItems.Add(oResult.Name);
        textBox1.AppendText(oResult.Name + Environment.NewLine);
    }

If you only set multiline in code your textbox will only display the first line making you have to place something like this next to view the results textBox1.ScrollBars = ScrollBars.Vertical; ,and that would make the textbox like a numericupdown control(since you didnt select multiline in designer the textbox would be just first line),and that does not make sense,so its better in your case to select multiline in the designer and resize the control first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM