2016-09-17 3 views
0

Я действительно новичок в программировании. C# - это первый класс, который я взял, и я застрял в этом проекте. Мы должны были создать программу, которая будет вычислять стоимость семинара после выбора радиоклуба семинара и переключателя местоположения. У меня все работает так, как предполагается, кроме одного.Как остановить программу от вычисления, если не выбрана радиокнопка?

Предположим, вы выбрали мастерскую, но вы не выбрали место. У меня есть там, где появится MessageBox, говорящий «выбрать местоположение», но как мне остановить программу от вычисления, если это произойдет? На данный момент он просто вычислит и даст сумму местоположения 0. Мне нужно, чтобы он не вычислялся вообще.

public partial class frmWorkshopSelector : Form 
{ 

    public frmWorkshopSelector() 
    { 
     InitializeComponent(); 
     } 

    private void btnExit_Click(object sender, EventArgs e) 
    { 
     this.Close();  //When clicking the exit button, the program will close 
    } 

    private void btncalc_Click(object sender, EventArgs e) 
    { 
     int wsregistration = 0; 
     int lcost = 0; 
     const decimal DAYS = 3; 


     //For the following if statements, depending on what workshop and location is selected, 
     //their correstponding registration and lodging fees will be displayed 

     { 
      if (rbtHandlingStress.Checked == true) 
      { 
       wsregistration = 1000; 
      } 
      else if (rbtSupervisionSkills.Checked == true) 
      { 
       wsregistration = 1500; 
      } 
      else if (rbtTimeManagement.Checked == true) 
      { 
       wsregistration = 800; 
      } 

      else 
      MessageBox.Show("Please Select a Workshop"); 
      lblTotalCost.Text = ""; 
      lblLodgingCost.Text = ""; 
      lblRegistrationCost.Text = ""; 
     } 

     { 
      if (rbtAustin.Checked == true) 
      { 
       lcost = 150; 
      } 
      else if (rbtChicago.Checked == true) 
      { 
       lcost = 225; 
      } 
      else if (rbtDallas.Checked == true) 
      { 
       lcost = 175; 
      } 
      else 
      { 
       MessageBox.Show("Please Select a Location"); 
       lblRegistrationCost.Text = " "; 
       lblTotalCost.Text = " "; 
       lblLodgingCost.Text = " "; 
      } 
     } 

     lblRegistrationCost.Text = wsregistration.ToString("C"); 
     lblLodgingCost.Text = lcost.ToString("C"); 
     lblTotalCost.Text = (wsregistration + (lcost * DAYS)).ToString("C"); 

    } 

    private void btnReset_Click(object sender, EventArgs e) 
    { 
     //unchecks all radio buttons as well as clears out the previous calculations 
     lblRegistrationCost.Text = ""; 
     lblLodgingCost.Text = ""; 
     lblTotalCost.Text = ""; 
     rbtHandlingStress.Checked = false; 
     rbtSupervisionSkills.Checked = false; 
     rbtTimeManagement.Checked = false; 
     rbtAustin.Checked = false; 
     rbtChicago.Checked = false; 
     rbtDallas.Checked = false; 
    } 
} 

ответ

1

Вы должны выйти из метода. Добавлен оператор return в блоке else.

private void btncalc_Click(object sender, EventArgs e) 
{ 
    int wsregistration = 0; 
    int lcost = 0; 
    const decimal DAYS = 3; 


    //For the following if statements, depending on what workshop and location is selected, 
    //their correstponding registration and lodging fees will be displayed 
    if (rbtHandlingStress.Checked == true) 
    { 
     wsregistration = 1000; 
    } 
    else if (rbtSupervisionSkills.Checked == true) 
    { 
     wsregistration = 1500; 
    } 
    else if (rbtTimeManagement.Checked == true) 
    { 
     wsregistration = 800; 
    } 

    else 
    {  
     lblTotalCost.Text = ""; 
     lblLodgingCost.Text = ""; 
     lblRegistrationCost.Text = ""; 
     MessageBox.Show("Please Select a Workshop"); 
     return; 
    } 


    if (rbtAustin.Checked == true) 
    { 
     lcost = 150; 
    } 
    else if (rbtChicago.Checked == true) 
    { 
     lcost = 225; 
    } 
    else if (rbtDallas.Checked == true) 
    { 
     lcost = 175; 
    } 
    else 
    {  
     lblRegistrationCost.Text = " "; 
     lblTotalCost.Text = " "; 
     lblLodgingCost.Text = " "; 
     MessageBox.Show("Please Select a Location"); 
     return; 
    } 

    lblRegistrationCost.Text = wsregistration.ToString("C"); 
    lblLodgingCost.Text = lcost.ToString("C"); 
    lblTotalCost.Text = (wsregistration + (lcost * DAYS)).ToString("C"); 
} 
0

писать «возвращение» в любом месте в пределах функции выходит из этой функции, может быть, после того, как вы показываете окно сообщения введите местоположение, вы набираете

return; 

и это должно сделать работу.

0

Просто добавь возвращенную в вашем коде после того, как показывает окно сообщения, как показано ниже

public partial class frmWorkshopSelector : Form 
{ 

    public frmWorkshopSelector() 
    { 
     InitializeComponent(); 
     } 

    private void btnExit_Click(object sender, EventArgs e) 
    { 
     this.Close();  //When clicking the exit button, the program will close 
    } 

    private void btncalc_Click(object sender, EventArgs e) 
    { 
     int wsregistration = 0; 
     int lcost = 0; 
     const decimal DAYS = 3; 


     //For the following if statements, depending on what workshop and location is selected, 
     //their correstponding registration and lodging fees will be displayed 

     { 
      if (rbtHandlingStress.Checked == true) 
      { 
       wsregistration = 1000; 
      } 
      else if (rbtSupervisionSkills.Checked == true) 
      { 
       wsregistration = 1500; 
      } 
      else if (rbtTimeManagement.Checked == true) 
      { 
       wsregistration = 800; 
      } 

      else 
      MessageBox.Show("Please Select a Workshop"); 
      lblTotalCost.Text = ""; 
      lblLodgingCost.Text = ""; 
      lblRegistrationCost.Text = ""; 
      return; 
     } 

     { 
      if (rbtAustin.Checked == true) 
      { 
       lcost = 150; 
      } 
      else if (rbtChicago.Checked == true) 
      { 
       lcost = 225; 
      } 
      else if (rbtDallas.Checked == true) 
      { 
       lcost = 175; 
      } 
      else 
      { 
       MessageBox.Show("Please Select a Location"); 
       lblRegistrationCost.Text = " "; 
       lblTotalCost.Text = " "; 
       lblLodgingCost.Text = " "; 
       return; 
      } 
     } 

     lblRegistrationCost.Text = wsregistration.ToString("C"); 
     lblLodgingCost.Text = lcost.ToString("C"); 
     lblTotalCost.Text = (wsregistration + (lcost * DAYS)).ToString("C"); 

} 


    private void btnReset_Click(object sender, EventArgs e) 
    { //uncheks all radio buttons as well as clears out the previous calculations 
     lblRegistrationCost.Text = ""; 
     lblLodgingCost.Text = ""; 
     lblTotalCost.Text = ""; 
     rbtHandlingStress.Checked = false; 
     rbtSupervisionSkills.Checked = false; 
     rbtTimeManagement.Checked = false; 
     rbtAustin.Checked = false; 
     rbtChicago.Checked = false; 
     rbtDallas.Checked = false; 
    } 
} 

}

Смежные вопросы