Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6818915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:14:10+00:00 2026-05-26T21:14:10+00:00

I have a list of classes, but different children have different properties that need

  • 0

I have a list of classes, but different children have different properties that need to be displayed.

What I want to achieve is to have a listbox-type control in the gui which enables each child to display it’s properties the way it wants to – so not using the same pre-defined columns for every class.

I envisage something like the transmission interface (below), where each class can paint it’s own entry, showing some text, progress bar if relevant, etc.

enter image description here

How can this be achieved in C#?

Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:14:11+00:00Added an answer on May 26, 2026 at 9:14 pm

    Let your list items implement an interface that provides everything needed for the display:

    public interface IDisplayItem
    {
        event System.ComponentModel.ProgressChangedEventHandler ProgressChanged;
        string Subject { get; }
        string Description { get; }
        // Provide everything you need for the display here
    }
    

    The transmission objects should not display themselves. You should not mix domain logic (business logic) and display logic.

    Customized ListBox:
    In order to do display listbox items your own way, you will have to derive your own listbox control from System.Windows.Forms.ListBox. Set the DrawMode property of your listbox to DrawMode.OwnerDrawFixed or DrawMode.OwnerDrawVariable (if the items are not of the same size) in the constructor. If you use OwnerDrawVariable then you will have to override OnMeasureItem as well, in order to tell the listbox the size of each item.

    public class TransmissionListBox : ListBox
    {
        public TransmissionListBox()
        {
            this.DrawMode = DrawMode.OwnerDrawFixed;
        }
    
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            e.DrawBackground();
            if (e.Index >= 0 && e.Index < Items.Count) {
                var displayItem = Items[e.Index] as IDisplayItem;
                TextRenderer.DrawText(e.Graphics, displayItem.Subject, e.Font, ...);
                e.Graphics.DrawIcon(...);
                // and so on
            }
            e.DrawFocusRectangle();
        }
    }
    

    You can let your original transmission class implement IDisplayItem or create a special class for this purpose. You can also have different types of objects in the list, as long as they implement the interface. The point is, that the display logic itself is in the control, the transmission class (or whatever class) only provides the information required.

    Example:
    Because of the ongoing discussion with Mark, I have decided to include a full example here. Let’s define a model class:

    public class Address : INotifyPropertyChanged
    {
        private string _Name;
        public string Name
        {
            get { return _Name; }
            set
            {
                if (_Name != value) {
                    _Name = value;
                    OnPropertyChanged("Name");
                }
            }
        }
    
        private string _City;
        public string City
        {
            get { return _City; }
            set
            {
                if (_City != value) {
                    _City = value;
                    OnPropertyChanged("City");
                    OnPropertyChanged("CityZip");
                }
            }
        }
    
        private int? _Zip;
        public int? Zip
        {
            get { return _Zip; }
            set
            {
                if (_Zip != value) {
                    _Zip = value;
                    OnPropertyChanged("Zip");
                    OnPropertyChanged("CityZip");
                }
            }
        }
    
        public string CityZip { get { return Zip.ToString() + " " + City; } }
    
        public override string ToString()
        {
            return Name + "," + CityZip;
        }
    
        #region INotifyPropertyChanged Members
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            if (handler != null) {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        #endregion
    }
    

    Here is a custom ListBox:

    public class AddressListBox : ListBox
    {
        public AddressListBox()
        {
            DrawMode = DrawMode.OwnerDrawFixed;
            ItemHeight = 18;
        }
    
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            const TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.VerticalCenter;
    
            if (e.Index >= 0) {
                e.DrawBackground();
                e.Graphics.DrawRectangle(Pens.Red, 2, e.Bounds.Y + 2, 14, 14); // Simulate an icon.
    
                var textRect = e.Bounds;
                textRect.X += 20;
                textRect.Width -= 20;
                string itemText = DesignMode ? "AddressListBox" : Items[e.Index].ToString();
                TextRenderer.DrawText(e.Graphics, itemText, e.Font, textRect, e.ForeColor, flags);
                e.DrawFocusRectangle();
            }
        }
    }
    

    On a form, we place this AddressListBox and a button. In the form, we place some initializing code and some button code, which changes our addresses. We do this in order to see, if our listbox is updated automatically:

    public partial class frmAddress : Form
    {
        BindingList<Address> _addressBindingList;
    
        public frmAddress()
        {
            InitializeComponent();
    
            _addressBindingList = new BindingList<Address>();
            _addressBindingList.Add(new Address { Name = "Müller" });
            _addressBindingList.Add(new Address { Name = "Aebi" });
            lstAddress.DataSource = _addressBindingList;
        }
    
        private void btnChangeCity_Click(object sender, EventArgs e)
        {
            _addressBindingList[0].City = "Zürich";
            _addressBindingList[1].City = "Burgdorf";
        }
    }
    

    When the button is clicked, the items in the AddressListBox are updated automatically. Note that only the DataSource of the listbox is defined. The DataMember and ValueMember remain empty.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an std::list<TYPE> which may contain child classes of TYPE. I need to
I have a list of < li >'s but without classes, I want to
Is it possible to use Except() for two List's that have two different classes
Need a list of Pcap.Net members or classes? Their website doesn't have much documentation
I have a system.collections.generic.list(of ListBox) I would like to use the collection classes built-in
I have two xml files with the same data but different tags. I need
I have several classes (A, B, C, ...) that all use a List<AnotherClass> to
So i have a linked list that I want to be able to remove
I have 3 different classes, a generic entity class, and then two classes that
I have the server project that I have seperated into 6 different classes: ServerConnectionManager

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.