Dotnet的代表 [英] Delegates in Dotnet

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

问题描述

先生/女士。



按钮采用MDI形式,文本字段为儿童形成。如果我们单击按钮,文本字段数据将使用多播代理插入数据库。这可能吗?

如果可能的话请给出解决这个问题的方法。



谢谢你..

hi sir / madam.

The button is in MDI form and the text fields in child form. If we are click the button the text fields data will insert into database using Multicast Delegate. Is this possible?
if possible please give the way to solve this.

thank u..

推荐答案

首先,请阅读我对该问题的评论。



是的,可以使用合并代表(多播代表)甚至将数据写入数据库。

请阅读这篇文章:如何:组合代理(多播代理)(C#编程指南) [ ^ ]
First of all, please read my comment to the question.

Yes, it is possible to use combine delegates (multicast delegates) even to write data to the database.
Please, read this article: How to: Combine Delegates (Multicast Delegates)(C# Programming Guide)[^]


这里不需要使用多播代理。假设您在MDI父表单的top = level菜单上有两个子菜单项:获取数据:



GetData

   活跃儿童表格

   所有儿童表格



并且,您有一个名为的表格'MDIChildDataForm上有三个TextBox。



'MDIChildDataForm有两个简单的修改:一个私有List< string>,以及一个返回List< string>的公共方法;:
There's no need to use multi-cast delegates here. Let's say you have two sub-menu items on the top=level menu of your MDI Parent Form: "Get Data:"

GetData
   Active Child Form
   All Child Forms

And, you have a Form named 'MDIChildDataForm which has three TextBoxes on it.

The 'MDIChildDataForm has two simple modifications: a private List<string>, and a public method that returns a List<string>:
private List<string> textList = new List<string>();

public List<string> GetData()
{
    textList.Clear();
    textList.Add(textBox1.Text);
    textList.Add(textBox2.Text);
    textList.Add(textBox3.Text);
    return textList;
}



在运行时,您可以根据需要在MDI父窗体中添加与MDIChildren一样多的'MDIChildDataForm实例:在此示例中,它们将被添加为使用'文件顶级菜单下的'新菜单项:


At run-time you add as many 'MDIChildDataForm instances as MDIChildren to the MDI Parent Form as you like: in this example they are being added by using the 'New menu item, under the 'File top-level menu:

private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
    MDIChildDataForm f1 = new MDIChildDataForm();
    f1.TopLevel = false;
    f1.MdiParent = this;
    f1.Show();
}

现在,当你想在MDIChildForms上获取数据时,这就是你需要的所有代码:

Now, when you want to "get the data," on the MDIChildForms, this is all the code you need:

// holds text data in three TextBoxes on the active MDIChildForm
List<string> ActiveMDIChildData = new List<string>();

// List of Lists: each List holds text data in three TextBoxes on one of the MDIChildForms
List<list><string>> AllMDIChildData = new List<list><string>>();

// Click EventHandler for GetData/Active Child Form
private void getActiveFormData_Click(object sender, EventArgs e)
{
    ActiveMDIChildData.Clear();

    // no active mdichild form : quit
    if (this.ActiveMdiChild == null) return;

    // note: this assumes every MDIChildForm here is of MDIChildDataForm
    // note the need to cast the ActiveMdiChild to the Type of the MDIChildDataForm
    ActiveMDIChildData = (this.ActiveMdiChild as MDIChildDataForm).GetData();
}

// Click EventHandler for GetData/All Child Forms
private void getAllFormsData_Click(object sender, EventArgs e)
{
    AllMDIChildData = new List<list><string>>();

    // no mdichild forms : quit
    if (this.MdiChildren.Length == 0) return;

    foreach (Form theForm in this.MdiChildren)
    {
        // note: this assumes every MDIChildForm here is of MDIChildDataForm
        // note the need to cast the ActiveMdiChild to the Type of the MDIChildDataForm
        AllMDIChildData.Add((theForm as MDIChildDataForm).GetData());
    }
}

此时,我确信您只需解析/迭代上述列表之一就可以将数据写入数据库。在实践中,您可能希望每个(运行时)创建的MDIChildControl都具有唯一标识符,并且还可以使用它。



注意:



虽然您可以直接将控件直接放在MDI表格的显示矩形中,但在任何MDI子表格上方都可以看到这一点:这不是一个好主意,因为用户可能正在移动子窗体,扩展它们以填充MDI显示rectamg; e区域:它看起来容易混淆。最好通过菜单项或ToolStrip中的按钮激活数据收集:我个人会选择菜单,因为,imho,工具条在MDI中显得怪异。



我们在这里真的需要更多信息:你是否会在MDI表单中有多个子表单,其中只有一些将包含你需要收集的数据?您是否需要收集所有有效的chlld表单中的所有文本数据,或者仅收集当前有效的ACTIVE子表单中的数据?



假设其中一个您的有效子表单有一堆文本字段,有些是空的:或者某些包含特定字段不可接受的内容:是否需要验证该子表单上的所有字段,并采取一些行动......就像没有写出数据库中的内容...如果子表单不完整,或者文本字段内容格式不正确,还是可以接受?



最后:有充分的理由,MS已经弃用了MDI,我相信,你可以使用MDI来实现更好看的实现。但是,如果你要使用MDI:嗯,好吧:)

At this point, I am sure you'd have no trouble writing the data out to your database by just parsing/iterating one of the above Lists. In practice, you'd probably want each of your (run-time) created MDIChildControls to have a unique identifier, and also make use of that.

Notes:

While you can "get away" with having a Control placed directly in the display rect of an MDI Form that will always be visible above any MDI child-for: this generally is not a good idea because the user may be moving around the child Forms, expanding them to fill the MDI display rectamg;e area : it's liable to look confusing. Best to activate data collection via a menu item, or a button in a ToolStrip: personally I'd go with a menu, since, imho, toolstrips look "weird" in MDI.

We really need some more information here: are you going to have multiple child Forms inside the MDI Form, only some of which are going to contain data you need to gather? Are you going to need to gather all the text-data inside ALL the valid chlld Forms, or only the data inside the current valid ACTIVE child Form ?

Assuming one of your valid child Forms has a bunch of text-fields, and some are empty: or some contain content that is not acceptable for a particular field: is it the case that you need to validate all the fields on that child Form, and take some action ... like not writing what's there to the database ... if the child Form is "incomplete," or text-field content is not in the right format, or acceptable ?

Finally: for good reason, MDI is "deprecated" by MS, and you could, I believe, have a better looking implementation not using MDI. But, if you gotta use MDI: well, okay :)


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

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