Click here to Skip to main content
16,016,580 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,
I'm trying to connect the checkbox with the selected combobox but I'm just not sure how to connect it to the arrays.
Can someone please help?

Here's the code.
string[] regionType = { "Europe", "Asia", "Australia" };
double[] rate = { 1950.00, 2250.00, 2550.00 };

        double regionRate,
               airfareCost = 0.0;

        public Form1()
        {
            InitializeComponent();
        }
        
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
        {
            int index;
            index = cboReg.SelectedIndex;
            regionRate = rate[index];
            displayRate();
        }
        private void displayRate()
        {
            txtPriceLand.Text = regionRate.ToString("C");
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            cboReg.Items.AddRange(regionType);
        }
        private void chkYes_CheckedChanged(object sender, EventArgs e)
        {
          
        }


[Edited]code is wrapped in "pre" tags[/Edited]
Posted
Updated 10-May-11 18:30pm
v2
Comments
cuteband 10-May-11 23:25pm    
you mean when you set your checkbox set to true then the combobox will bind the data is that what you mean?
Mlaura 10-May-11 23:27pm    
Yeah. So it's like when you choose "Europe" from the combobox, and check the checkbox for the airfare it'll output the airfare for Europe. Sorry if I'm not clear enough, I'm pretty new at this so I'm not exactly sure how to put it.
cuteband 10-May-11 23:50pm    
where do you want to show the result is it in textbox?
Hemant__Sharma 11-May-11 3:10am    
I've updated my solution. plz check

you can also do this:
C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (chkYes.Checked)
    {
        regionRate = rate[cboReg.SelectedIndex];
        displayRate();
    }
}

Edit:
if you want that to happen on checkbox's check change event remove all code from comboBox1_SelectedIndexChanged event and paste it to chkYes_checkedChanged method. like this:

C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    //nothing here
}
private void chkYes_CheckedChanged(object sender, EventArgs e)
{
    //Make sure cboReg.SelectedIndex is >= 0 because if nothing is selected
    //it will be -1 and that will cause an exception
    if (chkYes.Checked && cboReg.SelectedIndex >= 0)
    {
        regionRate = rate[cboReg.SelectedIndex];
        displayRate();
    }
}


Thanks,
Hemant
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 11-May-11 0:48am    
This is correct. Couple of minor problem: 1) OP should be noted that comboBox1_SelectedIndexChanged is not the event or event handler unless there is += operation on event, which is not shown; this is nothing more that a method; its name can be anything; 2) the name is this method violates Microsoft naming conventions (it does not matter that it is generated by Dispatcher, nobody says the name can be left as is; FxCop would warn it is incorrect). My 4.
--SA
Hemant__Sharma 11-May-11 0:58am    
you are right. i just copied the code he has pasted and did some refactoring and posted the refactored code.

I believe as a beginner he dragged and dropped the combobox on form and double clicked it, it created a default event handler for that control and because the default name was comboBox1 he got the event name appended to that ID only. Later he changed the combobox name to cboReg.

Thanks for the for the vote SA.
Regards,
Hemant
Sergey Alexandrovich Kryukov 11-May-11 15:07pm    
What you describe could happen, I agree.
--SA
Mlaura 11-May-11 3:32am    
Thank you, this one works for me. Thank you so much.
Hemant__Sharma 11-May-11 4:29am    
Welcome. Glad i could help :)

Thanks,
Hemant
Here is the solution: Set your properties Autopostback = True

C#
private void chkYes_CheckedChanged(object sender, EventArgs e)
            {
if (chkYes.checked == true && comboBox1.SelectedValue ="Europe" )
{
 txtPriceLand.Text = "1950.00"
}
else if (chkYes.checked == true && comboBox1.SelectedValue ="Asia" )
{
 txtPriceLand.Text = "2250.00"
}


} .


this is much easier for you.
 
Share this answer
 
Comments
Mlaura 11-May-11 1:53am    
I'm sorry, but how do I set the properties AutoPostBack?
Hemant__Sharma 11-May-11 3:02am    
You dont need to set AutoPostBack property that belongs to ASP.NET server controls and you are working with WinForm application.

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