【问题标题】:Why is my listbox only displaying one item?为什么我的列表框只显示一项?
【发布时间】:2019-07-26 19:22:11
【问题描述】:

我正在制作一个 Windows 窗体,用户可以在其中建立多个联系人(存储在列表框中的对象中),当单击一个项目时,文本框将显示联系人信息。但是,无论我点击多少次,我的文本框都只保存一个列表框项目信息。

我试着做一个foreach循环,但还是不行

public partial class FormManager : Form
{

    FormContact contactForm;
    Contact contact;

    public FormManager()
    {
        InitializeComponent();
        ControlsDisabled();
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        contactForm = new FormContact();

        if (contactForm.ShowDialog() == DialogResult.OK)
        {
        //I think this is the main problem the ig

                txtFname.Text = contactForm.firstname;
                txtLname.Text = contactForm.lastname;
                 contact = contactForm.contact;
                lstBoxAdd.Items.Add(contact.firstname + " " + contact.lastName);
                ControlsEnabled();


        }

    }

    private void btnEdit_Click(object sender, EventArgs e)
    {
        contactForm.Show();
        contactForm.Visible = false;

        if (contactForm.ShowDialog() == DialogResult.OK)
        {
            txtFname.Text = contactForm.firstname;
            txtLname.Text = contactForm.lastname;
            contact = contactForm.contact;
            lstBoxAdd.Items.Add(contact.firstname + " " + contact.lastName);
            ControlsEnabled();`enter code here`

        }
    }

    private void lstBoxAdd_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (lstBoxAdd.SelectedIndex != -1) 
        {
             Contact selectedcontact = (Contact)lstBoxAdd.SelectedItem;
             txtFname.Text = selectedcontact.firstname;
             txtLname.Text = selectedcontact.lastName;

        }
    }

    //The other form where the textboxes are getting updated from
    public string firstname;
    public string lastname;
    public Contact contact;


    public FormContact()
    {
        InitializeComponent();
    }

    public FormContact(Contact contact)
    {

        InitializeComponent();

    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        contact = new Contact(txtFname.Text, txtLname.Text);
        firstname = txtFname.Text;
        lastname = txtLname.Text;
        this.Hide();

    }

    //the class of the contact objects
    public class Contact
    {
    public string firstname;
    public string lastName;

    public Contact()
    {

    }
    public Contact(string FirstName, string LastName)
    {
        firstname = FirstName;
        lastName = LastName;
    }

}

【问题讨论】:

  • 这里有很多问题。您只声明了一个 Contact contact; 类对象。您需要一个List<Contact>,它必须是您的列表框的数据源。添加新联系人时,请将其添加到List<Contact>,不要将字符串添加到列表框。 FormContact 用作对话框。单击btnSave 时,关闭它,不要隐藏它。 btnSave.DialogResult 的属性必须设置为 OK(请参阅设计器)。删除 contactForm.Show();contactForm.Visible = false;。当您编辑一个项目时,您需要将该项目(一个联系人对象)apss 到FormContact。其他一些问题。
  • 您可以使用标准的 DataBindings 来简化整个过程。查看BindingSourceBindingList 类。

标签: c# winforms listbox


【解决方案1】:

这里是更正后的FormManager

public partial class FormManager : Form
{    
    FormContact contactForm;
    List<Contact> contacts; // Instead of 1 entity, you need a list of entities

    public FormManager()
    {
        InitializeComponent();            
        lstBoxAdd.DataSource = contacts;      // Bind the list to the listbox
        lstBoxAdd.DisplayMember = "FullName"; // Add Fullname to Contact class
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        ControlsDisabled();                   // It's enough to disable here
        contactForm = new FormContact();

        if (contactForm.ShowDialog() == DialogResult.OK)
        {                    
            contact = contactForm.contact;    // You need only the Contact object
            contacts.Add(contact); // You add the contact to to bound list
            txtFname.Text = contact.FirstName;
            txtLname.Text = contact.LastName;                 
        }
        ControlsEnabled();                    // Enable even if dialog fails
    }

    private void btnEdit_Click(object sender, EventArgs e)
    {            
        ControlsDisabled();
        contact = lstBoxAdd.SelectedItem as Contact; // Retrieve selected Contact
        contactForm = new ContactForm(contact);      // Pass it to the contactForm

        if (contactForm.ShowDialog() == DialogResult.OK)
        {
            contact = contactForm.contact;
            txtFname.Text = contact.FirstName;
            txtLname.Text = contact.LastName;                
            //lstBoxAdd.Items.Add(contact.firstname + " " + contact.lastName); --> Not needed
        }
        ControlsEnabled();
    }

    private void lstBoxAdd_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (lstBoxAdd.SelectedIndex != -1) 
        {
             Contact selectedContact = (Contact)lstBoxAdd.SelectedItem;
             txtFname.Text = selectedContact.FirstName;
             txtLname.Text = selectedContact.LastName;    
        }
    }
}

更正后的ContactForm

//The other form where the textboxes are getting updated from
//public string firstname; --> Not needed
//public string lastname;  --> Not needed
public Contact contact;

public FormContact()
{
    InitializeComponent();
    contact = new Contact();
}

public FormContact(Contact _contact)
{
    InitializeComponent();
    contact = _contact;
    txtFname.Text = contact.FirstName;
    txtLname.Text = contact.LastName;
}

private void btnSave_Click(object sender, EventArgs e)
{
    // contact = new Contact(txtFname.Text, txtLname.Text); --> Not needed
    contact.FirstName = txtFname.Text;
    contact.LastName = txtLname.Text;
    this.DialogResult = DialogResult.OK; // This must be set by you
    this.Close();                        // Close the form instead of hide
}

最后是Contact 类:

//the class of the contact objects
public class Contact
{
    public string FirstName;
    public string LastName;

    public string FullName => $"{FirstName} {LastName}"; // For displaying

    public Contact(){}

    public Contact(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }
}

我没有测试它所以很抱歉万一错别字..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    相关资源
    最近更新 更多