简体   繁体   English

C#事件点击包含多个方法

[英]C# event click containing more than one method

I am trying to nest methods, or place methods within other methods. 我正在尝试嵌套方法,或将方法放在其他方法中。 Using C# and Microsoft Visual Studio for the first time and my dilemma is this. 第一次使用C#和Microsoft Visual Studio,这是我的难题。 I have created form with an event click button to validate user input, which is fine in itself, but I need to validate more than one input when the button is clicked. 我已经创建了一个带有事件单击按钮的表单来验证用户输入,这本身很好,但是单击按钮时我需要验证多个输入。 In the Calculate button's click event handler, perform input validation on the three text boxes (the three that the user will use for data entry). 在“计算”按钮的单击事件处理程序中,对三个文本框(用户将用于输入数据的三个文本框)执行输入验证。 For code efficiency I want to use separate methods to achieve this. 为了提高代码效率,我想使用单独的方法来实现此目的。 I have tried writing more methods directly within the event handler, but no matter how I start the method public, private, static, void, main etc I always get errors. 我尝试直接在事件处理程序中编写更多方法,但是无论我如何启动方法public,private,static,void,main等,总是会出错。 Any assistance/advice would be greatly appreciated. 任何帮助/建议将不胜感激。

private void btnCalculate_Click(object sender, EventArgs e)      
        {                     
            int txtLength = 0;
            if ((txtLength < 5) & (txtLength > 50))
                MessageBox.Show("Length measurement is invalid" + "\r\n" + "Please enter a value between 5 and 50", "Data Invalid"); 


            int txtWidth = 0;         
            if ((txtWidth < 2) & (txtLength > 20))
                MessageBox.Show("Width measurement is invalid" + "\r\n" + "Please enter a value between 2 and 20");


            int txtAvgDepth = 0;
            if ((txtAvgDepth < 2) & (txtAvgDepth > 4))
                MessageBox.Show("Width measurement is invalid" + "\r\n" + "Please enter a value between 2 and 20");*/
        }

Changed code in method along with the error messages it throws. 方法中更改的代码及其引发的错误消息。 The original code had no syntax errors. 原始代码没有语法错误。

Problem solved. 问题解决了。 Thank you all very much for your help :) 非常感谢大家的帮助:)

Just add them to the class: 只需将它们添加到类中即可:

private void CalculateButton_Click(object sender, EventArgs e)      
{
    ValidateLength();
    ValidateWidth();
    ValidateDepth();
}

private void ValidateLength() 
{
    int txtLength = LengthTextBox.Value;
    if ((txtLength < 5) & (txtLength > 50))
        MessageBox.Show("Length measurement is invalid" + "\r\n" + "Please enter a value between 5 and 50", "Data Invalid"); 
}

private void ValidateWidth()
{
    int txtWidth = WidthTextBox.Value;         
    if ((txtWidth < 2) & (txtLength > 20))
        MessageBox.Show("Width measurement is invalid" + "\r\n" + "Please enter a value between 2 and 20");
}

private void ValidateDepth()
{
   int txtAvgDepth = DepthTextBox.Value;
   if ((txtAvgDepth < 2) & (txtAvgDepth > 4))
            MessageBox.Show("Depth measurement is invalid" + "\r\n" + "Please enter a value between 2 and 4");*/
}

I guess you're trying to tidy this up and have a single validation routine: 我想您正在尝试整理一下并有一个验证例程:

private void CalculateButton_Click(object sender, EventArgs e)      
{
    ValidateRange(LengthTextBox.Value, 5, 50, "Length");
    ValidateRange(WidthTextBox.Value, 2, 20, "Width");
    ValidateRange(DepthTextBox.Value, 2, 4, "Depth");
}

private void ValidateRange(int value, int min, int max, string msg) 
{
    if (value < min || value > max)
        MessageBox.Show(
            string.Format("{0} measurement is invalid\r\nPlease enter a value between {1} and {2}", msg, min, max), 
            "Data Invalid"); 
}

Click Handlers are known as delegates, they subscribe to events in a one-event, many subscribers fashion. 单击处理程序称为委托,它们以一种事件,许多订阅者的方式订阅事件。 What you are trying to achieve is completely possible but not from the IDE GUI, as it is designed to manage a single subscription. 您试图实现的目标是完全可能的,但不能通过IDE GUI实现,因为它旨在管理单个订阅。 You could instead subscribe to the event from code like this 您可以改为通过以下代码订阅活动

btnCalculate.Click += ClickHandler1;
btnCalculate.Click += ClickHandler2;
btnCalculate.Click += ClickHandler3;

Even though it's possible, i'd strongly advise against it as it seems over-engineered and it's not really how the platform was thought out to be used. 即使有可能,我还是强烈建议不要这样做,因为它似乎设计过度,而且并不是人们认为该平台的使用方式。 It shouldn't have any bad side effects as the .Net event/delegate system is ready to handle this use case but the maintainability suffers in the process. 它不会有任何不良影响,因为.Net事件/代理系统已准备好处理此用例,但是在此过程中,可维护性受到影响。

I'd rather go with an orchestrator method. 我宁愿使用协调器方法。 Subscribe once and call each validation method from there. 订阅一次,然后从那里调用每个验证方法。

More reading material: 更多阅读材料:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM