本文介绍了如何创建接受不同表单的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您好, 我想创建一个接受不同表单类型的函数。主要目的是防止用户从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推荐答案 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 这篇关于如何创建接受不同表单的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 15:42