【发布时间】:2017-03-07 22:27:16
【问题描述】:
我想对我的程序进行一些错误检查。 如果没有选择属性,我的程序应该会抛出一条错误消息,指示用户输入缺失的信息。 我已经为一些属性实现了这个,但处理单选按钮和布尔值的最后部分给我带来了麻烦。
private void btnAddPatient_Click(object sender, RoutedEventArgs e)////Adds Patients using buttone etc to set properties
{
string name = txtPatientName.Text,bloodType;
int x=1;
DateTime dob;
bool bloodA = rbA.IsChecked.Equals(true);
bool bloodB = rbB.IsChecked.Equals(true);
bool bloodAB = rbAB.IsChecked.Equals(true);
bool blood0 = rb0.IsChecked.Equals(true);
if (dpDOB.SelectedDate == null || txtPatientName.Text == "" || rbA.IsChecked.Equals(false) || rbB.IsChecked.Equals(false) || rbAB.IsChecked.Equals(false) || rb0.IsChecked.Equals(false))
{
if (txtPatientName.Text == "")
{
MessageBox.Show("Please enter Patient's Name");
}
else if (dpDOB.SelectedDate == null)
{
MessageBox.Show("Please select a date");
}
else if (rbA.IsChecked.Equals(false) || rbB.IsChecked.Equals(false) || rbAB.IsChecked.Equals(false) || rb0.IsChecked.Equals(false))
{
MessageBox.Show("Please enter patient's blood type");
}
}
else
{
if (bloodA)
{
bloodType = "A";
}
else if (bloodB)
{
bloodType = "B";
}
else if (bloodAB)
{
bloodType = "AB";
}
else
{
bloodType = "0";
}
dob = dpDOB.SelectedDate.Value;
Patient patient = new Patient(name, bloodType, x, dob);
MainWindow mainWindow = Owner as MainWindow;
patients.Add(patient);
lstPatients.ItemsSource = null;
lstPatients.ItemsSource = patients;
// this.Close();
}
}
【问题讨论】:
-
你到底遇到了什么麻烦?
-
即使我选中其中一个单选按钮,代码也不会转到分配属性等的 else 语句。
-
是的。您的代码说,如果未选中其中一个单选按钮,请发送警告