• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

ComboBox items are not displayed in drop down list

ComboBox items are not displayed in drop down list when class is delivered from from UserControl.
Help fix this.. I need class type UserControl.
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        Uc f = new Uc();
        comboBox1.Items.Add(f);
        comboBox1.Items.Add("Upper item is empty why?");
    }
}

public class Uc : UserControl
{
    public override string ToString()
    {
        return "not visible text";
    }
}

Open in new window

0
Cebik
Asked:
Cebik
  • 3
2 Solutions
 
HainKurtSr. System AnalystCommented:
try adding public properties...

Uc f = new Uc();
f.Name = "HainKurt";
f.Value = "HK";
comboBox1.Items.Add(f);

or try these

// comboBox1.Items.Add(f.Name);
// comboBox1.Items.Add(f.Value);
// comboBox1.Items.Add(f.toString());
private String _Name;
public String Name {
  get { return _Name;}
  set { _Name = value; }
}

private String _Value;
public String Value {
  get { return _Value;}
  set { _Value = value; }
}

Open in new window

0
 
CebikAuthor Commented:
I prefer to add an object. This isn't helping. The same result.
0
 
tikusbalapCommented:
Why deriving from UserControl?
0
 
CebikAuthor Commented:
The truth is that it is UserControl and I want to have it in combobox.
0
 
CebikAuthor Commented:
I've found a solution by my self..
(property in class and displaymember in combobox)
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        uc f = new uc();

        comboBox1.DisplayMember = "DisplayText"; 
        comboBox1.Items.Add(f);
        
    }
}

public class uc : UserControl
{
    string text = "example";

    public override string ToString()
    {
        return text;    //not working
    }

    public String DisplayText
    {
        get { return text; }
    }
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now