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