Hi Guys,

New to C# and I am struggling with storing a selected item from the array of comboboxes created at the runtime. Can you please help me write the command within the button_Click event so that it calculates the sum of all selected items from the comboboxes created at the runtime. It's almost done. Thank you very much in advance

Here is my code:

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 CourseHours
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        String[] hours = { "1", "2", "3", "4", "5", "6", "7", "8" };
        
        private ComboBox[] hrsComboBox;

        
        double selectedValue; //this variable should be used in the button_click event to store the  selected value from hrsComboBox

        
        double finalResult; //this variable should be used in the button_click event to add all the values selected by the user
        
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int count = Convert.ToInt16(comboBox1.SelectedItem.ToString());

            int z=259, m=75; 
            
            hrsComboBox = new ComboBox[count];

            
            
            for (int i = 0; i < count; i++)
            {

                hrsComboBox[i] = new ComboBox();
                hrsComboBox[i].Items.AddRange(hours);
                hrsComboBox[i].Location = new System.Drawing.Point(z, m);
                hrsComboBox[i].Size = new Size(80, 70);
                hrsComboBox[i].TabIndex = 1;
                this.Controls.Add(hrsComboBox[i]);

                this.ResumeLayout(false);
                m += 40;
                
                

            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            
        
        }
    }
}
int count = Convert.ToInt16(comboBox1.SelectedItem.ToString());

            int z=259, m=75; 
            
            hrsComboBox = new ComboBox[count];

Could you explain what this is supposed to do?


Let me try and rephrase what you want to do.

Option 1:
You have one combobox.

You want to get the Sum of all values in that combo box?

Option 2:
You have multiple Comboboxes.

You want the Sum of all current selections of those Comboboxes.

Hi,

Thanks for the quick reply

that whole statement will

1. take the value selected from the combobox and convert it to intiger
3. place the combobox under location x = 259, m = 75
5. based on the user selected item within combobox1 it will create a new comboboxes under location x,m

I need a sum of all the selections from multiple comboxes based on their selected value (by user).
if you select 5 from the combobox1, 5 comboboxes will be created and I need a sum of the 5 user inputs (Selected Item values) from the newley created comboboxes.

Thanks for quick reply

int x=0;
for (int i = 0; i < count; i++)
            {

                x += int.Parse(hrsComboBox[i].Text);
                

            }

Thanks finito, I have a few questions:

1. Where would the above code that you provided reside? within the button1_click_event?

2. I am assuming that int x will store the value of all added selections from multiple comboboxes?

3. So If I put the following code within the button1_click_event, will this be ok ?

MessageBox.Show("The sum of all selections is: " + x);

Yes, Yes, Yes

Sure if that is how you wish to display it.

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.