0

The Items property of a checked list box control in Windows forms is of type object, so my naive hope was that I can add a customized User control as item. (Since, usually, my task is to write logic for background tasks I'm not too familiar with UI programming, so this may be a stupid idea..)

More precisely I want to display two labels and a button in each line of the the checked list box. The first label is supposed to display the name of an object the user can select (so that later on a specific operation will be performed on all checked items). For any item checked, the button is supposed to allow the user to choose a file from which custom settings can be read for performing that operation and the second label should display the choice the user has made using the button (i.e. the file name or something like the string "default settings").

So, in the forms designer, I created a custom control CustomControl1 with label1, label2, button1, and methods to set the text properties, set autosize of the labels and the button to false, defined their size manually. Then in the main window I created the checked list box, to which I added custom controls. The constructor of my main window now looks as follows:

        InitializeComponent();

        UserControl1 uc1 = new UserControl1();
        uc1.setLabel1("label1_text");
        uc1.setLabel2("label2_text");
        uc1.setButtonText("button_text");

        this.checkedListBox1.Items.Add(uc1);

        uc1.Visible = true;

This compiles without any error and also runs, but the checked list box shows an empty field. I also experimented with the size of the list box. If I reduce the height so that the check box just fits into it then I do see fragments of the button in the corresponding line, but no label.

Is it possible to use a custom form in a checked list box and if yes, what am I missing?

Thomas
  • 1,160
  • 3
  • 16
  • 34
  • this logic is some kind of `WPF` stuff, not `winforms`. In winforms, you have to draw everything for each item of the listBox, that means you have to draw your UserControl in each item, if your UserControl has such as a Button, you also have to draw that button, use some hittesting to provide interactivity between user and the controls on the drawn item.... it's very complex indeed. Another approach is to host all the user controls in the listBox, it requires some docking. I think you just need some **Panel** in this case, enable `AutoScroll` and add UControls on that panel. – King King Dec 16 '13 at 13:09
  • Your question is similar to one I asked [here](http://stackoverflow.com/questions/15532639/complex-ui-inside-listboxitem). The response I got from that is that winforms is a completely useless piece of crap and that you need a decent, usable technology (WPF) if you ever want to customize any aspect of the UI, or create anything that gets even 1 pixel away from the default, ugly, boring, Windows 3.1-like winforms stuff. OR if you want a UI framework that supports REAL DataBinding and doesn't force you to type a horrendous gargantuan amount of useless code behind. – Federico Berasategui Dec 16 '13 at 15:00
  • Since you already have a custom control to display, why not create a custom container for it? Or if you all you really want is a way to show them in a list-type format, then drop them into a `TableLayoutPanel`, which can be defined as 1 column and many rows. It would look and feel like a list. If you need all the Add, Insert, etc. helpers, then you're coding a container for it as well. – DonBoitnott Dec 16 '13 at 15:49

2 Answers2

2

No, you can't do this.

The listbox only shows a list of elements. The listbox uses the property .ToString() for each objects in the list to show the items.

You need to look for a custom listbox

Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219
0
private void Form1_Load(object sender,EventArgs e)
    {
        checkedListBox1.Items.Add("IIT");
        checkedListBox1.Items.Add("CSE");
        checkedListBox1.Items.Add("EEE");
        checkedListBox1.Items.Add("ICT");
        checkedListBox1.Items.Add("URP");
        checkedListBox1.Items.Add("ENGLISH");
        checkedListBox1.Items.Add("BANGLA");
        checkedListBox1.Items.Add("MATH");
    }
    private void checkedListBox1_SelectedIndexChanged(object sender,EventArgs e)
    {
        //var item=checkedListBox1.SelectedItem;

        label1.Text=checkedListBox1.SelectedItem.ToString();
    }
Mayank Jain
  • 5,663
  • 7
  • 32
  • 65
tanjila
  • 21
  • 1