我需要一个int血型等于特定值,具体取决于检查哪个单选按钮。如果任何bool为真,它将使血型等于特定值。我只是不知道如何让bool为int bloodtype分配一个值。有什么指针吗?
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 :(得分:4)
首先,我建议提取类型,enum
确切地说:
// [Flags] // you may want declare enum as Flags
public enum BloodType {
O = 0,
A = 1,
B = 2,
AB = 3
}
然后您可以在三元运算符的帮助下分配BloodType
值:
BloodType bloodType =
rb0.IsChecked ? BloodType.O
: rbA.IsChecked ? BloodType.A
: rbB.IsChecked ? BloodType.B
: BloodType.AB;
如果你想获得int
值,只需投射:
int v = (int) bloodType;
BloodType t = (BloodType) v;
答案 1 :(得分: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;
}
}
答案 2 :(得分:0)
如果您不是嵌套ifs的朋友,那么另一种解决方案是将int值分配给每个单选按钮的Tag属性。然后,对组框中的所有按钮进行分组,找出哪个按钮被选中,最后将标记内容强制转换为int。
import sys
import ctypes
from sdl2 import *
import sdl2.sdlgfx as sdlgfx
if __name__ == "__main__":
SDL_Init(SDL_INIT_VIDEO)
window = SDL_CreateWindow("Test",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
200, 200, SDL_WINDOW_SHOWN)
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED)
############################################################
surface = SDL_CreateRGBSurface(0, 200, 200, 32,
0xff000000, # r mask
0x00ff0000, # g mask
0x0000ff00, # b mask
0x000000ff) # a mask
soft_renderer = SDL_CreateSoftwareRenderer(surface)
SDL_SetRenderDrawColor(soft_renderer, 255, 255, 0, 255) #yellow background
SDL_SetRenderDrawBlendMode(soft_renderer, SDL_BLENDMODE_NONE)
SDL_RenderClear(soft_renderer)
sdlgfx.filledCircleRGBA(soft_renderer, 100, 100, 100, 0, 255, 255, 255)
sdlgfx.filledCircleRGBA(soft_renderer, 100, 100, 80, 255, 255, 0, 255) #yellow
SDL_SetColorKey(surface, SDL_TRUE, 0xffff00ff) #yellow colorkey
circ = SDL_CreateTextureFromSurface(renderer, surface)
SDL_DestroyRenderer(soft_renderer)
SDL_FreeSurface(surface)
############################################################
running = True
event = SDL_Event()
while running:
# Event loop
while SDL_PollEvent(ctypes.byref(event)) != 0:
if event.type == SDL_QUIT:
running = False
break
# Rendering
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255)
SDL_RenderClear(renderer)
sdlgfx.thickLineRGBA(renderer, 200, 0, 0, 200, 10, 255, 255, 255, 255)
SDL_RenderCopy(renderer, circ, None, None)
SDL_RenderPresent(renderer);
SDL_DestroyTexture(circ)
SDL_DestroyRenderer(renderer)
SDL_DestroyWindow(window)
SDL_Quit()
答案 3 :(得分:0)
有很多方法可以做到这一点。还有一个:
首选强数据类型。使用Enum over int。
public enum BloodTypeEnum
{
TypeO = 0,
TypeA = 1,
TypeB = 2
}
将实例添加到ViewModel
public class MainViewModel : ViewModelBase
{
private BloodTypeEnum _bloodType;
public BloodTypeEnum BloodType
{
get { return _bloodType; }
set
{
_bloodType = value;
RaisePropertyChanged();
}
}
}
使用强数据类型和ValueConverter
编写XAML<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;
}
}