For this kind of scenario I typically use a "viewmodel" object that encapsulate the state, and then I use simple data-binding to control the visibility of associated controls.
For example, on this viewmodel class, you'd implement INotifyPropertyChanged, and have a ControlsVisible bool property, and an integer SelectedValue property:
public class ViewModel : INotifyPropertyChanged
{
public bool ControlsVisible
{
get { return SelectedValue == 0; }
}
private int selectedValue;
public bool SelectedValue
{
get { return selectedValue; }
set
{
if(value != selectedValue)
{
selectedValue = value;
OnNotifyPropertyChanged("SelectedValue");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
I would then generate a "Project DataSource" (Project > Add New Data Source ...) and select that ViewModel class.
The next step is to put a BindingSource on your WinForm, and set its DataSource to the ViewModel object. Remember to instantiate an object of type ViewModel, and set it at runtime on the BindingSource.DataSource property. I typically do this in an OnLoad override.
Then, you data bind the Visible property on each PictureBox to the BindingSource - ControlsVisible property.
Finally, you data bind set the ComboBox's SelectedValue Binding to the BindingSource - SelectedValue property.
For this to work I'm assuming that your ComboBox's DataSource is set and that its SelectedValue property is set to return an int. You might have to adapt the actual type of the SelectedValue on the ViewModel.
While this might look like a lot of work at first sight, once you've implemented this pattern a few times in your code, it will become second nature and you won't have to worry about modifying your code in many places. You just need to modify the ControlVisible's condition and let the Windows Forms simple data-binding mechanism do its job.
Cheers
this.Controls.OfType<PictureBox>().ForEach(c => c.Visible = comboBox1.SelectedIndex == 0);
– Damith 23 hours agoelse if
since theSelectedIndex
check is mutually exclusive. – swandog 23 hours ago