如何在创建表单后创建对话框? [英] How do I create a dialog after a form is created?

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

问题描述

大家好。

我是.net和windows表单的新手。我创建了一个创建主窗体的窗口,然后它应该创建一个模式对话框,显示一些信息并输入一些数据。在显示主窗体后,应该在没有任何物理事件的情况下创建对话框。我尝试在主窗体的显示事件中创建对话框,但这不是正确的方法,因为它阻止了主要窗体显示事件。在win32 api中,我应该在单独的线程中创建对话框,或者将用户消息发布到主窗体的窗口过程。我怎么能在Windows窗体中这样做?我不想使用不安全的代码。

提前谢谢

Hello guys.
I'm new to .net and windows forms. I have created a window that creates a main form and then It should create a modal dialog over it displaying some information and input some data. The dialog should be created without any physical event, just after the main form is displayed. I tried to create dialog in Shown event of main form, but this is not the right way because it blocks the main forms Shown event. In win32 api, I should simply create the dialog in a separate thread or post a user message to the window procedure of main form. How can I do this in windows forms? I don't want to use unsafe code.
Thanks in advance

推荐答案

首先,您要实现的设计是非常难看,但如果我知道你的最终设计目标,我只能建议你更好。但问题很简单;你不需要任何额外的线程,没有任何不安全的代码。你建议使用 Form.Shown 是正确的,你认为它可以阻止任何事情只是一种误解。但也许您不知道对话框是什么。



自定义对话框与表单相同,只显示使用表单.ShowDialog 。然后,在它的模态状态期间,您不能选择同一应用程序的任何其他形式。如果您确实需要访问其他表单,则应该显示。如果您已经有两种表单类型,那么我能想到的最简单的代码就是入口点方法。它看起来像这样:

First of all, the design you are trying to implement is pretty ugly, but I could only advise you something better if I knew your ultimate design goals. But the problem is simple; you don't need any additional thread, no unsafe code nothing. Your suggestion to use Form.Shown is correct, and your idea that it can block anything is just a misconception. But perhaps you don't know what a dialog is.

A custom dialog is the same as the form, only it is shown using Form.ShowDialog. Then, during the modal state of it, you cannot select any other form of the same application. If you do need to access other forms, it should be Show instead. The simplest code I could think of would be all in the entry-point method, provided you already have two form types. It can look like this:
using System;
using System.Windows.Forms;

//...

[STAThread]
static void Main() {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    var mainForm = new FormMain();
    mainForm.Shown += (sender, eventArgs) => {
        new FormDialog().ShowDialog();
    };
    Application.Run(mainForm);
}





-SA


private void Form1_Shown(object sender, EventArgs e)
       {
           Form2 foo = new Form2();
           foo.Show();
       }





这是非阻塞的,两种形式都会在运行时显示。



This is non blocking and both forms will be shown at runtime.


这篇关于如何在创建表单后创建对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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