Hi, I am creating a program on behalf of my business and have become stuck in one particular matter. I am fairly new to C#.

The application consists of the user entering data into a mixture of comboBoxes and textboxes, after the user has entered the data he clicks the add button, when pressed an identical set of textboxes and comboboxes will appear for which I am using the following code.

TextBox field1 = new TextBox();
                field1.Location = new Point(15, 120);
                field1.Size = new Size(230, 20);
                field1.Name = "field1";
                this.Controls.Add(field1);

                TextBox field2 = new TextBox();
                field2.Location = new Point(260, 120);
                field2.Size = new Size(42, 20);
                field2.Name = "field2";
                this.Controls.Add(field2);

                ComboBox comboBox3 = new ComboBox();
                comboBox3.Location = new Point(330, 120);
                comboBox3.Size = new Size(240, 21);
                comboBox3.Items.Add("Clear Float");
                comboBox3.Items.Add("Low E");
                comboBox3.Items.Add("Toughened Low E");
                comboBox3.Items.Add("Toughened Float One Side");
                comboBox3.Items.Add("Clear Laminate Low E");
                comboBox3.Items.Add("Float Gray Tint");
                comboBox3.Items.Add("Float Gray Tint Toughened One Side");
                comboBox3.Items.Add("Obscure - Toilet Only");
                comboBox3.Items.Add("Obscure - Bathroom Toughened One Side");
                comboBox3.Items.Add("Toughened E Tch Lite - Toilet Only");
                comboBox3.Items.Add("E Tch Lite - Toughened One Side");
                comboBox3.Name = "comboBox3";
                comboBox3.SelectedIndexChanged += new System.EventHandler(comboBox3_SelectedIndexChanged);
                this.Controls.Add(comboBox3);

                TextBox field3 = new TextBox();
                field3.Location = new Point(590, 120);
                field3.Size = new Size(60, 20);
                field3.Name = "field3";
                this.Controls.Add(field3);

                TextBox field4 = new TextBox();
                field4.Location = new Point(655, 120);
                field4.Size = new Size(60, 20);
                field4.Name = "field4";
                this.Controls.Add(field4);

                ComboBox comboBox4 = new ComboBox();
                comboBox4.Location = new Point(720, 120);
                comboBox4.Size = new Size(120, 21);
                comboBox4.Items.Add("Light");
                comboBox4.Items.Add("Standard");
                comboBox4.Items.Add("Heavy");
                comboBox4.SelectedIndexChanged += new System.EventHandler(comboBox4_SelectedIndexChanged);
                comboBox4.Name = "comboBox4";
                this.Controls.Add(comboBox4);

                Button button2 = new Button();
                button2.Location = new Point(860, 120);
                button2.Size = new Size(40, 20);
                button2.Text = "Add";
                button2.Click += new System.EventHandler(button2_Click);
                this.Controls.Add(button2);

As the user needs to enter a maximium of 10 diffrent values I am having to use this code for every add button by copying and pasting and renaming the buttons, txtboxes and comboBoxes, My question is, is there a more simpler way to having this code as it takes up alot of space and time having to copy and ast all the time.

Any help is appreciated and thanked in advance.

Recommended Answers

All 17 Replies

i attached the sample application for you :

and here is the code :

Form1.cs :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddNewComboBox();
        }
        private void AddNewComboBox()
        {
            ComboBox myNewComboBox = new ComboBox();
            myNewComboBox.Items.Add("addYourItems");
            myNewComboBox.SelectedIndexChanged += new EventHandler(myNewComboBox_SelectedIndexChanged);
            flowLayoutPanel1.Controls.Add(myNewComboBox);
        }

        void myNewComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show("you will implement something here");
        }
    }
}

you will do something similar to this but i dont know your goal so i cant post something exactly as what you need.

notice that i add the controls to flowlayoutpanel, it saves you lots of code when it comes to positioning. you dont have to specify positions for dynamically created elements as they are ordered from top to bottom or left to right or the opposite way when you set the flowdirection property of the flowlayoutpanel.
open the project and run the application you will understand what i mean. you can open it with visual studio 2008 express edition which is for free to download if you dont already have it.

Hi,
Thanks that is a quicker way of doing it but what I need to do is create 10 comboBoxes 1 at a time with a button at diffrent locations, the way you posted would still mean I will have to keep changing the locations.

