如何创建接受不同表单的函数 [英] How do I create a function that accepts different Forms

查看:91
本文介绍了如何创建接受不同表单的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我想创建一个接受不同表单类型的函数。主要目的是防止用户从MdiParent打开时打开多个表单(相同的表单)。相反,如果它尚未打开,我想打开。否则,请将表格保存下来。

目前我所做的是:

Hello,

I want to create a function that accept different form types. Main purpose is to prevent openning multiple forms (same forms) when user open from MdiParent. Instead, I would like to open if it was not opened yet. Else, bring the form alive.
Currently what I have done is:

private FrmUser l_FrmUser;
private FrmRolePermission l_FrmRolePermission;
private void usersToolStripMenuItem_Click(object sender, EventArgs e)
{
    if (l_FrmUser == null || l_FrmUser.IsDisposed == true)
    {
        l_FrmUser = new FrmUser();
    }
    l_FrmUser.MdiParent = this;
    l_FrmUser.Show();
    if (l_FrmUser.WindowState == FormWindowState.Minimized)
        l_FrmUser.WindowState = FormWindowState.Normal;
}
private void rolePermissionToolStripMenuItem1_Click(object sender, EventArgs e)
{
    if (l_FrmRolePermission == null || l_FrmRolePermission.IsDisposed == true)
    {
        l_FrmRolePermission = new FrmRolePermission();
    }
    l_FrmRolePermission.MdiParent = this;
    l_FrmRolePermission.Show();
    if (l_FrmRolePermission.WindowState == FormWindowState.Minimized)
        l_FrmRolePermission.WindowState = FormWindowState.Normal;
}



这只是一个有两种形式的例子。我也有其他形式,你可以看到有很多重复的代码。为此,我想创建一个通用函数,如:




This is just an example with two forms. I have other forms too and as you can see there are lots of duplicate codes. For this, I would like to create a common function like:

private FrmUser l_FrmUser;
private FrmRolePermission l_FrmRolePermission;
private void rolePermissionToolStripMenuItem1_Click(object sender, EventArgs e)
{
    OpenForm(l_FrmRolePermission);
}
private void usersToolStripMenuItem_Click(object sender, EventArgs e)
{
    OpenForm(l_FrmUser);
}

private void OpenForm(Form aForm)
{
    if (aForm == null || aForm.IsDisposed == true)
    {
        aForm = new Form(); //<-- Problem here! How to initialize to desired form?
    }
    aForm.MdiParent = this;
    aForm.Show();

    if (aForm.WindowState == FormWindowState.Minimized)
        aForm.WindowState = FormWindowState.Normal;
}





OpenForm(..)函数中的注释描述了问题。我不知道如何将Form类转换为FrmUser或FrmRolePermission。



或者还有其他优雅方法可以达到此目的吗?



请咨询。



感谢先进,

tslin



Problem is described with the comment inside OpenForm(..) function. I don't know how to convert Form class to FrmUser or FrmRolePermission.

Or is there any other elegant methods to serve this purpose?

Please advice.

Thanks in advanced,
tslin

推荐答案

在我回复您的问题之前代码,我想问你是否知道或已经使用了MenuStrip的特殊'MdiWindowListItem属性,该属性导致Mdi应用程序自动跟踪所有打开的Mdi子窗体,并将任何Child-Form映射到您点击的子项目,并将其带到前面?



如果您对此不熟悉,请参阅:[< a href =http://msdn.microsoft.com/en-us/library/ms171654%28v=vs.90%29.aspxtarget =_ blanktitle =New Window> ^ ] 。



可能没有你想要的功能;它可能/可能不容易修改以具有你想要的功能。



如果你描述你想要什么而不是那个自动会有所帮助窗口管理设施提供。



其他信息我认为必须知道:



1.你愿意吗?永远创建任何类型的儿童表格的多个实例。



2.曾经是一种儿童的实例 - 表单已创建,您是否允许用户关闭它,而不仅仅是隐藏它?



3.您是否总是创建至少一个每种类型的实例儿童形式?



你的代码中的跳出来(第二个例子):



1.在'OpenForm中,你设置Child-Form实例的MdiParent属性作为参数传递,无论参数是'null还是现有实例:如果它是现有实例,那么你不是需要设置它再次使用MdiParent属性,您可以使用aForm.Visible = true使其可见;
Before I respond to your question with code, I'd like to ask you if you are aware of, or have used, the special 'MdiWindowListItem property of the MenuStrip that causes the Mdi App to automatically keep track of all open Mdi Child-Forms, and brings-to-front whichever Child-Form is mapped to the sub-item you clicked on ?

If you are not familiar with this, see: [^].

That may not have the functionality you want; it may/may-not be easily modified to have the functionality you want.

It would be helpful if you described what you want in contrast with what that "automatic" window management facility provides.

Other information I think is essential to know:

1. will you ever create more than one instance of any of your types of Child-Forms.

2. once an instance of one type of Child-Form is created, do you allow the user to 'Close it, rather than just 'Hide it ?

3. do you always create at least one instance of each type of Child-Form ?

This "jumps out at me" in your code (second example):

1. in 'OpenForm you are setting the MdiParent property of the instance of the Child-Form passed as a parameter whether or not the parameter is 'null or is an existing instance: if it's an existing instance, then you don't need to set its 'MdiParent property again, and you can just make it visible using aForm.Visible = true;


您是否正在寻找以下内容:



Are you looking for something like the following:

Public Function FnInitiliseForm(ByVal ChildForm As Form, ByVal title As String) As Form
        For Each ChildForm1 As Form In Me.MdiChildren
            If ChildForm1.Name = ChildForm.Name Then
                ChildForm1.Activate()
                ChildForm1.WindowState = FormWindowState.Normal
                ChildForm.Close()
                FnInitiliseForm = ChildForm1
                Return FnInitiliseForm
            End If
        Next

        ChildForm.FormBorderStyle = Windows.Forms.FormBorderStyle.Fixed3D
        ChildForm.StartPosition = FormStartPosition.Manual
        ChildForm.MaximizeBox = False
        ChildForm.WindowState = FormWindowState.Maximized

        ChildForm.MdiParent = Me
        ChildForm.Text = title

        ChildForm.KeyPreview = True
        ChildForm.Show()
        ChildForm.Left = 0
        ChildForm.Top = 0

        FnInitiliseForm = ChildForm
    End Function


这篇关于如何创建接受不同表单的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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