0

I am new to WinForms coding and I am trying to make this minipaint which has 3 buttons (circle, line and rectangle). Here are my buttons:

private void circle_btn_Click(object sender, EventArgs e)
{
    circle c = new circle() {startx=10,thickness=4,starty=10,radius=100,color=Color.Black };
    shapes.Add(c);
    panel1.Invalidate();
}
private void rectangle_btn_Click(object sender, EventArgs e)
{
    rectangle r = new rectangle() { startx = 10,thickness=4, starty = 10, length = 200, width = 100, color = Color.Black };
    shapes.Add(r);
    panel1.Invalidate();
}

private void line_btn_Click(object sender, EventArgs e)
{
    line l = new line() {startx=10,starty=10,thickness=4,endx=200,endy=200,color=Color.Black };
    shapes.Add(l);
    panel1.Invalidate();
}

When I click on them, a random shape will be printed on my Panel. I want to make a ComboBox , which has these three values(circle, line and rectangle) which lets me choose one of them in order to change it's properties in a PropertyGrid. I tried something like:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    Button namestr = line_btn;
    comboBox1.Items.Add(namestr);
}

but I know it is not right. How am I suppose to do that?

6
  • What is going wrong? Is there a failure when the SelectedIndex is changed, or is one of the other buttons not functioning? Commented Nov 8, 2016 at 15:21
  • the buttons are functioning correctly but my combobox is still empty Commented Nov 8, 2016 at 15:23
  • If you haven't added anything to your combobox, how can the SelectedIndex change? You need to add something to it first, then change the index to enter the SelectedIndexChanged event handler. Commented Nov 8, 2016 at 15:24
  • Question title: "how to make combobox c#" ... Comment: "but my combobox is still empty" ... please re-read your question (do not fill in the empty gaps) and edit it, so we can at least understand what you are actually asking. Commented Nov 8, 2016 at 15:25
  • that means I tried to make a combobox but it's not functioning properly. I think this is pretty clear Commented Nov 8, 2016 at 15:28

1 Answer 1

1

what you could do is to populate your ComboBox first eg. in the constructor:

public Form1()
{
   comboBox1.Items.AddRange(new List<string>() { "circle", "line", "rectangle"}.ToArray());
}

or you take already an array:

public Form1()
{
   comboBox1.Items.AddRange(new string []{ "circle", "line", "rectangle"});
}

and switch according to the values in the SelectedIndexChanged event:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    switch (comboBox1.SelectedItem.ToString())
    {
        case "circle":
            //do something circular
            break;
        case "line":
            //do something linish
            break;
        case "rectangle":
            //do something edgy
            break;
        default:
            break;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for answering my question but the line "comboBox1.Items.AddRange(new List<string>() { "circle", "line", "rectangle"});" has some errors
sorry forgot a decisive element at the end: .ToArray(). The method needs an array not a list. I edited my post. You can already take the string[]

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.