简化 crud 按钮 [英] simplify crud button

查看:32
本文介绍了简化 crud 按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的基础表单中简化我的 CRUD 按钮.为此,我认为将所有这些转换为自定义控件是最佳做法.

hi i would like to simplify my CRUD buttons in my based form. For this, i would thinks convert all that to a custom control is the best practice.

我参考了Rocket框架,下面是他写的.他创建了一个自定义控件,该控件具有 CRUD 按钮和搜索按钮,并且 loadAll 按钮在同一行中对齐.然后当我们需要某个特定的按钮时,我们可以像我们对网站图像所做的那样,将所有图像都变成 1 个图像,从而获取该按钮的位置.这对我来说听起来不错.

I referred to the Rocket framework, and this is what he wrote as below. He create a custom control which has CRUD buttons and also search button, and loadAll button align in a same row. Then when we need the certain particular button, we can just grab the location of that button like what we did for website image where we make all the images to become 1 image. This sounds good to me.

问题:
1) 如何将此自定义控件按钮应用于我的 GUI 表单?
2) 如何简化我的 CRUD 按钮的确认消息和提示结果消息给用户?我觉得在按下保存按钮后多次在我所有的 crud 按钮上写 你确定要保存/删除吗"此记录保存成功/失败" 是多余的> 在 save() 事件之后.无论如何,我可以通过将其放入基于表单的 CRUD 自定义控件来简化它?例如,当用户按下保存"按钮时,它将运行基于表单的确认消息,并将继续运行返回数据模型的 Save() 事件并返回基于表单以获取成功保存"的结果消息.

Questions:
1) How do i apply this custom control button to my GUI form?
2) How can simplify the confirmation message and prompt result message to user for my CRUD button? i felt it redundant to write "Are you sure to save/ delete" on all my crud button many times upon pressing the save button and "This record is saved successfully / failed" after the save() event. Anyway i can simplify this by putting it to the based form CRUD custom control? E.g when user press Save button, it will run the based form confirmation message and it will continue run back the Save() event at the datamodel and back to the based form to get the Result message " successsfully save".

 public partial class AdminController : UserControl
{
    public event EventHandler AddUpdateClick;
    public event EventHandler DeleteClick;
    public event EventHandler CreateNewClick;
    public event EventHandler RefreshClick;
    public event EventHandler FilterClick;
    public event EventHandler LoadAllClick;
    public AdminController()
    {
        InitializeComponent();
    }

    private void buttonDelete_Click(object sender, EventArgs e)
    {
        if (DeleteClick != null)
            DeleteClick(sender, e);
    }

    private void buttonAddUpdate_Click(object sender, EventArgs e)
    {
        if (AddUpdateClick != null)
            AddUpdateClick(sender, e);
    }

    private void buttonCreateNew_Click(object sender, EventArgs e)
    {
        if (CreateNewClick != null)
            CreateNewClick(sender, e);
    }

    private void buttonLoadAll_Click(object sender, EventArgs e)
    {
        if (LoadAllClick != null)
            LoadAllClick(sender, e);
    }

    private void buttonFilter_Click(object sender, EventArgs e)
    {
        if (FilterClick != null)
            FilterClick(sender, e);
    }

    private void buttonRefresh_Click(object sender, EventArgs e)
    {
        if (RefreshClick != null)
            RefreshClick(sender, e);
    }
} 

请指教

推荐答案

1) 编译后,将可以从工具箱中使用用户控件.查看工具箱的最顶层组.

1) Once compiled a User Control will be available from the Toolbox. Look in the Toolbox's top most group.

2) 下面的代码调用 AdminController 的 DeleteClick 事件,如果它已经被钩住了:

2) The following code invokes the AdminController's DeleteClick event if it has been hooked:

if (DeleteClick != null)
    DeleteClick(sender, e);

要在调用删除事件之前实现确认消息,请考虑将上述代码更改为:

To implement a confirmation message before invoking the delete event consider changing the above code to:

    if (DeleteClick != null)
    {
        if (MessageBox.Show("Are you sure to save?", "Please Confirm", MessageBoxButtons.OKCancel) == DialogResult.OK)
        {
            DeleteClick(sender, e);
        }
    }

为了实现成功/失败消息,挂钩事件的委托将通过回传一个布尔值让 AdminController 知道更新成功与否.这可以使用自定义 EventArgs 类来实现

To implement the success / fail message the delegate that is hooking the event will to let AdminController know that the update was successful or not by passing back a boolean value. This can be achived using a custom EventArgs class

public class AdminControllerEventArgs : EventArgs
{
    public bool Success;

    public AdminControllerEventArgs() : base()
    {
        Success = true;
    }
}

并更改 AdminController 中事件的声明以使用 AdminControllerEventArgs:

and changing the declarations of the events in AdminController to use AdminControllerEventArgs:

public partial class AdminController : UserControl
{
    public delegate void AdminControllerEvent(object sender, AdminControllerEventArgs e);

    public event AdminControllerEvent AddUpdateClick;
    public event AdminControllerEvent DeleteClick;

那么成功的值可以测试如下:

Then the value of success can be tested as follows:

    AdminControllerEventArgs e = new AdminControllerEventArgs();

    if (DeleteClick != null)
    {
        if (MessageBox.Show("Are you sure to save?", "Please Confirm", MessageBoxButtons.OKCancel) == DialogResult.OK)
        {
            DeleteClick(sender, e);

            if (adminControllerEventArgs.Success)
            {
                MessageBox.Show("This record is saved successfully.");
            }
            else
            {
                MessageBox.Show("This record is saved failed.");
            }
        }
    }

当数据库更新失败时,连接到新事件的委托需要设置e.Success = false.

Delegates hooking to the new events will need to set e.Success = false when the database update fails.

这篇关于简化 crud 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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