Reputation: 1633
I have made an application that consists of a datePicker
and a comboBox
. The comboBox
has a list of numbers ranging from 0 through to 5. When the user selects "1" using the comboBox
my datePicker
"dpkClaim1
" will be enabled but the rest of my datePickers
will be disabled. If the user selects 2 both dpkClaim1
and dpkClaim2
will be enabled if the user selects 3 dpkClaim1
dpkClaim2
dpkClaim3
will be enabled and so on. I'm wondering if I could use a switch instead of a series of if statements to provide cleaner code.
My code is as fallows
xaml
<ComboBox x:Name="cbxNoClaims" SelectionChanged="cbxNoClaims_SelectionChanged" Loaded="cbxNoClaims_Loaded" Grid.Row="13" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Top" Width="120"/>
<DatePicker Name="dpkClaim1" Grid.Row="13" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Top"/>
<DatePicker Name="dpkClaim2" Grid.Row="14" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Top"/>
<DatePicker Name="dpkClaim3" Grid.Row="15" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Top"/>
<DatePicker Name="dpkClaim4" Grid.Row="16" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Top"/>
xaml.cs
private void cbxNoClaims_Loaded(object sender, RoutedEventArgs e)
{
// ... A List.
List<string> claim = new List<string>();
claim.Add("0 ");
claim.Add("1");
claim.Add("2");
claim.Add("3");
claim.Add("4");
claim.Add("5");
// ... Get the ComboBox reference.
var comboBox = sender as ComboBox;
// ... Assign the ItemsSource to the List.
comboBox.ItemsSource = claim;
comboBox.SelectedIndex = 0;
}
private void cbxNoClaims_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (cbxNoClaims.SelectedItem.ToString() == "1")
{
dpkClaim1.IsEnabled = true;
}
else
{
dpkClaim1.IsEnabled = false;
dpkClaim2.IsEnabled = false;
}
if (cbxNoClaims.SelectedItem.ToString() == "2")
{
dpkClaim1.IsEnabled = true;
dpkClaim2.IsEnabled = true;
}
else
{
dpkClaim2.IsEnabled = false;
}
if (cbxNoClaims.SelectedItem.ToString() == "3")
{
dpkClaim1.IsEnabled = true;
dpkClaim2.IsEnabled = true;
dpkClaim3.IsEnabled = true;
}
else
{
dpkClaim3.IsEnabled = false;
}
if (cbxNoClaims.SelectedItem.ToString() == "4")
{
dpkClaim1.IsEnabled = true;
dpkClaim2.IsEnabled = true;
dpkClaim3.IsEnabled = true;
dpkClaim4.IsEnabled = true;
}
else
{
dpkClaim4.IsEnabled = false;
}
Upvotes: 2
Views: 6954
Reputation: 245
In the onchanged event of the combobox you want to do something like this:
switch (combobox.SelectedItem) {
case "1":
datebox1.IsEnabled = true;
break;
case "2":
datebox1.IsEnabled = true;
datebox2.IsEnabled = true;
break;
default:
//what you want when nothing is selected
break;
}
Upvotes: 3
Reputation: 184271
This could be simplified a lot:
cbxNoClaims.ItemsSource = Enumerable.Range(1, 6);
var count = (int)cbxNoClaims.SelectedItem;
dpkClaim1.IsEnabled = count >= 1;
dpkClaim2.IsEnabled = count >= 2;
dpkClaim3.IsEnabled = count >= 3;
dpkClaim4.IsEnabled = count >= 4;
dpkClaim5.IsEnabled = count >= 5;
dpkClaim6.IsEnabled = count >= 6;
Though you still should really use data binding and data templating.
Upvotes: 3
Reputation: 7189
Sure you can. However, you might want to rethink the way you do things in your project (refering to H.B.'s answer).
switch(cbxNoClaims.SelectedItem) {
case "1":
// code here
break;
case "2":
// also here
break;
// etc...
}
Upvotes: 1
Reputation: 184271
Would not work with concrete instances at all, instead create a list of models based on the selected number, then bind an ItemsControl
with ItemTemplate
containing the ComboBox
to that. This will yield n inputs, without the need for disabling anything.
Upvotes: 0