Click here to Skip to main content
15,886,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am learning C#. For one of my school project I need to create a window form where I've multiple check boxes and each check box has one combo box in front of it. If the user checked the check box then he must select a value from the combo box too.

when user click OK button then system must check if there is atleast one check box is checked and the associated combo is filled, if not then show error. Being a starter only I dont have any idea how to do this. Please help me



Thanks
James
Posted
Comments
Sibasisjena 13-Jan-15 5:22am    
Hi James,
Which control you have used.
1. A list of CheckBox control
2. A CheckedListBox control

Check the checked property of the checkbox (==true) and the selectedindex of the combox (>-1), e.g.
SQL
if (checkBox1.Checked && comboBox1.SelectedIndex > -1) {
    // do something
}

refer:
1. CheckBox.Checked Property[^]
2. ComboBox.SelectedIndex Property[^]
+++++++++++[round 2]+++++++++
You can create checkboxes and comboxes and placed them in separate List objects. Then insert a button on your form, in the click event handler of this button, loop through both list objects and check the condition for compliance. Try this:
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 WindowsFormsApplicationCS
{
    public partial class Form16 : Form
    {
        // declare List to store checkboxes and comboxes
        List<CheckBox> checkboxList = new List<CheckBox>();
        List<ComboBox> comboboxList = new List<ComboBox>();

        public Form16()
        {
            InitializeComponent();
        }

        private void Form16_Load(object sender, EventArgs e)
        {
            int j = 1;
            int k = 1;

            for (int i = 1; i <= 25; i++)
            {
                CheckBox chkBox = new CheckBox();

                chkBox.Tag = i.ToString();
                chkBox.Name = "Slot" + i;
                if (i <= 9)
                { chkBox.Text = "Slot0" + i; }
                else 
                { chkBox.Text = "Slot" + i; }


                if (i <= 13)
                { chkBox.Location = new Point(10, j * 20);}
                else
                { chkBox.Location = new Point(210, k * 20);  }


                this.Controls.Add(chkBox);

                // add this checkbox to the list
                checkboxList.Add(chkBox);

                ComboBox cmbBox = new ComboBox();
                cmbBox.Tag = i.ToString();
                cmbBox.Name = "Mode" + i;
                cmbBox.Items.Add("REGULAR STUDENT");
                cmbBox.Items.Add("VOCATIONAL STUDENT");

                cmbBox.AutoSize = true;
                if (i <= 13)
                { cmbBox.Location = new Point(70, j * 20); j++; }
                else { cmbBox.Location = new Point(270, k * 20); k++; }
                this.Controls.Add(cmbBox);

                // Add this combox to the list
                comboboxList.Add(cmbBox);
                
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < checkboxList.Count; i++)
            {
                if (checkboxList[i].Checked && comboboxList[i].SelectedIndex == -1)
                {
                    MessageBox.Show("You have checked "+ checkboxList[i].Name +" but forget to select " + comboboxList[i].Name);
                }
            }
        }
    }
}
 
Share this answer
 
v6
Comments
James@SysSol 13-Jan-15 5:29am    
hi,
Thanks for your input. I created all check boxes and combo dynamically. No dont understand how to proceed with that. Here is what I have done

CheckBox chkBox;
ComboBox cmbBox;
int j = 1;
int k = 1;

for (int i = 1; i <= 25; i++)
{
chkBox = new CheckBox();
chkBox.Tag = i.ToString();
chkBox.Name = "Slot" + i;
if (i <= 9)
{ chkBox.Text = "Slot0" + i; }
else { chkBox.Text = "Slot" + i; }

if (i <= 13)
{ chkBox.Location = new Point(10, j * 20); }
else { chkBox.Location = new Point(210, k * 20); }
this.Controls.Add(chkBox);


cmbBox = new ComboBox();
cmbBox.Tag = i.ToString();
cmbBox.Name="Mode" + i;
cmbBox.Items.Add("REGULAR STUDENT");
cmbBox.Items.Add("VOCATIONAL STUDENT");

cmbBox.AutoSize = true;
if (i <= 13)
{ cmbBox.Location = new Point(70, j * 20); j++; }
else { cmbBox.Location = new Point(270, k * 20); k++; }
this.Controls.Add(cmbBox);
}
Peter Leow 13-Jan-15 6:27am    
See the improved solution. It should work.
Consider you have 3 checkboxes and 3 comboboxes on form and one button. On button click you want to validate the case. Please find below code.

C#
private void button1_Click(object sender, EventArgs e)
        {
            //check if no checkbox is selected
            if (!(checkBox1.Checked || checkBox2.Checked || checkBox3.Checked))
            {
                MessageBox.Show("No Checkbox is slected");
            }
            else
            {
                string status = "";
                if (checkBox1.Checked) { status += CheckRespectiveComboboxSelected(comboBox1, "First"); }
                if (checkBox2.Checked) { status += CheckRespectiveComboboxSelected(comboBox2, "Second"); }
                if (checkBox3.Checked) { status += CheckRespectiveComboboxSelected(comboBox3, "Third"); }

                if (status != "")
                    MessageBox.Show(status);
                else
                    MessageBox.Show("Success");
            }
        }

        private string CheckRespectiveComboboxSelected(ComboBox cmb, string s)
        {
            if (cmb.Text == "" || cmb.SelectedIndex == 0) //first item of combobox is --select--
            {
                return "Please select item from " + s + " combobox.\n";
            }
            return "";
        }
 
Share this answer
 
Comments
James@SysSol 13-Jan-15 5:38am    
Hi,
This is great but I have 25 check and combo boxes in one form.... so do I've to write 25 lines for each check boxes and is it possible to do it by other way.

Thanks in advance.
James@SysSol 13-Jan-15 20:45pm    
Thank you all for your great work and support.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900