3

I'm trying to use a List<> of objects of my custom class as a source for ListBox. The problem is the instead of strings showing up as an item in my ListBox the whole string is broken down and individual chars are turned into ListBox items. Below are my class and replication to ListBox. I store a list in another class.

What am I doing wrong? Or maybe there is a better way of binding ListBox to List<>?

Listing 1 - replication

         BindingSource bds1 = new BindingSource(_newProspectiveCustomer.PhoneNumbers, "PhoneNumber");
         phonesListBox.BeginUpdate();
         phonesListBox.DataSource = bds1;
         phonesListBox.EndUpdate();

Listing 2 - class

public class PCPhone : IEnumerable<string>
    {
        public string Dma { get; set; }
        public IEnumerator<string> GetEnumerator()
        {
            yield return Dma;
            yield return PhoneNumber;
            yield return IndexOrder.ToString();
            yield return Type.ToString();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        private string _phoneNumber,
                        _type,
                        _notes;
        private int _indexOrder;
        public event EventHandler PhoneChanged;

        public string PhoneNumber
        {
            get
            {
                return _phoneNumber;
            }
            set
            {
                _phoneNumber = value;
                CleanPhoneString();
            }
        }
        public string Type
        {
            get
            {
                return _type;
            }
            set
            {
                _type = value;
            }
        }
        public string Notes
        {
            get
            {
                return _notes;
            }
            set
            {
                _notes = value;
            }
        }
        public int IndexOrder
        {
            get 
            {
                return _indexOrder;
            }
            set
            {
                _indexOrder = value;
            }
        }
        private void CleanPhoneString()
        {
            if(_phoneNumber.IndexOf("(") > -1)
                _phoneNumber = _phoneNumber.Remove('(');
            if(_phoneNumber.IndexOf(")") > -1)
                _phoneNumber = _phoneNumber.Remove(')');
            if(_phoneNumber.IndexOf(" ") > -1)
                _phoneNumber = _phoneNumber.Remove(' ');
            if(_phoneNumber.IndexOf("-") > -1)
                _phoneNumber = _phoneNumber.Remove('-');
        }

        public PCPhone(string t, int i, string num)
        {
            _type = t;
            _indexOrder = i;
            _phoneNumber = num;
            CleanPhoneString();
        }
    }
2
  • What's the type of _newProspectiveCustomer.PhoneNumbers? a List<PhoneNumber>? so what is the definition of the PhoneNumber? Commented Nov 21, 2013 at 16:25
  • About 90% of the code in your sample is irrelevant, and you're missing the important parts as @KingKing points out. sscce.org Commented Nov 21, 2013 at 16:28

1 Answer 1

2

Assuming list is a List of PCPhone just

BindingSource bds1 = new BindingSource() { DataSource = list };
listBox1.DisplayMember = "PhoneNumber";
listBox1.ValueMember = ... ; // value member
listBox1.DataSource = bds1;

or just set directly the list to the listBox1 datasource

listBox1.DisplayMember = "PhoneNumber";
listBox1.ValueMember = ... ; // value member
listBox1.DataSource = list;
Sign up to request clarification or add additional context in comments.

2 Comments

Well you are right. I just fed list<> into listbox datasource. I had done it before without success but at that time I didn't have PCPhone class interfaced to IEnumerable.
It's not the PCPhone that has to be enumerable, just the list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.