2017-03-07 85 views
1

我需要一個int血型類型以等於特定值,具體取決於檢查哪個單選按鈕。如果任何布爾值爲真,它將使血型等於特定值。我只是不知道如何讓bool爲int血型賦值。任何指針?如果bool爲真,則返回int

private void btnAddPatient_Click(object sender, RoutedEventArgs e)////Adds Patients using buttone etc to set properties 
    { 
     string name = txtPatientName.Text; 
     int bloodType,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 == "" || !bloodA || !bloodAB || !bloodB || !blood0) 
     { 

      if (txtPatientName.Text == "") 
      { 
       MessageBox.Show("Please enter Patient's Name"); 
      } 

      else if (dpDOB.SelectedDate == null) 
      { 
       MessageBox.Show("Please select a date"); 
      } 


      else if(!bloodA || !bloodAB || !bloodB || !blood0) 
      { 
       MessageBox.Show("Please enter patient's blood type"); 
      } 

     } 

     else 
     { 
      //bloodType How to make this equal to a value depending on what radio button is checked? 
      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(); 
     } 
    } 
+0

難道你不能只用'if if if'來檢查,哪個單選按鈕是真的並且分配正確的值? –

+0

bloodType的int值是在哪裏定義的? – maccettura

+0

是的,我可以。上帝,我在時間愚蠢。 –

回答

1

請與if else if,每個單選按鈕,如果這是真的,並指定合適的值。
事情是這樣的:

  else 
      { 
       //bloodType How to make this equal to a value depending on what radio button is checked? 
       if(bloodA) 
       { 
        bloodType = 0; 
       } 
       else if(bloodB) 
       { 
        bloodType = 1; 
       } 
      } 
+0

這工作正常! –

0

如果你不嵌套IFS的朋友,那麼另一種解決方案是將int值分配給每個單選按鈕的Tag屬性。然後,將一個組框中的所有按鈕分組,找出哪一個被檢查,最後將標記內容轉換爲你的int。

int i; 
var checkedButton = groupBox1.Controls 
       .OfType<RadioButton>() 
       .FirstOrDefault(rb => rb.Checked);    
if (int.TryParse(checkedButton.Tag.ToString(), out i)) 
{ 
    // here's your value 
} 
4

首先,我建議提取類型enum確切的說:如果你

BloodType bloodType = 
     rb0.IsChecked ? BloodType.O 
    : rbA.IsChecked ? BloodType.A 
    : rbB.IsChecked ? BloodType.B 
    : BloodType.AB; 

// [Flags] // you may want declare enum as Flags 
    public enum BloodType { 
    O = 0, 
    A = 1, 
    B = 2, 
    AB = 3 
    } 

然後你可以用一個三元運算符的幫助下分配BloodType值想要獲得int的價值,只需施放:

int v = (int) bloodType; 

BloodType t = (BloodType) v; 
+0

打倒我吧。絕對是最簡潔的方式...你可以定義公共枚舉BloodType {O,A,B,AB}'和你的'BloodType bloodType = rb0.checked? ......甚至在單行上。第三行將bloodType轉換爲int。 –

0

有很多方法可以做到這一點。這裏有一個:

傾向於強數據類型。使用Enum over int。

public enum BloodTypeEnum 
{ 
    TypeO = 0, 
    TypeA = 1, 
    TypeB = 2 
} 

一個實例添加到您的視圖模型

public class MainViewModel : ViewModelBase 
{ 
    private BloodTypeEnum _bloodType; 

    public BloodTypeEnum BloodType 
    { 
     get { return _bloodType; } 
     set 
     { 
      _bloodType = value; 
      RaisePropertyChanged(); 
     } 
    } 
} 

使用強大的數據類型和寫您的XAML一個ValueConverter

<StackPanel> 
    <StackPanel.Resources> 
     <local:EnumToBoolConverter x:Key="EnumToBoolConverter"></local:EnumToBoolConverter> 
    </StackPanel.Resources> 
    <StackPanel> 
     <RadioButton IsChecked="{Binding BloodType, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static local:BloodTypeEnum.TypeO}, Mode=TwoWay}">Type O</RadioButton> 
     <RadioButton IsChecked="{Binding BloodType, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static local:BloodTypeEnum.TypeA}, Mode=TwoWay}">Type A</RadioButton> 
     <RadioButton IsChecked="{Binding BloodType, Converter={StaticResource EnumToBoolConverter}, ConverterParameter={x:Static local:BloodTypeEnum.TypeB}, Mode=TwoWay}">Type B</RadioButton> 
    </StackPanel> 
</StackPanel> 

落實ValueConverter

public class EnumToBoolConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return object.Equals(value, parameter); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return System.Convert.ToBoolean(value) ? parameter : DependencyProperty.UnsetValue; 
    } 
}