Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have a combobox with five choices {item1,item2,item3,item4,item5} and two groupboxes
groupbox1{numericupdown1,numericupdown2}
groupbox2{txtbox1 txtbox2 txbox3 txbox4 txtbox5}

Now I want for every item of combo to enter 2 parameters a and b for numericupdown1 and numericupdown2 and automatically in groupbox2 txtboxes fill with calculated
a+b a*b a/b....

My problem is that code is becoming too long and I need a best practice to separate code for every item.

Please help because I am a beginner

Thx

C#
private void comboShperndarjet_SelectedIndexChanged(object sender, EventArgs e)
{
	switch (comboShperndarjet.SelectedIndex)
	{
		case 1:
			groupBoxKarakteristika.Visible = true;
			groupBoxParametra.Visible = true;

			break;


		case 2:
			groupBoxParametra.Visible = false;
			groupBoxKarakteristika.Visible = true;
			break;
		case 3:
			break;


		case 4:
			break;
		case 5:
			break;
		case 6:
			break;

	}


}

private void groupBoxParametra_Enter(object sender, EventArgs e)
{

}

private void numericUpDownAlpha_ValueChanged(object sender, EventArgs e)
{
	Decimal Alpha = numericUpDownAlpha.Value;
	Decimal Beta = numericUpDownBeta.Value;

	txtMinimum.Text = Convert.ToString(numericUpDownAlpha.Value);
	Decimal mesorja = (Alpha + Beta) / 2;
	txtMesorja.Text = mesorja.ToString();
	Decimal varianca = (((Alpha + Beta) * (Alpha + Beta)) - 1) / 2;
	txtVarianca.Text = varianca.ToString();
	txtMesorja.Refresh();
	txtVarianca.Refresh();

	
}

private void numericUpDownBeta_ValueChanged(object sender, EventArgs e)
{
	txtMaximum.Text = Convert.ToString(numericUpDownBeta.Value);
	Decimal mesorja = (numericUpDownAlpha.Value + numericUpDownBeta.Value) / 2;
	txtMesorja.Text = mesorja.ToString();
	txtMesorja.Refresh();
}


For every case I need different values in groupbox2 and I don't know how to make not repeat all the time.
Except this for every case numericupdown1 or numericupdown2 may be shown first/second or both so it is another
reason to create separate code file for every case
Posted
Updated 31-May-11 9:25am
v3
Comments
Sandeep Mewara 31-May-11 14:36pm    
My problem is that code is becoming too long and I need a best practice to separate code for every item.
Elaborate and share your code that you think has issues/needs modification.
OriginalGriff 31-May-11 15:03pm    
As Sandeep says: but only the relevant fragments! Don't just dump your entire file here.
And use "Improve question" widget to edit your question to do this, don't start a new question.
Sergey Alexandrovich Kryukov 31-May-11 15:06pm    
I would say: do not post your original code at all! Design some code sample simplified as much as possible but still manifesting the problem.
--SA
OriginalGriff 31-May-11 15:16pm    
I think that's his problem SA! He doesn't know how to simplify it... :)
Sergey Alexandrovich Kryukov 31-May-11 19:49pm    
Yes, certainly. I believe the attempt to create a demo like I say will give one a good chance to sort it out without any help.
--SA

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; }    // Not really necessary, but sometimes helpful
    int GetOperationResult(int leftValue, int rightValue);
    bool LeftArgumentVisiblle { get; }
    bool RightArgumentVisible { get; }
    // ... etc. - if you want to control visibility, or enabled state, of the text boxes, etc.
    }


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;    // Provides behavior for the currently selected combobox item


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 OperationNames 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)[^]
 
Share this answer
 
Comments
KORCARI 8-Jun-11 10:23am    
That's seems very good to me.
Bu please any illustration of
# 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.

regards
Think object oriented to start. You are entering the data for some single purpose, but need it multiple times. That is your object.
The combo box is the selctor of which instance you are accessing. When it is switched update all of the appropriate entry items (text boxes Nud controls etc.).

I might be missing something but it seems you have Group Boxes with all of these elements (text boxes and nuds) for each different selection in the combo box. Seperating in this way removes that need.

E.g. Say your items 1 through 5 all have a property "Name" that you want to capture in a text box field. Well no need to capture in
item1Name.Text
item2Name.Text and so on.

Have the combo box capture which is the active item (SelectedItem) and then when any of the props are changed you can send them directly to your object for udpating.
e.g. Nud value Changed
_selectedItem.SomeValue = e.NewValue
 
Share this answer
 
Indices are 0-based, so your switch statement should go from 0 to 4 and not 1 to 6 (???).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900