As I understand your problem, you have a combobox that selects a mode of behavior, a pair of numbers that feed into math operations selected by the combo box, and a set of text boxes that show the result of the math operations.
I recommend you read about the
Strategy Pattern[
^].
How I would approach this problem is to create an interface that defines the behaviors:
interface IMathOperationDemo
{
string OperationName { get; set; }
int GetOperationResult(int leftValue, int rightValue);
bool LeftArgumentVisiblle { get; }
bool RightArgumentVisible { get; }
}
I would then create classes that implement this interface, one class for each mode, for example:
class SumDemo : IMathOperationDemo
{
public string OperationName
{
get { return "Sum Demo"; }
}
public int GetOperationResult(int leftValue, int rightValue)
{
return leftValue + rightValue;
}
bool LeftArgumentVisible { get { return true; } }
bool RightArgumentVisible { get { return true; } }
}
In the form (or controller, if you're using the Model View Controller pattern), you'll need the following:
IMathOperationDemo CurrentOperation;
Then, in the comboBox change event you'll:
- Update
CurrentOperation
with an instance of the class appropriate for the selected comobox item. - Use
CurrentOperation
to make whatever changes you need to the UI.
This design moves all code specific to the selected operation out of the form class, leaving only code that is common to all operations.
When getting the correct class to assign to CurrentOperation
, for simplicity, you could use a switch statement. For a more OO implementation look at the Factory Pattern[^] (whose purpose is to isolate deciding what sub-class of IMathOperationDemo
should be implemented).
You could also look at a way of discovering all classes that inherit IMathOperationDemo
and populating the combobox with the OperationName
s of all classes discovered (use reflection to get the types from the assembly, and find only those types that are assignable to IMathOperationDemo
). This could work well with the Factory Pattern, if your factory has a GetAllOperationNames()
method (which does the discovery work; returns IEnumerable<string>
).
Note - if you're interested in design patterns, I would look for as many different descriptions of the patterns you're interested in as possible, since there are inevitable different ways of understanding and presenting the ideas that the patterns express.
Factory Pattern (wikipedia)[
^]
Strategy Pattern (wikipedia)[
^]