2016-11-13 4 views
-1

Ich kämpfe mit, warum mein Code nicht funktioniert. Dies ist für eine Schulaufgabe. Ich darf um Hilfe bitten. Ich bin neu im Programmieren. Ich benutze Visual Studio 2015. Ich versuche, es zu bekommen, so dass der Benutzer nur eine Checkbox auswählen darf. Ich habe andere Checkboxen in dieser Zuweisung, so dass die Verwendung der letzten Überprüfung nicht funktioniert. Ich bekomme keine Fehler, es tut einfach nichts. Vielen Dank!Checkbox nur eine erlaubt, Code funktioniert nicht

Meine Ankreuzfelder sind CheckBox1, checkbox2 genannt, ...... 5

Mein ganzer aktuellen Code ist:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Chapter6Homework 
{ 
    public partial class IceCreamOrder : Form 
    { 
     public IceCreamOrder() 
     { 
      InitializeComponent(); 
     } 

     private void btn_Clear_Click(object sender, EventArgs e) 
     { 
      // Clear flavors by automatically selecting default button on Clear button click 
      rdbDefault.Checked = true; 

      // Clear toppings 
      checkBox_CookieDough.CheckState = CheckState.Unchecked; 
      checkBox_ChocolateSyrup.CheckState = CheckState.Unchecked; 
      checkBox_Marshmallows.CheckState = CheckState.Unchecked; 
      checkBox_OreoPieces.CheckState = CheckState.Unchecked; 
      checkbox_Sprinkles.CheckState = CheckState.Unchecked; 
      checkbox_Walnuts.CheckState = CheckState.Unchecked; 

      // Clear List Box 
      lstDisplay.Items.Clear(); 

      // Clear scoops 
      checkBox1.CheckState = CheckState.Unchecked; 
      checkBox2.CheckState = CheckState.Unchecked; 
      checkBox3.CheckState = CheckState.Unchecked; 
      checkBox4.CheckState = CheckState.Unchecked; 
      checkBox5.CheckState = CheckState.Unchecked; 
     } 

     private void btn_CalculateCost_Click(object sender, EventArgs e) 
     { 
      // Verify user selected a flavor 
      if (rdbDefault.Checked == true) 
      { 
       MessageBox.Show("Please select a flavor"); 
       return; 
      } 

      // Verify user seleted # of scoops 
      if (checkBox1.CheckState == CheckState.Unchecked && 
        checkBox2.CheckState == CheckState.Unchecked && 
        checkBox3.CheckState == CheckState.Unchecked && 
        checkBox4.CheckState == CheckState.Unchecked && 
        checkBox5.CheckState == CheckState.Unchecked) 
      { 
       MessageBox.Show("You must select a number of scoops. 1 is a must but 5 is recommended!"); 
       return; 
      } 

      //Verify user got the toppings they wanted if any 
      if (checkBox_ChocolateSyrup.CheckState == CheckState.Unchecked && 
       checkBox_CookieDough.CheckState == CheckState.Unchecked && 
       checkBox_Marshmallows.CheckState == CheckState.Unchecked && 
       checkBox_OreoPieces.CheckState == CheckState.Unchecked && 
       checkbox_Sprinkles.CheckState == CheckState.Unchecked && 
       checkbox_Walnuts.CheckState == CheckState.Unchecked) 
      { 
       DialogResult dr = MessageBox.Show("Are you sure you don't want toppings?", 
        "help", MessageBoxButtons.YesNo); 
       switch (dr) 
       { 
        case DialogResult.Yes: break; 
        case DialogResult.No: return; 
       } 
      } 

      // Declare Variables and constants 
      double flavorCost = FlavorCost(); 
      double toppingCost = ToppingCost(); 
      double scoops = Scoops() * flavorCost; 
      double subTotal = (flavorCost + toppingCost + scoops); 
      double salesTax = subTotal * .08; 
      double total = subTotal + salesTax; 

      // Display total price of order 
      lstDisplay.Items.Clear(); 
      lstDisplay.Items.Add("Total: " + total.ToString("C2")); 

      // Display total sales tax 
      lstDisplay.Items.Add(""); 
      lstDisplay.Items.Add("Sales Tax: " + salesTax.ToString("C2")); 

      // Display Flavor Cost 
      lstDisplay.Items.Add("Flavor:  " + flavorCost.ToString("C2")); 

      // Display Scoops Cost 
      lstDisplay.Items.Add("Scoops:  " + scoops.ToString("C2")); 

      // Display Toppings 
      lstDisplay.Items.Add("Toppings: " + toppingCost.ToString("C2")); 
     } 
     // Get flavor cost 
     Double FlavorCost() 
     { 
      if ((radioButton_Chocolate.Checked == true) || (radioButton_Strawberry.Checked == true)) 
       return 1.5F; 
      else if (radioButton_Vanilla.Checked == true) 
       return 1.25F; 
      else 
       return 0; 
     } 

     // Get num of scoops 
     Double Scoops() 
     { 
      if (checkBox1.Checked == true) 
       return 1; 
      else if (checkBox2.Checked == true) 
       return 2; 
      else if (checkBox3.Checked == true) 
       return 3; 
      else if (checkBox4.Checked == true) 
       return 4; 
      else if (checkBox5.Checked == true) 
       return 5; 
      else 
       return 0; 
     } 

     // Get Toppings 
     Double ToppingCost() 
     { 
      if ((checkBox_ChocolateSyrup.Checked == true) || 
       (checkBox_Marshmallows.Checked == true) || 
       (checkbox_Sprinkles.Checked == true)) 
       return .25F; 
      else if ((checkBox_OreoPieces.Checked == true) || 
        (checkBox_CookieDough.Checked == true) || 
        (checkbox_Walnuts.Checked == true)) 
       return .50F; 
      else 
       return 0; 
     } 

     private void IceCreamOrder_Load_1(object sender, EventArgs e) 
     { 
      //Set Default to true on load 
      rdbDefault.Checked = true; 
     } 

     internal class Sub 
     { 
     } 

     private void checkBox1_CheckedChanged(object sender, EventArgs e) 
     { 
      int numberChecked = 0; 
      CheckBox[] array = new 
      CheckBox[] { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5 }; 
      for (int i = 0; i < array.Length; i++) 
      { 
       if 
       (array[i].Checked) 
        numberChecked++; 
       else if 
       (numberChecked > 1) 
        MessageBox.Show("You have checked " 
       + numberChecked.ToString() + " checkBoxes. Only one is allowed."); 
       else 
        return; 
      } 

     } 
    } 
} 
+0

