Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# GUI
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 10-May-11 18:19pm
Mlaura261
Edited 10-May-11 19:30pm
v2
Comments
cuteband at 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 at 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 at 10-May-11 23:50pm
   
where do you want to show the result is it in textbox?
Hemant-Sharma-81 at 11-May-11 3:10am
   
I've updated my solution. plz check
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

you can also do this:
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:
 
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
  Permalink  
v2
Comments
SAKryukov at 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-81 at 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
SAKryukov at 11-May-11 15:07pm
   
What you describe could happen, I agree.
--SA
Mlaura at 11-May-11 3:32am
   
Thank you, this one works for me. Thank you so much.
Hemant-Sharma-81 at 11-May-11 4:29am
   
Welcome. Glad i could help :)
 
Thanks,
Hemant
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Here is the solution: Set your properties Autopostback = True
 
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.
  Permalink  
Comments
Mlaura at 11-May-11 1:53am
   
I'm sorry, but how do I set the properties AutoPostBack?
Hemant-Sharma-81 at 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)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 150
2 PIEBALDconsult 110
3 Richard MacCutchan 105
4 Sergey Alexandrovich Kryukov 104
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 11 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100