簡體   English   中英

公共重寫字符串toString,ArrayList和Listbox輸出多行?

[英]Public override string toString, ArrayList and Listbox output in multiplelines?

我整夜都在為這個C#問題苦苦掙扎。

我有一個重寫ToString(),它工作正常,我可以將數據放入ListBox中。 但是由於數據非常長,並且帶有許多類,因此輸出變長。 我希望能夠將ListBox輸出分成多行。

這是類文件中的替代:

//ToString
public override string ToString()
{
    return  "Name " + firstName + lastName + ". Nationality " + nationality + ". Lives in " + address + " " + zipCode + " " + city + " " + country + "."//
        + " Height is " + height + " meters. Hair color is " + hairColor + " and eye color is " + eyeColor + ". Specialmarkings: "//
        + specialMark + ". Is associated with " + association + ". Codename is " + codeName + "Photo (filename): " + photo;

}

這是索引代碼:

public partial class Index : System.Web.UI.Page
{
    static ArrayList personarraylist;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            personarraylist = new ArrayList();
        }
    }

    protected void ButtonCreate_Click(object sender, EventArgs e)
    {
        //create new object
        Person p = new Person(TextBox1FirstName.Text, TextBox2LastName.Text, TextBox3Nation.Text, TextBox4Address.Text, //
            TextBox5City.Text, TextBox7Country.Text, //
            TextBox10HairColor.Text, TextBox11EyeColor.Text, TextBox12SpecialMark.Text, TextBox13Asso.Text, TextBox14Codename.Text, TextBox15Photo.Text, //
            Convert.ToDouble(TextBox9Height.Text), Convert.ToInt32(TextBox6ZipCode.Text), Convert.ToInt32(TextBox8Pass.Text));
        //add object to arraylist
        personarraylist.Add(p);
    }

    protected void ButtonShow_Click(object sender, EventArgs e)
    {
        //clear list box
        ListBox1.Items.Clear();

        //loop through Arraylist
        for (int i = 0; i < personarraylist.Count; i++)
        {
            ListBox1.Items.Add(personarraylist[i].ToString());
            ListBox1.Items.Add("");
            TextBox1.Text = "";
        }
    }
}

是否可以在ListBox中以多行中斷輸出? 我試圖在重寫返回中插入一些html中斷標簽,但是這些標簽被剝離了,是的,這是一個web應用程序。

在此先感謝您的時間。 PS我是C#(學生)的新手,所以要友善;)

更新:再次感謝您的幫助,我已經嘗試使用Environment.Newline和其他解決方案,但是當在ListBox中顯示文本時,這些方法似乎被忽略了。 我可以在后面的代碼中看到斷點,但是在瀏覽器中,列表框仍然只將它們全部放在一行中。 因此,我決定改用TextBox,它會自動斷開文本以及指向的位置。

//loop through Arraylist
        for (int i = 0; i < personarraylist.Count; i++)
        {
            TextBox1.Text += personarraylist[i].ToString();
        }

再次感謝您的幫助:-)

您可以使用Environment.NewLine或簡單地使用"\\n"來創建多行文本。

如果這不起作用,則可以嘗試使用DataList控件:

<asp:DataList id="myDataList" runat="server">
    <ItemTemplate>
        Line 1
        <br />
        Line 2
    </ItemTemplate>
</asp:DataList>

暫無
暫無

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

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