如何编写执行此代码的函数

发布于 2024-10-16 11:46:45 字数 788 浏览 2 评论 0原文

我使用这段代码在 MdiWindow 中制作并显示一个表单:

        if (currentForm != null) {
            currentForm.Dispose();
        }
        currentForm = new ManageCompanies();
        currentForm.MdiParent = this;
        currentForm.Show();
        currentForm.WindowState = FormWindowState.Maximized;

我使用这段代码显示大约 20 种不同的表单...

我想编写一个这样的函数:

private void ShowForm(formClassName) {

            if (currentForm != null) {
                currentForm.Dispose();
            }
            currentForm = new formClassName();
            currentForm.MdiParent = this;
            currentForm.Show();
            currentForm.WindowState = FormWindowState.Maximized;
}

我是否必须将 formClassName 作为字符串或其他内容发送?以及如何将其包含在代码中...... 我想要最终的代码...

I've used this code to make and show a form in a MdiWindow:

        if (currentForm != null) {
            currentForm.Dispose();
        }
        currentForm = new ManageCompanies();
        currentForm.MdiParent = this;
        currentForm.Show();
        currentForm.WindowState = FormWindowState.Maximized;

I used this code to show about 20 different forms...

I want to write a such function:

private void ShowForm(formClassName) {

            if (currentForm != null) {
                currentForm.Dispose();
            }
            currentForm = new formClassName();
            currentForm.MdiParent = this;
            currentForm.Show();
            currentForm.WindowState = FormWindowState.Maximized;
}

Do I have to send formClassName as a string or something else; and how to include it in the code...
I want the final code...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴梦一场 2024-10-23 11:46:45

尝试泛型:

 public void ShowForm<FormClass>() where FormClass: Form,new() {

        if (currentForm != null) {
            currentForm.Dispose();
        }
        currentForm = new FormClass();
        currentForm.MdiParent = this;
        currentForm.Show();
        currentForm.WindowState = FormWindowState.Maximized;
}

或使用反射

public void ShowForm(string formClassName) {

        if (currentForm != null) {
            currentForm.Dispose();
        }
        currentForm = (Form) Activator.CreateInstance(Type.GetType(formClassName)) ;
        currentForm.MdiParent = this;
        currentForm.Show();
        currentForm.WindowState = FormWindowState.Maximized;
}

Try generics:

 public void ShowForm<FormClass>() where FormClass: Form,new() {

        if (currentForm != null) {
            currentForm.Dispose();
        }
        currentForm = new FormClass();
        currentForm.MdiParent = this;
        currentForm.Show();
        currentForm.WindowState = FormWindowState.Maximized;
}

Or using reflection

public void ShowForm(string formClassName) {

        if (currentForm != null) {
            currentForm.Dispose();
        }
        currentForm = (Form) Activator.CreateInstance(Type.GetType(formClassName)) ;
        currentForm.MdiParent = this;
        currentForm.Show();
        currentForm.WindowState = FormWindowState.Maximized;
}
野侃 2024-10-23 11:46:45

您还必须指定:

private void ShowForm<FormClass> where T : Form, new() {

注意那里的 new(),以便您可以默认构造 FormClass,否则它不会让您构造它。

You also have to specify:

private void ShowForm<FormClass> where T : Form, new() {

Note the new() on there so you can default construct the FormClass, otherwise it won't let you construct it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文