简体   繁体   English

将结果输入多行文本框

[英]strings results into multiline textbox

I am been trying to get multiple results in my TextBox and I can't get it right. 我一直在尝试在我的TextBox中获得多个结果,但无法正确显示。 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可能会为您提供更好的性能:

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. 在多行模式下,使用Lines属性而不是Text ( MSDN ) MSDN

In your foreach loop you are resetting your text everytime. 在foreach循环中,您每次都在重置文本。 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. 尝试执行Andrei提到的操作,否则您可以将所有响应附加到字符串/字符串生成器,然后将最终文本分配给文本框。

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 . 您只是重复地将文本框的Text属性设置为最后一个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. 就是说,如果您要进行大量此类操作,出于性能原因,您可以考虑使用StringBuilder。 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. 在该foreach中,您将文本框值仅分配给当前的循环项目值。

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(); 这行: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(); 为了简化起见,请使用串联:textBox1.Text + ='\\ n'+ oResult.Name.ToString();

And use the StringBuilder class if you are worried about resources or performance issues. 如果您担心资源或性能问题,请使用StringBuilder类。

public ArrayList GetUserGroups(string sUserName) { 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循环中:

    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; 如果仅在代码中设置多行,则文本框将仅显示第一行,这使得您必须在下面放置类似内容才能查看结果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. ,这会使文本框像一个数值上移控件(因为您没有在设计器中选择多行,所以文本框只是第一行),这没有任何意义,因此最好在设计器中选择多行并调整其大小首先控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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