每个项目的单独代码 [英] Separated code for every item
问题描述
我有一个带有五个选项{item1,item2,item3,item4,item5}的组合框和两个组框
groupbox1 {numericupdown1,numericupdown2}
groupbox2 {txtbox1 txtbox2 txbox3 txbox4 txtbox5}
现在,我要为组合的每个项目输入2个参数a和b分别用于numericupdown1和numericupdown2,并自动在groupbox2 txtbox中填充计算出的
a + b a * b a/b ....
我的问题是代码变得太长,我需要最佳实践来将每个项目的代码分开.
请帮助,因为我是初学者
Thx
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
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();
}
对于每种情况,我都需要在groupbox2中使用不同的值,而且我不知道如何始终不重复.
除此之外,在每种情况下,numericalupdown1或numericupdown2可能会先显示/后显示,或者两者都显示,所以它是另一个
为每种情况创建单独的代码文件的原因
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
推荐答案
据我了解您的问题,您有一个选择行为方式的组合框,并输入了一对数字组合框选择的数学运算,以及一组显示数学运算结果的文本框.
我建议您阅读有关策略模式 [
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; } }
}
在表单(或控制器,如果您使用的是Model View Controller模式)中,您将需要以下内容:
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
然后,在comboBox change事件中,您将:
- 使用适合于所选comobox项的类的实例更新
CurrentOperation
. - 使用
CurrentOperation
对UI进行所需的任何更改.
- 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.
Then, in the comboBox change event you''ll:
为简单起见,在获取正确的类以分配给CurrentOperation
时,可以使用switch语句.有关OO实施的更多信息,请参见工厂模式 [
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).
您还可以寻找一种发现所有继承IMathOperationDemo
的类,并用发现的所有类的OperationName
填充组合框的方式(使用反射从程序集中获取类型,并仅查找可分配给这些类型的类型). IMathOperationDemo
).如果您的工厂具有GetAllOperationNames()
方法(该方法可以执行发现;返回IEnumerable<string>
),则此方法可以很好地与Factory模式一起使用.
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.
认为面向启动的对象.您出于某些目的输入数据,但需要多次.那是你的对象.
组合框是您要访问的实例的选择器.切换时,更新所有适当的输入项(文本框Nud控件等).
我可能会丢失一些东西,但看来您有组合框,其中每个组合框的每个不同选择都有所有这些元素(文本框和裸露).以这种方式分离消除了这种需求.
例如.假设您的第1项到第5项都具有要在文本框字段中捕获的属性名称".好吧,无需捕获
item1Name.Text
item2Name.Text等.
具有组合框捕获功能,它是活动项目(SelectedItem),然后在更改任何道具时,可以将它们直接发送到对象进行udpating.
例如裸体值已更改
_selectedItem.SomeValue = e.NewValue
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
索引基于0,因此您的switch语句应从0到4而不是1到6(???).
Indices are 0-based, so your switch statement should go from 0 to 4 and not 1 to 6 (???).
这篇关于每个项目的单独代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!