locked
combobox1 combobox2

    Question

  • namespace WindowsFormsApplication1
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
        }
            
        private void button2_Click(object sender, EventArgs e)
        {
          string trede;
          double sktr;
          if (comboBox1.SelectedIndex == 0)
          {
            trede = "100.50 110.25 120.50 135.15";
            //with the selection of combobox2 let say its the first item sktr has to be 100.50
            sktr = comboBox2.Text;       
          }
          else if (comboBox1.SelectedIndex == 1)
          {
            trede = "120.75 140.25 160.50 175.25";
            sktr = comboBox2.Text;        
          }
          else if (comboBox1.SelectedIndex == 2)
          {
            trede = "140.85 170.15 190.40 275.25";
            sktr = comboBox2.Text;
          }
          
          double ppt = 0;
          if (radioButton8.Checked)
          {
            ppt = 0;
          }
          else if (radioButton9.Checked)
          {
            ppt = 0.4;
          }
          else if (radioButton10.Checked)
          {
            ppt = 0.8;
          }
          else if (radioButton11.Checked)
          {
            ppt = 0.12;
          }
          // i have no idea how to do this but this is an error too
          if (ppt = 0)
          {
            label10.Text = "totaal: " + sktr.ToString();
            //MessageBox.Show("ppt 0");
          }
          else
          {
            double totaal;
            totaal = (sktr + (sktr * ppt));
            //when the string end like 130.50 output is 130.5 i would like that it would be 130.50
            label10.Text = "totaal: " + Math.Round(totaal,2).ToString();
          }
        }
      }
    }
    

    Im having combobox1 and combobox2

    combobox1 has a few items like: a10 a20 a30 

    combobox2 has a few items like: 1 2 3 4

    When having those 2 selected then sktr will be 1 value to use in the calculation bellow. Help would be nice. thank in advance.

    Thursday, December 23, 2010 8:09 AM

Answers

  • For

    //when the string end like 130.50 output is 130.5 i would like that it would be 130.50
    
    label10.Text = "totaal: " + Math.Round(totaal,2).ToString();

    use a format like

    label10.Text = "totaal: "
     + Math.Round(totaal,2).ToString("000.00"
    );

    so your string will be XXX.XX format!

     

    And the

    sktr = comboBox2.Text;

    is also not right. If you want the selected item in comboBox2 then do this:

     

    sktr = comboBox2.SelectedItem;

    Happy to Help :)
    • Proposed as answer by vijaykamat Thursday, December 23, 2010 10:31 AM
    • Marked as answer by TALES75 Thursday, December 23, 2010 10:42 AM
    Thursday, December 23, 2010 8:15 AM
  • use SelectedIndex to compare which row has been selected:

       if (comboBox1.SelectedIndex == 0)
       {
        string[] trede = { "100.50", "110.25", "120.50", "135.15", };
        sktr = trede[comboBox2.SelectedIndex];
       }
    

     

    but would be better to use trade array as global variable for the whole class, so you wont need to write the array (trade) in every loop:

    like:

      public partial class Form1 : Form
      {
        string[] trede;
        string sktr;
        public Form1()
        {
          InitializeComponent();
          CreatingTredes();
        }
    
        private void CreatingTredes()
        {
          trede = new string[] { "100.50", "110.25", "120.50", "135.15" };
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
          int index = comboBox1.SelectedIndex;
          sktr = trede[index];
        }

    • Marked as answer by TALES75 Thursday, December 23, 2010 10:43 AM
    Thursday, December 23, 2010 10:00 AM
  • lol, i set sktr as a string, and you have it as double, so only do:

    sktr = Convert.ToDouble(trade[index]);

    ... and it shoud work.

     

    • Marked as answer by TALES75 Thursday, December 23, 2010 10:23 AM
    Thursday, December 23, 2010 10:11 AM
  • First of all your question is not clear.. As far as

    // i have no idea how to do this but this is an error too
    
    if (ppt = 0)

    is concered change it to(==), single = sign is assignment and == is for comparison.

     

    // i have no idea how to do this but this is an error too
    
    if (ppt == 0)

    Happy to Help :)
    • Proposed as answer by vijaykamat Thursday, December 23, 2010 10:31 AM
    • Marked as answer by TALES75 Thursday, December 23, 2010 10:42 AM
    Thursday, December 23, 2010 8:12 AM


  • //when the string end like 130.50 output is 130.5 i would like that it would be 130.50
    label10.Text = "totaal: " + Math.Round(totaal,2).ToString();

    Change this line to:

     

    label10.Text = "totaal: " + Math.Round(totaal,2).ToString("#.00");

    If you want to know more about string formats for numbers in 'double' check:

    http://www.csharp-examples.net/string-format-double/

    Hope this helps.

    Please Mark it as answer if it helped you.

     


    Santosh.
    • Marked as answer by TALES75 Thursday, December 23, 2010 10:43 AM
    Thursday, December 23, 2010 9:04 AM

