我已经制作了一个列表,用户使用文本框和组合框将数据添加到列表中,我现在正尝试将此数据列表输入到列表框中,但每次我尝试添加数据时,我都会将输出视为类名,例如WindowApplicaion.Journey,或者它出现为System.Collections.Generic.List`1 [WindowApplication.Journey],我不确定这是否是由于我将转换代码放在错误的地方或我是只是做错了,这是我的代码:
private void ShowAllToursbttn_Click(object sender, RoutedEventArgs e)
{
foreach (Tour t in company.tours)
{
string converter = company.tours.ToString();
ToursListBox.Items.Add(converter);
}
}
或
private void ShowAllToursbttn_Click(object sender, RoutedEventArgs e)
{
foreach (Tour t in company.tours)
{
string ConvertedList = string.Join(" ", company.tours);
TourListBox.Items.Add(ConvertedList);
}
}
我的公司课程中创建了我的列表,其中t是列表中的每个实例,任何建议都很棒,谢谢!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowApplication
{
class Tour
{
private string custFirstname;
private string custSurname;
private string custAddress;
private string pickupArea;
private string pickupDateandTime;
private string pickupDescription;
private string destinationArea;
private string destinationDescription;
//Creating getters and setters for each attribute
#region getters/setters
public string firstname
{
get { return custFirstname; }
set { custFirstname = value; }
}
public string surname
{
get { return custSurname; }
set { custSurname = value; }
}
public string address
{
get { return custAddress; }
set { custAddress = value; }
}
public string pickuparea
{
get { return pickupArea; }
set { pickupArea = value; }
}
public string pickupdateandtime
{
get { return pickupDateandTime; }
set { pickupDateandTime = value; }
}
public string pickupescription
{
get { return pickupDescription; }
set { pickupDescription = value; }
}
public string destinationarea
{
get { return destinationArea; }
set { destinationArea = value; }
}
public string destinationdescription
{
get { return destinationDescription; }
set { destinationDescription = value; }
}
}
}
这是我的旅游课程。
private void AddThisTourbttn_Click(object sender, RoutedEventArgs e)
{
Tour t = new Tour();
t.firstname = CustomerFirstnameTxt.Text;
t.surname = CustomerSurnameTxt1.Text;
t.address = CustomerAddressTxt.Text;
t.pickupdateandtime = TourDateTimeTxt.Text;
t.pickuparea = TourPickupArea.Text;
t.pickupescription = TourPickupDescriptionTxt.Text;
t.destinationarea = TourDestinationArea.Text;
t.destinationdescription = TourDestinationDescriptionTxt.Text;
company.addTour(t);
}
在我的MainWindow上,我已将每个文本框分配给其相应的get / set。
答案 0 :(得分:0)
您的程序在列表框中显示类名,因为您在ShowAllToursbttn_Click中使用默认对象的toString()方法,它将输出类名。尝试覆盖Tour类中的ToString()方法,以输出具有所需格式的字符串,例如:
public override string ToString()
{
return String.Format("Firstname: {0}; Surname: {1}", firstname, surname);
}
将ShowAllToursbttn_Click逻辑更改为:
private void ShowAllToursbttn_Click(object sender, RoutedEventArgs e)
{
foreach (Tour t in company.tours)
{
TourListBox.Items.Add(t.ToString());
}
}