beginnen mit der Verwendung des Debuggers und Schreiten durch den Code, der zweite mit der Formatierung des Codes konsequenter sein in Bezug auf Ihre 'wenn:

Um Ihre Lösung funktioniert else blockt drittens nach, was das Schlüsselwort 'break or return' verwendet und wie man es benutzt. – MethodMan

+2

Ihr Code scheint korrekt zu sein. Welche Technologie verwendest du? Ist es Windows-Formulare, WPF oder etc. Auch RadioButton schauen Sie vielleicht nach dem Verhalten von RadioButton. – Emad

+0

Ich habe meinen gesamten Code für dieses Projekt hinzugefügt. Ich weiß, dass ich mehr als ein Problem habe, aber die Checkboxen sind die ersten, die ich angehen möchte. Ich bekomme keine Fehler mit meinem Code. Es tut einfach nichts. Und ich habe einige Radiobuttons, aber diese sind Checkboxen. – Jessie

Antwort

1

Verwenden RadioButton mit Gruppierung. (Absender ist das ausgewählte Kontrollkästchen)

private void checkBox1_Checked(object sender, EventArgs e) 
{ 
var array = new CheckBox[] { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5 }; 
foreach(var checkbox in array) 
{ 
    if(checkbox != sender){ 
     checkbox.IsChecked = false 
    } 
} 
Verwandte Themen