簡體   English   中英

將結果輸入多行文本框

[英]strings results into multiline textbox

我一直在嘗試在我的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;
}

這條線

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

每次執行時都會覆蓋文本框中的文本。 您真正想要做的是將每個新字符串附加到文本框中已經存在的文本中:

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

此外,如果找到的主體數量相對較大,則使用StringBuilder可能會為您提供更好的性能:

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

textBox1.Text = text.ToString();

您每次通過循環都將覆蓋文本。

在多行模式下,使用Lines屬性而不是Text MSDN

在foreach循環中,您每次都在重置文本。 因此,它將僅保留最終循環的值。

嘗試執行Andrei提到的操作,否則您可以將所有響應附加到字符串/字符串生成器,然后將最終文本分配給文本框。

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

textBox1.Text = str;

要么

StringBuilder sb = new StringBuilder();

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

textBox1.Text = sb.ToString();

您只是重復地將文本框的Text屬性設置為最后一個oResult.Name

相反,您需要附加它。 就像是

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

就是說,如果您要進行大量此類操作,出於性能原因,您可以考慮使用StringBuilder。 像這樣:

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

我想你的問題在這里。

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

在該foreach中,您將文本框值僅分配給當前的循環項目值。

嘗試類似

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

而且,您在該方法中做了兩件事,所以您隱藏了在另一個方法中填充文本框的事實。

這行:textBox1.Text = oResult.Name.ToString();

如果要遍歷集合中的每個主體,則將文本替換為最后一個主體。 為了簡化起見,請使用串聯:textBox1.Text + ='\\ n'+ oResult.Name.ToString();

如果您擔心資源或性能問題,請使用StringBuilder類。

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

首先,不要以編程方式在設計器中放置多行,而是選擇文本框,然后單擊右上角的箭頭並選擇多行,然后將文本框擴展到所需的大小,然后在foreach循環中:

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

如果僅在代碼中設置多行,則文本框將僅顯示第一行,這使得您必須在下面放置類似內容才能查看結果textBox1.ScrollBars = ScrollBars.Vertical; ,這會使文本框像一個數值上移控件(因為您沒有在設計器中選擇多行,所以文本框只是第一行),這沒有任何意義,因此最好在設計器中選擇多行並調整其大小首先控制。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM