Listbox doesn't show data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Niloo
    New Member
    • Dec 2011
    • 2

    Listbox doesn't show data

    I should show this _customers to list box:

    Code:
    public Customer GetCustomer(int index)
            {
                return (Customer)_customers[index];
               
            }
    so I used:
    Code:
            private void UpdateCustomerList()
            {
                lstCustomer.Items.Clear();
                for (int index = 0; index <= (_customerMngr.count-1); index++)
                    lstCustomer.Items.Add(_customerMngr.GetCustomer(index).ToString());
                
            }
    but my list box doesnt show my data.
    how can I convert _customer to string?
    Last edited by Niheel; Dec 11 '11, 10:45 PM. Reason: code tatgs
  • adriancs
    New Member
    • Apr 2011
    • 122

    #2
    Hi, I'm not fully understand what's your situation.

    However, base on my understanding of your problem.
    The situation that you want to solve might be this:

    try this:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication7
    {
        public partial class Form2 : Form
        {
            List<Customer> lstCustomer;
            public Form2()
            {
                InitializeComponent();
                lstCustomer = new List<Customer>();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Customer a = new Customer();
                a.CustomerCode = "A001";
                a.CustomerId = 1;
                a.DateRegister = DateTime.Today;
                a.Name = "John Smift";
    
                Customer b = new Customer();
                b.CustomerCode = "A002";
                b.CustomerId = 2;
                b.DateRegister = DateTime.Today;
                b.Name = "Christina Jess";
    
                lstCustomer.Add(a);
                lstCustomer.Add(b);
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                if (lstCustomer == null || lstCustomer.Count == 0)
                    return;
                listBox1.Items.Clear();
                for (int i = 0; i < lstCustomer.Count; i++)
                {
                    string c = lstCustomer[i].CustomerId + " - ";
                    c += "(" + lstCustomer[i].CustomerCode + ") ";
                    c += lstCustomer[i].Name;
                    listBox1.Items.Add(c);
                }
            }
        }
    
        public class Customer
        {
            public int CustomerId;
            public string CustomerCode;
            public string Name;
            public DateTime DateRegister;
        }
    }

    Comment

    • foahchon
      New Member
      • Aug 2006
      • 13

      #3
      If you want the ListBox to display a member in the Customer class (like the customer's name, for instance), you need to override the ToString() method in the Customer class:

      Code:
      public override string ToString()
      {
          return String.Format("{0} {1}", this.FirstName, this.LastName);
      }
      ...or something similar.
      Last edited by foahchon; Dec 13 '11, 01:32 AM. Reason: Forgot keyword.

      Comment

      • adriancs
        New Member
        • Apr 2011
        • 122

        #4
        to foahchon:
        I think you have answered the question.
        d^^b

        Comment

        Working...