All replies

  • First of all your question is not clear.. As far as

    // i have no idea how to do this but this is an error too
    
    if (ppt = 0)

    is concered change it to(==), single = sign is assignment and == is for comparison.

     

    // i have no idea how to do this but this is an error too
    
    if (ppt == 0)

    Happy to Help :)
    • Proposed as answer by vijaykamat Thursday, December 23, 2010 10:31 AM
    • Marked as answer by TALES75 Thursday, December 23, 2010 10:42 AM
    Thursday, December 23, 2010 8:12 AM
  • For

    //when the string end like 130.50 output is 130.5 i would like that it would be 130.50
    
    label10.Text = "totaal: " + Math.Round(totaal,2).ToString();

    use a format like

    label10.Text = "totaal: "
     + Math.Round(totaal,2).ToString("000.00"
    );

    so your string will be XXX.XX format!

     

    And the

    sktr = comboBox2.Text;

    is also not right. If you want the selected item in comboBox2 then do this:

     

    sktr = comboBox2.SelectedItem;

    Happy to Help :)
    • Proposed as answer by vijaykamat Thursday, December 23, 2010 10:31 AM
    • Marked as answer by TALES75 Thursday, December 23, 2010 10:42 AM
    Thursday, December 23, 2010 8:15 AM
  • Thank for that ==

     

    if (comboBox1.SelectedIndex == 0)
       {
        trede = "100.50 110.25 120.50 135.15";
        //with the selection of combobox2 let say its the first item sktr has to be 100.50
        sktr = comboBox2.Text;    
       }
    

    combobox1 has a few items like: a10 a20 a30 

    combobox2 has a few items like: 1 2 3 4

    If i select combobox1-> a20 and for combobox2 -> 2 i would like that sktr will be: 110.25 

    Thursday, December 23, 2010 8:21 AM
  • Please check my last post I edited it for your stirng format problem.

     

    For your combo box solution it is:

     private
     void
     button2_Click(object
     sender, EventArgs e)
    {
    switch(combo1.SelectedItem)
    {
    case "a10":
    {
    switch(combo2.SelectedItem)
    {
    case 1:
    sktr = 110.25
    case2:
    .
    .

    }



    break;
    case "a20"
    .
    .
    .
    }

    }

    Happy to Help :)
    Thursday, December 23, 2010 8:25 AM
  • string trede;
    double sktr;
     if (comboBox1.SelectedIndex == 0)
       {
         trede = "100.50 110.25 120.50 135.15";
         sktr = trede.comboBox2.SelectedIndex;       
        }
    

    i think the switch methode will work. But is their not something like this: sktr = trede.combobox2.SelectedIndex;


    Thursday, December 23, 2010 8:48 AM


  • //when the string end like 130.50 output is 130.5 i would like that it would be 130.50
    label10.Text = "totaal: " + Math.Round(totaal,2).ToString();

    Change this line to:

     

    label10.Text = "totaal: " + Math.Round(totaal,2).ToString("#.00");

    If you want to know more about string formats for numbers in 'double' check:

    http://www.csharp-examples.net/string-format-double/

    Hope this helps.

    Please Mark it as answer if it helped you.

     


    Santosh.
    • Marked as answer by TALES75 Thursday, December 23, 2010 10:43 AM
    Thursday, December 23, 2010 9:04 AM
  • Many thanks for that URL.
    Thursday, December 23, 2010 9:37 AM
  • So your issue has been salved?
    Thursday, December 23, 2010 9:47 AM
  • No not yet still wnat to have this but i can't.

    if (comboBox1.SelectedIndex == 0)
      {
       string[] trede = { "100.50", "110.25", "120.50", "135.15", };
    
       sktr = trede[comboBox2.Select];
      }
    
    so from the selection of combobox2 take that value out of trade.

    Thursday, December 23, 2010 9:52 AM
  • use SelectedIndex to compare which row has been selected:

       if (comboBox1.SelectedIndex == 0)
       {
        string[] trede = { "100.50", "110.25", "120.50", "135.15", };
        sktr = trede[comboBox2.SelectedIndex];
       }
    

     

    but would be better to use trade array as global variable for the whole class, so you wont need to write the array (trade) in every loop:

    like:

      public partial class Form1 : Form
      {
        string[] trede;
        string sktr;
        public Form1()
        {
          InitializeComponent();
          CreatingTredes();
        }
    
        private void CreatingTredes()
        {
          trede = new string[] { "100.50", "110.25", "120.50", "135.15" };
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
          int index = comboBox1.SelectedIndex;
          sktr = trede[index];
        }

    • Marked as answer by TALES75 Thursday, December 23, 2010 10:43 AM
    Thursday, December 23, 2010 10:00 AM
  • im getting this error:

    Error 1 Cannot implicitly convert type 'string' to 'double'

    Thursday, December 23, 2010 10:10 AM
  • lol, i set sktr as a string, and you have it as double, so only do:

    sktr = Convert.ToDouble(trade[index]);

    ... and it shoud work.

     

    • Marked as answer by TALES75 Thursday, December 23, 2010 10:23 AM
    Thursday, December 23, 2010 10:11 AM
  • namespace WindowsFormsApplication1
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();
        }        
        private void button2_Click(object sender, EventArgs e)
        {
          double sktr;
          if (comboBox1.SelectedIndex == 0)
          {
            string[] trede = { "100.50", "110.25", "120.50", "135.15" };
            sktr = Convert.ToDouble(trede[comboBox2.SelectedIndex]);
          }
          else if (comboBox1.SelectedIndex == 1)
          {
            string[] trede = { "120.75", "140.25", "160.50", "175.25" };
            sktr = Convert.ToDouble(trede[comboBox2.SelectedIndex]);
          }
          else if (comboBox1.SelectedIndex == 2)
          {
            string[] trede = { "130.25", "160.25", "170.55", "195.75" };
            sktr = Convert.ToDouble(trede[comboBox2.SelectedIndex]);
          }
          
          double ppt = 0;
          if (radioButton8.Checked)
          {
            ppt = 0;
          }
          else if (radioButton9.Checked)
          {
            ppt = 0.4;
          }
          else if (radioButton10.Checked)
          {
            ppt = 0.8;
          }
          else if (radioButton11.Checked)
          {
            ppt = 0.12;
          }
    
          if (ppt == 0)
          {
            //Here im getting: Error 1 Use of unassigned local variable 'sktr'
            label10.Text = "totaal: " + sktr.ToString("#.00");
          }
          else
          {
            double totaal;
    
            //Here im getting: Error 1 Use of unassigned local variable 'sktr'
            totaal = (sktr + (sktr * ppt));
            label10.Text = "totaal: " + Math.Round(totaal,2).ToString("#.00");
          }
        }
      }
    }
    
    This is what i have till now. 
    Thursday, December 23, 2010 10:21 AM
  • I have it  double sktr =0;

    many thanks everybody for the help and use of you knowlage.

    Thursday, December 23, 2010 10:23 AM
  • Hey, thats why we are here :)

    I am glad you are glad, and the code works like you want to.

    bye,

    Mitja

    Thursday, December 23, 2010 10:28 AM
  • Your most welcome :)

    Please mark answer all the posts that helped :)


    Happy to Help :)
    Thursday, December 23, 2010 10:31 AM