TheBiz
TheBiz

Reputation: 83

How to show all values from a class in a textbox from a combobox

I am currently creating a winform program where account holders are able to purchase specific products/services.

The current problem I am having is that when I select an account holders name on the combobox, all the account holders details is supposed to be shown on the multiline textbox, however all im getting so far is the account holders name.

Here is the relevant code...

 public Form1()
    {
        InitializeComponent();

        mAccHolder[0] = new Customer("Rich", "Bronze Account", 11);
        mAccHolder[1] = new Customer("Patrick", "Silver Account", 21);
        mAccHolder[2] = new Customer("Steve", "Gold Account", 12);
        mAccHolder[3] = new Customer("Kevin", "Platinum Account", 25);

        foreach(Customer r in mAccHolder)
        {
            comboBox1.Items.Add(r.GetName());
        }
    }

and the code which connects the combobox and textbox together...

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (comboBox1.SelectedIndex == -1)
            {
                cstTxtBox.Text = string.Empty;
            }
            else
            {
                cstTxtBox.Text = comboBox1.SelectedItem.ToString();
            }

Upvotes: 1

Views: 106

Answers (4)

Salih Karagoz
Salih Karagoz

Reputation: 2289

You can use foreach,

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
     if (comboBox1.SelectedIndex == -1)
     {
         cstTxtBox.Text = string.Empty;
     }
     else
     {
         foreach (var item in comboBox1.Items)
         {
             cstTxtBox.Text = cstTxtBox.Text + item.ToString();
         }                    
     }
}

Upvotes: 0

wikiCan
wikiCan

Reputation: 449

Easy add directly Customer to combobox. With this way you can use it anywhere any time. like this.

public Form1()
    {
        InitializeComponent();

        mAccHolder[0] = new Customer("Rich", "Bronze Account", 11);
        mAccHolder[1] = new Customer("Patrick", "Silver Account", 21);
        mAccHolder[2] = new Customer("Steve", "Gold Account", 12);
        mAccHolder[3] = new Customer("Kevin", "Platinum Account", 25);

        foreach(Customer r in mAccHolder)
        {
            ComboboxItem item = new ComboboxItem();
            item.Text = r.GetName();
            item.Value = r;

            comboBox1.Items.Add(item);

        }
    }

And use Customer where you want...

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == -1)
            {
                cstTxtBox.Text = string.Empty;
            }
            else
            {
                Customer c=(Customer) comboBox1.SelectedValue;
                cstTxtBox.Text = c.whatyouwant....
            }

Upvotes: 0

Toskr
Toskr

Reputation: 379

You are only getting the account holders name because that is all you are giving the combobox. It does not know anything about a Customer. It only knows that it was supplied a string value, and that is all. You can pass it the name, this is fine, but you need to print the information corresponding with what that combobox item represent. You can do this by using an index. Since we know they are being supplied in the combobox in order, the index's will match. However, unless you override the ToString, you are just going to get the object name, e.g. "Customer.Object[]".

I threw an example together for you.

    private Customer[] customers = new Customer[3];

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        customers[0] = new Customer("Bob", "Bronze", 22);
        customers[1] = new Customer("Jane", "Silver", 32);
        customers[2] = new Customer("Jordan", "Gold", 26);

        foreach(var cust in customers)
        {
            comboBox1.Items.Add(cust.Name);
        }
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        textBox1.Text = customers[comboBox1.SelectedIndex].ToString();
    }

Also, in your 'Customer' class you may want to override the "ToString()" to make this nicer.

    public override string ToString()
    {
        return Name + "\r\n" + Membership + "\r\n" + Age;
    }

Upvotes: 1

JohnG
JohnG

Reputation: 9469

Using your example with an array of “Customer” objects, when the combo box's selected value changes, you need to get the selected “Customer” Object from the array mAccHolder. Once you have this customer either you can output the “Customer” values individually to the text box or you could make a “ToString” method for the “Customer” to output the customer info to your specifications, which is what the code below does.

The changes below to your current code appear to work as described. I added a blank value to the combo box so the user can de-select any currently selected customer. The “GetCustomer” method simply loops through the array mAccHolder to get the selected customer in the combo box. Hope this helps.

Customer Class

class Customer {
  public string Name { get; set; }
  public string AccountType { get; set; }
  public int ID { get; set; }

  public Customer(string inName, string actType, int inID) {
    Name = inName;
    AccountType = actType;
    ID = inID;
  }

  public string GetName() {
    return Name;
  }

  public override string ToString() {
    return "Name: " + Name + " AccountType: " + AccountType + " ID: " + ID;
  }
}

Updated code to use the Customer Class above.

Customer[] mAccHolder = new Customer[10];

public Form1() {
  InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e) {
  mAccHolder[0] = new Customer("Rich", "Bronze Account", 11);
  mAccHolder[1] = new Customer("Patrick", "Silver Account", 21);
  mAccHolder[2] = new Customer("Steve", "Gold Account", 12);
  mAccHolder[3] = new Customer("Kevin", "Platinum Account", 25);
  comboBox1.Items.Add(""); // <- add a blank selection so the user can select NO customer
  foreach (Customer r in mAccHolder) {
    comboBox1.Items.Add(r.GetName());
  }
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
  if (comboBox1.SelectedIndex == -1) {
    cstTxtBox.Text = string.Empty;
  } else {
    if (comboBox1.SelectedItem.ToString() != "") {
      Customer selectedCustomer = GetCustomer(comboBox1.SelectedItem.ToString());
      if (selectedCustomer != null)
        cstTxtBox.Text = selectedCustomer.ToString();
      else
        cstTxtBox.Text = "Customer not found!";
    }
    else {
      cstTxtBox.Text = "No Customer selected";
    }
  }
}

private Customer GetCustomer(string targetName) {
  foreach (Customer curCustomer in mAccHolder) {
    if (curCustomer.Name.Equals(targetName)) {
      return curCustomer;
    }
  }
  return null;
}

Upvotes: 1

Related Questions