简体   繁体   English

在多个Windows窗体中重复使用相同的方法

[英]Re-using same method in multiple windows forms

I would like to ask for your help. 我想请你帮忙。 Will you help captain newbie once again? 您会再次帮助新手上尉吗? :) :)

I have several windows forms where I use datagridview. 我有几个使用datagridview的Windows窗体。 I would like to format the datagridviews in the same way on all the forms (eg AllowUserToAddRows = false;). 我想在所有表单上以相同的方式设置datagridviews的格式(例如AllowUserToAddRows = false;)。

To do this I created a class MYFormatting and method as below. 为此,我创建了一个类MYFormatting和方法,如下所示。 I am going to use composition to re-use this method on multiple forms. 我将使用合成在多种形式上重复使用此方法。 I would be grateful if you could let tell me if my approach is correct? 如果您能告诉我我的方法是否正确,我将不胜感激。

public class MyFormating
    {
        public void FormatDGV(DataGridView dgv)
        {
            dgv.AllowUserToAddRows = false;
            dgv.AllowUserToDeleteRows = false;
            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgv.ColumnHeadersVisible = false;
            dgv.RowHeadersVisible = false;
            dgv.MultiSelect = false;
        }
    }

When initializing new form 初始化新表格时

Form1 frmForm1 = new Form1(new MyFormating());

Then in each form I am going to call myFormat method and pass datagridview. 然后以每种形式,我将调用myFormat方法并传递datagridview。

public partial class Form1 : Form
{
    private readonly MyFormating _myFormat;

    public Managers(MyFormating myFormat)
    {
        InitializeComponent();
        _myFormat = myFormat;
        _myFormat.FormatDGV(dgvManagers);

Leaving MyFormatting class as it is and then changing my Form1 code to below would give me same result. 保留MyFormatting类不变,然后将Form1代码更改为以下内容,将得到相同的结果。 Is this still composition? 这仍然构成吗? Should I do something like this or convention rather say to follow the way I described above? 我应该做这样的事情还是约定,而是要遵循我上面描述的方式?

When initializing new form 初始化新表格时

Managers frmManagers = new Managers();

Then in each form I am going to create new MyFormatting instance and pass datagridview to it's method 然后在每种形式中,我将创建一个新的MyFormatting实例并将datagridview传递给它的方法

public partial class Form1: Form
    {
        private readonly MyFormating _myFormat;

        public Form1()
        {
            InitializeComponent();
            myFormat = new MyFormating();
            myFormat.FormatDGV(dgvManagers);

I understand your purpose. 我明白你的目的。 "Extension" is a more practical method; “扩展”是一种更实用的方法;

public static class UIExtensions
{
    public static void FormatDGV(this DataGridView dgv)
    {
        dgv.AllowUserToAddRows = false;
        dgv.AllowUserToDeleteRows = false;
        dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        dgv.ColumnHeadersVisible = false;
        dgv.RowHeadersVisible = false;
        dgv.MultiSelect = false;
    }
}

and in your form code(onload or constructor); 并以您的表单代码(onload或构造函数);

dgvManagers.FormatDGV();

Look for Extension Methods: https://docs.microsoft.com/tr-tr/dotnet/csharp/programming-guide/classes-and-structs/extension-methods 寻找扩展方法: https : //docs.microsoft.com/tr-tr/dotnet/csharp/programming-guide/classes-and-structs/extension-methods

I suggest you make a new class which inherits from DataGridView. 我建议您创建一个继承自DataGridView的新类。 In this class just set the propertys in the constructor as you wish. 在此类中,只需根据需要在构造函数中设置属性。 Rebuild the project and your own DataGridView should show up in the Toolbox. 重建项目,您自己的DataGridView应该显示在工具箱中。 Just place it on the Form like you would do with the standard DataGridView. 只需将其放置在Form上,就像使用标准DataGridView一样。

Example for the DataGridView with predefined values: 具有预定义值的DataGridView的示例:

public class MyDataGridView: DataGridView
{
    public MyDataGridView()
    {
        AllowUserToAddRows = false;
        AllowUserToDeleteRows = false;
        AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        ColumnHeadersVisible = false;
        RowHeadersVisible = false;
        MultiSelect = false;
    }
}

And also a screenshot of the custom DataGridView in Action: 还有自定义DataGridView的屏幕截图:

工具箱中的DataGridView

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

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