Ashton
Ashton

Reputation: 363

Fill textbox with text from Listbox

I have looked at many answers on this topic but have found no solution because my particular situation uses a class called Customer and I am trying to set my textbox called tbxFirstName to the selected name I click on in the listbox by setting the tbxFirstName.Text = cust.getFirstName;.

I can not figure out why it is not re-filling the first name text box with the string in the selected item.

This is my code.


public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        listBoxDatabase.Name = "CUSTOMERS";
    }

    private void btnAddCustomer_MouseClick(object sender, MouseEventArgs e)
    {
        Customer cust = new Customer(tbxFirstName.Text, tbxLastName.Text, tbxPhone.Text);

        if (listBoxDatabase.Items.Contains(cust.ToString()))
        {

            MessageBox.Show("Customer Already Exist!", "ERROR");
        } else
        {
            listBoxDatabase.Items.Add(cust.ToString());
        }
    }

    private void listBoxDatabase_SelectedIndexChanged(object sender, EventArgs e)
    {
        Customer cust = new Customer(tbxFirstName.Text, tbxLastName.Text, tbxPhone.Text);

        tbxFirstName.Text = cust.getFirstName;
    }
}

class Customer
{
    //Variables
    private string firstName, lastName, phone;

    //Constructor
    public Customer(string firstName, string lastName, string phone)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.phone = phone;
    }

    public string getFirstName
    {
        get
        {
            return firstName;
        }
    }

    public string getLastName
    {
        get
        {
            return lastName;
        }
    }

    public string getPhone
    {
        get
        {
            return phone;
        }
    }

    public override string ToString()
    {
        return firstName + " " + lastName + " " + phone;
    }
}

Can anyone help me with what I am missing?

Upvotes: 1

Views: 1680

Answers (3)

Steve
Steve

Reputation: 216363

Your problems are caused by the fact that you store a string in your ListBox items. Instead you should store a Customer instance and retrieve a Customer instance

Your Customer class contains the override of ToString, so the ListBox calls itself this override when it needs to display the item, but the underlying dataobject stored by the ListBox is a full Customer object

private void btnAddCustomer_MouseClick(object sender, EventArgs e)
{
    Customer cust = new Customer(tbxFirstName.Text, tbxLastName.Text, tbxPhone.Text);

    // This will replace the Contains because now you have 
    // a collection of Customer not a collection of strings.
    if (listBoxDatabase.Items.Cast<Customer>()
                       .Any(x => x.ToString() == cust.ToString())) 
    {
        MessageBox.Show("Customer Already Exist!", "ERROR");
    }
    else
    {
        // Add a customer, not its ToString representation
        listBoxDatabase.Items.Add(cust);
    }
}

Now, you can retrieve the Customer instance directly with

private void listBoxDatabase_SelectedIndexChanged(object sender, EventArgs e)
{

    if (listBoxDatabase.SelectedIndex != -1)
    {
        Customer cust = listBoxDatabase.Items[listBoxDatabase.SelectedIndex] as Customer;
        tbxFirstName.Text = cust.getFirstName;
        tbxLastName.Text = cust.getLastName;
        tbxPhone.Text = cust.getPhone;
    }
}

Upvotes: 1

Fivestar
Fivestar

Reputation: 323

From your original code posted you are creating a NEW customer each time the selection changes. But you want to be selecting the customer object that is highlighted in the listbox - not making a brand new one.

EDIT: I also noticed that when you add to the listbox you are adding a string, not the object. If you want to do it that way then you would need to parse the string and break it up by the spaces or something. I would suggest adding the item/object to the listbox instead

In your SelectedIndexChanged event try something like this

Customer c = (Customer)listboxDatabase.SelectedItem;
tbxFirstName.Text = c.FirstName;
tbxLastName.Text = c.LastName;
tbxPhoneNumber.Text = c.PhoneNumber;

Upvotes: 1

Rahul
Rahul

Reputation: 77936

Well it's not changing cause it's the same text as can be seen below. You are creating Customer object with data from tbxFirstName.Text and that's itself you are trying to re-populate in the same textbox.

    Customer cust = new Customer(tbxFirstName.Text, tbxLastName.Text, tbxPhone.Text);

    tbxFirstName.Text = cust.getFirstName;

Per your comment, you want to fill textbox with listbox selected item text. In that case you can just do like

private void listBoxDatabase_SelectedIndexChanged(object sender, EventArgs e)
{
    tbxFirstName.Text = listBoxDatabase.SelectedItem.ToString();
}

Upvotes: 1

Related Questions