1
我一直在努力解決這個C#問題。公共覆蓋字符串toString,ArrayList和Listbox以多行輸出?
我有一個重寫ToString(),它工作正常,我可以把我的數據放在列表框中。但是由於數據很長,有很多類,輸出變長。 我希望能夠將我的列表框輸出分成多行。
這裏是在類文件倍率:
//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 = "";
}
}
}
是否有可能打破multiplelines輸出在ListBox? 我正試圖在覆蓋返回中注入一些html breaktags,但是這些被剝離,是的這是一個web應用程序。
提前感謝您的時間。 PS我在C#(學生)一個新手,所以善待;)
UPDATE: 嗨再次一切,THX的幫助,我已經嘗試過用Environment.Newline和其他的解決方案,但這些似乎在ListBox中顯示文本時被忽略。我可以在代碼隱藏中看到斷點,但在瀏覽器中,列表框仍然只保留一行。所以我決定使用一個TextBox來代替它,它會自動地分割文本以及我指出的地方。
//loop through Arraylist
for (int i = 0; i < personarraylist.Count; i++)
{
TextBox1.Text += personarraylist[i].ToString();
}
再次THX的幫助:-)
嘗試'\ r \ N':
如果不工作,你可以嘗試使用
DataList
控制。我*認爲*會起作用 – Prescott 2013-03-10 00:00:40在你想要的地方嘗試一個'Environment.NewLine'。這應該有希望做到這一點。雖然我可以想象它會在ListBox中看起來很奇怪。 – krillgar 2013-03-10 00:00:46
我甚至不知道HTML ListBoxes _could_顯示多行條目。 – 2013-03-10 00:07:30