i cant visualize what you want to do, but i feel like you still mention the same thing. then put the buttons into that panel too, add them the same way.

Oh I loaded the program and it worked beautifully thank you so much!

you are welcome, it is a pleasure :)

Just one thing tho, how would you assign values to the comboboxes and relate to just one of those because wouldn't they all have the same name?

when you create the combobox the name after the class name is the object name, so to give a name to that object you still have the name property like myNewComboBox.Name = "someName";

Yes i know that but we are creating lots of comboboxes so won't the all have the same name and also is there a way to cap how many it creates?

private void AddNewComboBox()
        {
            ComboBox myNewComboBox = new ComboBox();
            myNewComboBox.Name = "someName";
            myNewComboBox.Items.Add("addYourItems");
            myNewComboBox.SelectedIndexChanged += new EventHandler(myNewComboBox_SelectedIndexChanged);
            flowLayoutPanel1.Controls.Add(myNewComboBox);
        }

like above you can set a name for your controls but you dont necessarily need it in this case.

in the event handler you can cast the sender to combobox and then you can do operations specific to that particular combobox.

here is what i mean, i also attach the latest version of the application :

Form1.cs :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private int comboBoxIndex = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddNewComboBox();
        }
        private void AddNewComboBox()
        {
            ComboBox myNewComboBox = new ComboBox();
            myNewComboBox.Name = "someName" + comboBoxIndex.ToString();
            comboBoxIndex++;
            myNewComboBox.Items.Add("addYourItems");
            myNewComboBox.SelectedIndexChanged += new EventHandler(myNewComboBox_SelectedIndexChanged);
            flowLayoutPanel1.Controls.Add(myNewComboBox);
        }

        void myNewComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show(((ComboBox)sender).Name);
        }
    }
}

Could you please explain to me how you could relate to say the second created comboBox with this please?

to control the number of the ComboBoxes modify the code like below :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private int comboBoxIndex = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddNewComboBox();
        }
        private void AddNewComboBox()
        {
            // to prevent adding more than a specific number just add an if statement
            if (comboBoxIndex != 10)
            {
                ComboBox myNewComboBox = new ComboBox();
                myNewComboBox.Name = "someName" + comboBoxIndex.ToString();
                comboBoxIndex++;
                myNewComboBox.Items.Add("addYourItems");
                myNewComboBox.SelectedIndexChanged += new EventHandler(myNewComboBox_SelectedIndexChanged);
                flowLayoutPanel1.Controls.Add(myNewComboBox);
            }
            else
            {
                MessageBox.Show("you reached to 10, cant add more");
            }
        }

        void myNewComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show(((ComboBox)sender).Name);
        }
    }
}

i couldnt attach to last post.

Absolutley brillant thanks for bearing with me!

if you want to get information related to that combobox like first or second or any index number starting with zero in this case, you can do the following, look at the attached project as well.

Form1.cs :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private int comboBoxIndex = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddNewComboBox();
        }
        private void AddNewComboBox()
        {
            // to prevent adding more than a specific number just add an if statement
            if (comboBoxIndex != 10)
            {
                ComboBox myNewComboBox = new ComboBox();
                myNewComboBox.Name = "someName" + comboBoxIndex.ToString();
                comboBoxIndex++;
                myNewComboBox.Items.Add("addYourItems");
                myNewComboBox.SelectedIndexChanged += new EventHandler(myNewComboBox_SelectedIndexChanged);
                flowLayoutPanel1.Controls.Add(myNewComboBox);
            }
            else
            {
                MessageBox.Show("you reached to 10, cant add more");
            }
        }

        void myNewComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show(((ComboBox)sender).Name);
        }

        private void btnAlertName_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(textBox1.Text) <= flowLayoutPanel1.Controls.Count)
            {
                MessageBox.Show(flowLayoutPanel1.Controls[Convert.ToInt32(textBox1.Text)].Name);
            }
            else
                MessageBox.Show("wrong index number");
        }
    }
}

ok, so if i have "variable1" and in my combo box I have 5 choices,
Choice1
Choice2
Choice3
Choice4
Choice5

and I want to make "variable 1" equal diffrent numbers for each choice, thats simple but what if In the second combobox created where i have the same choices i just want to make "variable2" equal diffrent numbers depending on whats selected how would i do that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.