繁体   English   中英

在C#中将列表插入列表框

[英]Inserting a list into a listbox in C#

我已经创建了一个列表,并且用户使用文本框和组合框将数据添加到了列表中,现在我试图将此数据列表输入到列表框中,但是每次尝试添加数据时,我都会得到输出作为类名例如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上,我已将每个文本框分配给其相应的获取/设置。

您的程序在列表框中显示类名,因为您在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());
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM