每个项目的单独代码 [英] Separated code for every item

查看:53
本文介绍了每个项目的单独代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有五个选项{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事件中,您将:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