Using IF with CheckBoxes =S *Question*

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 753 Views - Last Post: 30 July 2013 - 05:23 PM Rate Topic: -----

#1 kwontum  Icon User is offline

  • New D.I.C Head

Reputation: -20
  • View blog
  • Posts: 19
  • Joined: 24-July 13

Using IF with CheckBoxes =S *Question*

Posted 28 July 2013 - 07:18 PM

I'll try let the code do the talking for me and ask questions after... =/
So far this is all i have written and now im getting stuck with using checkboxes
{
     public partial class Form1 : Form 
     {
    // Content item for the combo box
    private class Item
    {

      public string Name;
      public double Value;
      public Item(string name, double value)
      {
          Name = name; Value = value;
         


      }
      public override string ToString() 
      {
        // Generates the text shown in the combo box
        return Name;
      }
    }

         public Form1()
         {
             InitializeComponent();
             // Put some stuff in the combo box
             dropbx.Items.Add(new Item("Ashburton ", 17.99));
             dropbx.Items.Add(new Item("Timaru ", 24.77));
             dropbx.Items.Add(new Item("Oamaru ", 29.44));
             dropbx.Items.Add(new Item("Dunedin ", 35.66));
             dropbx.Items.Add(new Item("Invercargill ", 46.00));
             
         }

         private void dropbx_SelectedIndexChanged(object sender, EventArgs e)
         {
             // Display the Value property
             Item itm = (Item) dropbx.SelectedItem;
             lbx.Items.Add("It will cost " + textBox2.Text + " $" + itm.Value + " To travel to " + itm.Name);

         }


And my question is how do i use an if statement (and where do i place such code) to check wether any boxes are checked and if so to -20% from my selected Combobox items value?
Ive googled alot and cant seem to find any direct answers.
Im not necessarily asking for the code per say. Even any hints/reminders may help.

Dont know what happened there but line 40 is supposed to read
lbx.Items.Add("It will cost " + textBox2.Text + " $" + itm.Value + " To travel to " + itm.Name);



Is This A Good Question/Topic? 0
  • +

Replies To: Using IF with CheckBoxes =S *Question*

#2 Toadill  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 392
  • Joined: 08-January 12

Re: Using IF with CheckBoxes =S *Question*

Posted 28 July 2013 - 07:42 PM

Well it is not normal for a combo box to have a check option although there are a few work arounds. The first is to create a custom control that has check boxes.
Custom Control
Or you can try combining controls in a panel.
Combining Controls
Combobox with CheckedListBox

This post has been edited by Toadill: 28 July 2013 - 07:46 PM

Was This Post Helpful? 1
  • +
  • -

#3 toad87  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 188
  • Joined: 21-May 12

Re: Using IF with CheckBoxes =S *Question*

Posted 29 July 2013 - 02:43 PM

Edit: Nevermind...I misunderstood your question.

Did you want the checkboxes in the combobox next to each item? Or can they be on the form?

If it's just on the form, you can create eventhandlers for each checkbox and change the value of the items in the comobobox whenever the boxes are checked or unchecked.

This post has been edited by toad87: 29 July 2013 - 02:56 PM

Was This Post Helpful? 1
  • +
  • -

#4 kwontum  Icon User is offline

  • New D.I.C Head

Reputation: -20
  • View blog
  • Posts: 19
  • Joined: 24-July 13

Re: Using IF with CheckBoxes =S *Question*

Posted 29 July 2013 - 03:11 PM

View Posttoad87, on 29 July 2013 - 02:43 PM, said:

Edit: Nevermind...I misunderstood your question.

Did you want the checkboxes in the combobox next to each item? Or can they be on the form?

If it's just on the form, you can create eventhandlers for each checkbox and change the value of the items in the comobobox whenever the boxes are checked or unchecked.


Just on the form. I've created an event handler for the checkbox and and it works, but i have no idea how i can utilize this to edit my original values in my combobox =S
Was This Post Helpful? -1
  • +
  • -

#5 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3074
  • View blog
  • Posts: 10,268
  • Joined: 12-December 12

Re: Using IF with CheckBoxes =S *Question*

Posted 29 July 2013 - 03:44 PM

When should all this happen? Do you have a single checkbox which, when checked or unchecked, will alter the combobox's content?
Was This Post Helpful? 1
  • +
  • -

#6 kwontum  Icon User is offline

  • New D.I.C Head

Reputation: -20
  • View blog
  • Posts: 19
  • Joined: 24-July 13

Re: Using IF with CheckBoxes =S *Question*

Posted 29 July 2013 - 04:25 PM

View Postandrewsw, on 29 July 2013 - 03:44 PM, said:

When should all this happen? Do you have a single checkbox which, when checked or unchecked, will alter the combobox's content?

at this stage my application is quite crude as this only my second week into coding.
{
     public partial class Form1 : Form 
     {
    // Content item for the combo box
    private class Item
    {

      public string Name;
      public double Value;
      public Item(string name, double value)
      {
          Name = name; Value = value;
      }


      public override string ToString() 
      {
        // Generates the text shown in the combo box
        return Name;
      }
    }

         public Form1()
         {
             InitializeComponent();
             // Put some stuff in the combo box
             dropbx.Items.Add(new Item("Ashburton ", 17.99));
             dropbx.Items.Add(new Item("Timaru ", 24.77));
             dropbx.Items.Add(new Item("Oamaru ", 29.44));
             dropbx.Items.Add(new Item("Dunedin ", 35.66));
             dropbx.Items.Add(new Item("Invercargill ", 46.00));
             
         }

         private void dropbx_SelectedIndexChanged(object sender, EventArgs e)
         {
             // Display the Value property
             Item itm = (Item) dropbx.SelectedItem;
             lbx.Items.Add("It will cost " + textBox2.Text + " $"
                 + itm.Value + " To travel to " + itm.Name);

         }

         private void CheckBox1_CheckStateChanged(Object sender, EventArgs e)
         {



So as you may see i dont have a button to operate this, everything happens when you make a selection on my combobox
and what i am wanting to do now is make it so before you make your selection in the combobox, you can select the checkbox to give a 20% discount on your combobox selection.. if that makes sense. =)

View Posttoad87, on 29 July 2013 - 02:43 PM, said:

Edit: Nevermind...I misunderstood your question.

Did you want the checkboxes in the combobox next to each item? Or can they be on the form?

If it's just on the form, you can create eventhandlers for each checkbox and change the value of the items in the comobobox whenever the boxes are checked or unchecked.



So how can i "and change the value of the items in the comobobox whenever the boxes are checked or unchecked. "
i guess is my real question.
Was This Post Helpful? -1
  • +
  • -

#7 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3074
  • View blog
  • Posts: 10,268
  • Joined: 12-December 12

Re: Using IF with CheckBoxes =S *Question*

Posted 29 July 2013 - 04:38 PM

I'm a bit slow today. Do you have a single checkbox that when checked, or unchecked, will apply the 20% discount to all of the values in the combobox?
Was This Post Helpful? 1
  • +
  • -

#8 kwontum  Icon User is offline

  • New D.I.C Head

Reputation: -20
  • View blog
  • Posts: 19
  • Joined: 24-July 13

Re: Using IF with CheckBoxes =S *Question*

Posted 29 July 2013 - 04:44 PM

View Postandrewsw, on 29 July 2013 - 04:38 PM, said:

I'm a bit slow today. Do you have a single checkbox that when checked, or unchecked, will apply the 20% discount to all of the values in the combobox?

yes i have a checkbox but i cant figure out how to apply the 20% discount to all the values when checked =/
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3074
  • View blog
  • Posts: 10,268
  • Joined: 12-December 12

Re: Using IF with CheckBoxes =S *Question*

Posted 29 July 2013 - 04:45 PM

Well, it's a bit late for me, but the essential structure you need is:

        private void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            foreach (var itm in this.dropbx.Items)
            {

            }

        }

Was This Post Helpful? 2
  • +
  • -

#10 kwontum  Icon User is offline

  • New D.I.C Head

Reputation: -20
  • View blog
  • Posts: 19
  • Joined: 24-July 13

Re: Using IF with CheckBoxes =S *Question*

Posted 29 July 2013 - 04:48 PM

View Postandrewsw, on 29 July 2013 - 04:45 PM, said:

Well, it's a bit late for me, but the essential structure you need is:

        private void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            foreach (var itm in this.dropbx.Items)
            {

            }

        }

Thank you thank you thank you!!
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3074
  • View blog
  • Posts: 10,268
  • Joined: 12-December 12

Re: Using IF with CheckBoxes =S *Question*

Posted 29 July 2013 - 04:53 PM

You are welcome, welcome!

I guess you would use itm.Value (within the loop) to examine and change each item's Value :sorcerer:
Was This Post Helpful? 0
  • +
  • -

#12 kwontum  Icon User is offline

  • New D.I.C Head

Reputation: -20
  • View blog
  • Posts: 19
  • Joined: 24-July 13

Re: Using IF with CheckBoxes =S *Question*

Posted 29 July 2013 - 05:03 PM

View Postandrewsw, on 29 July 2013 - 04:53 PM, said:

You are welcome, welcome!

I guess you would use itm.Value (within the loop) to examine and change each item's Value :sorcerer:/>

so somthing along the lines of
         private void CheckBox1_CheckStateChanged(Object sender, EventArgs e)
         {
             foreach (var itm in dropbx.Items)
             {
                 itm.Value = itm.Value - (itm.Value*.2);
             }



???? or do i need to place the maths within my dropbx block??
Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3074
  • View blog
  • Posts: 10,268
  • Joined: 12-December 12

Re: Using IF with CheckBoxes =S *Question*

Posted 29 July 2013 - 05:21 PM

Why not give it a bash? You might change your ToString method temporarily so that you can see what is going on:

    return Name + " " + Value.ToString();

This post has been edited by andrewsw: 29 July 2013 - 05:21 PM

Was This Post Helpful? 0
  • +
  • -

#14 kwontum  Icon User is offline

  • New D.I.C Head

Reputation: -20
  • View blog
  • Posts: 19
  • Joined: 24-July 13

Re: Using IF with CheckBoxes =S *Question*

Posted 29 July 2013 - 05:25 PM

View Postandrewsw, on 29 July 2013 - 05:21 PM, said:

Why not give it a bash? You might change your ToString method temporarily so that you can see what is going on:

    return Name + " " + Value.ToString();

haha I was bashing in the totally wrong area ^.^
Was This Post Helpful? 0
  • +
  • -

#15 kwontum  Icon User is offline

  • New D.I.C Head

Reputation: -20
  • View blog
  • Posts: 19
  • Joined: 24-July 13

Re: Using IF with CheckBoxes =S *Question*

Posted 30 July 2013 - 02:01 PM

So this is the resulting "working" code.
Cheers for all your help. =)
         private void dropbx_SelectedIndexChanged(object sender, EventArgs e)
         {
             Item itm = (Item) dropbx.SelectedItem;
             if (checkBox1.Checked)
             {
                 itm.Value = (itm.Value - (itm.Value*.2));
             }

             
             lbx.Items.Add("It will cost " + textBox2.Text + " $"
                 + itm.Value + " To travel to " + itm.Name);

             
             
         }



Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2