Home > Enterprise >  Display object name in Windows Forms
Display object name in Windows Forms

Time:10-04

I'm working on a project where I need to make an interactive windows forms application displaying the mandelbrot set. I now want to implement a drop down list on this window, where you can choose between different pictures of the mandelbrot set. I have made a class for the settings for these pictures:

public class Mandelpoint
{
    public double x;
    public double y;
    public double s;
    public string name;

    public Mandelpoint(double x, double y, double s, string name)
    {
        this.x = x;
        this.y = y;
        this.s = s;
        this.name = name;
    }
}

and I have the following drop down menu:

presets = new ComboBox();
presets.Size = new Size(150, 20);
presets.Location = new Point(250, 20);
this.Controls.Add(presets);

presets.Items.Add(new Mandelpoint(0, 0, 0.01, "standard"));

as you can see, I want the items in the combobox to be Mandelpoints. When I open the form, the dropdownlist is shown like this:

enter image description here

is there a way, by for example defining some sort of MandelpointToString method in the Mandelpoint class, that the Mandelpoints are displayed as their name in the drop down list? Any help would be greatly appreciated.

CodePudding user response:

You can implement the ToString method.

public class Mandelpoint
{
    public override string ToString()
        => name;
}

You can also set the DisplayMember of the combobox.

public class Mandelpoint
{
    public string Name => name;
}

presets.DisplayMember = "Name"
  • Related