如何使用独立的.cs文件在C#? [英] How to use separate .cs files in C#?

查看:173
本文介绍了如何使用独立的.cs文件在C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

论坛;我是工作了一段代码的新手。我想知道使用含有其他类和函数的单独的.cs文件的最好方法。作为一个基本功能的一个例子是想点击一个按钮,清晰,并有明确的表单上的所有字段。代码被分为三个的.cs文件。 Main.cs,MainForm.Designer.cs和btClear.cs。

Forum; I am a newbie working out a bit of code. I would like to know the best way to use separate .cs files containing other classes and functions. As an example of a basic function would would like to click on a clear button and have it clear all fields on a form. The code is split up into three .cs files. Main.cs, MainForm.Designer.cs and btClear.cs.

MainForm.Designer.cs包含自动创建的代码包括清除按钮的设计,并在文本框中我想清楚了。

MainForm.Designer.cs contains the designers automatically created code including the Clear Button, and the text boxes I would like to clear.

我想有代码清除包含在btClear.cs文本框中。我知道这将是很容易简单地将此代码放在MainForm.Designer.cs不过,我想为了使用单独的.cs文件作为标准的做法,以此为模板。

I would like to have the code to clear the text boxes contained in btClear.cs. I understand it would be easy to simply to place this code in MainForm.Designer.cs however I would like to use this as a template in order to use separate .cs files as a standard practice.

有人可以给我如何做到这一点吗?

Can someone give me an example of how to do this please?

推荐答案

大多数.NET程序员会做的方式是:

The way most .net programmers would do it is:


  • 将在 MainForm.cs所有代码,但申报分离,做清算代码的新方法。

  • Leave all your code inside MainForm.cs, but declare a new method for separating the code that does the clearing.

我总是在事件处理方法离开(一VS产生,当你双击按钮)代码来改变鼠标光标(万一我打电话的代码需要几秒钟或更长时间运行)和捕捉所有未处理的异常,并把我的逻辑在一个单独的私有方法:

I always leave in the event handler method (the one VS generates when you double-click the button) code to change the mouse cursor (in case the code I'm calling takes a couple of seconds or more to run) and catch all unhandled exceptions, and put my logic in a separate private method:

partial class MainForm : Form // this is MainForm.cs
{
    // This is the method VS generates when you double-click the button
    private void clearButton_Click(object sender, EventArgs e)
    {
        this.Cursor = Cursors.WaitCursor;
        try
        {
            ClearForm();
        }
        catch(Exception ex)
        {
            // ... have here code to log the exception to a file 
            // and/or showing a message box to the user
        }
        finally
        {
            this.Cursor = Cursors.Default;
        }
    }

    private void ClearForm()
    {
        // clear all your controls here
        myTextBox.Clear();
        myComboBox.SelectedIndex = 0;
        // etc...
    }
}

在我认为,如果要按照.NET做事的传统方式,你应该坚持这个第一个例子。

In my opinion, if you want to follow the conventional way of doing things in .net, you should stick with this first example.

移动代码的表格的.cs文件推荐的当代码是不是形式逻辑的一部分,例如,数据访问代码或业务逻辑。这这种情况下,我不认为清除表单控件资格作为业务逻辑。这仅仅是形式的代码的一部分,应该留在 MainForm.cs

Moving code out of the form .cs files is recommended when the code is not part of the form logic, for example, data access code or business logic. It this case I don't think that clearing the form controls qualifies as business logic. It is just part of the form code and should stay in the MainForm.cs.

但是,如果你真的想把 ClearForm 在另一个cs文件的方法,你可以创建一个新的类,移动 ClearForm 方法那里。您还需要每个控件的修饰符属性更改为公共所以你的新类可以从外面的MainForm访问它们。事情是这样的:

But if you really want to put the ClearForm method in another .cs file, you could create a new class and move the ClearForm method there. You would also need to change the Modifiers property of each control to Public so your new class can have access to them from outside MainForm. Something like this:

public class FormCleaner // this is FormCleaner.cs
{
    public void ClearForm(MainForm form)
    {
        // clear all your controls here
        form.myTextBox.Clear();
        form.myComboBox.SelectedIndex = 0;
        // etc...
    }
}

您必须改变主要形式的代码:

You would have to change the main form code to:

partial class MainForm : Form // this is MainForm.cs
{
    // This is the method VS generates when you double-click the button
    private void clearButton_Click(object sender, EventArgs e)
    {
        this.Cursor = Cursors.WaitCursor;
        try
        {
            FormCleaner cleaner = new FormCleaner();
            cleaner.ClearForm(this);
        }
        catch(Exception ex)
        {
            // ... have here code to log the exception to a file 
            // and/or showing a message box to the user
        }
        finally
        {
            this.Cursor = Cursors.Default;
        }
    }
}



但要注意,你的 FormCleaner 类就必须了解你的的MainForm 类,以便知道如何清除每个表单的控件,或你需要拿出一个通用的算法,能够通过控制回路任何形式的收集清除它们:

But note that your FormCleaner class would have to know about your MainForm class in order to know how to clear each of the controls of the form, or you would need to come up with a generic algorithm that is able to loop through the Controls collection of any form to clean them:

public class FormCleaner // this is FormCleaner.cs
{
    // 'generic' form cleaner
    public void ClearForm(Form form)
    {
        foreach(Control control on form.Controls)
        {
            // this will probably not work ok, because also
            // static controls like Labels will have their text removed.
            control.Text = "";
        }
    }
}

和如其他人所说, MainForm.Designer.cs 是机器生成的,你不应该把自己的代码那里。

And as others have said, MainForm.Designer.cs is machine-generated and you should never put your own code there.

这篇关于如何使用独立的.cs文件在C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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