如何在c#中使用多个表单。 [英] How to use multiple forms in c#.

查看:112
本文介绍了如何在c#中使用多个表单。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包含两个表单的项目但是这里的问题我对使用多个表单的知识最少,所以请帮助我...........我的项目将是像这样首先来到form1,询问你的名字,然后在输入名字后按下按钮,第二个出现,第一个关闭..........









提前致谢..........





问候,

Ahsan Naveed

I want to create a project that contains two forms in it but here is the problem i do not have the least knowledge of using multiple forms so please help me...........my project would be like this first comes form1 which ask your name and then after the name is entered and a button is pressed the second from comes up and the first closes..........




Thanks in advance..........


Regards,
Ahsan Naveed

推荐答案

不要关闭第一种形式 - 当你这样做时,它会关闭应用程序。



最简单的方法是隐藏当前表单然后显示新表单:



Don't close the first form - when you do that, it closes the application.

The easiest way to do it is to Hide the current form then show teh new one:

MySecondForm f = new MySecondForm();
Hide();
f.ShowDialog();
Close();

这会隐藏当前表单,显示新表单,然后在新表单关闭时关闭主表单(以及应用程序)。

This hides the current form, displays the new one, and then closes the main form (and thus the application) when the new form closes.


检查这个

http: //msdn.microsoft.com/en-us/library/aa984329(v=VS.71).aspx [ ^ ]


我将非常回答你的问题从字面上看,假设您想要隐藏其主窗体启动WinForms应用程序,并在用户输入名称时显示InitialForm,然后,当用户关闭InitialForm时,您希望您的PrimaryForm(应用程序)变得可见,并捕获在InitialForm上输入的信息,因此您可以在您的应用程序(PrimaryForm)中使用它。



因此,InitialForm代码是这样的:保证你有一个TextBox和一个Button,以及按钮Click Click事件:
I'm going to answer your question very "literally," assuming that you want to start a WinForms application with its "Primary Form" hidden, and an InitialForm displayed where the user enters a name, and then, when the user closes the InitialForm, you want your PrimaryForm (the Application) to become visible, and to have "captured" the information entered on the InitialForm, so you can use it in your Application (the PrimaryForm).

So, the InitialForm code is something like this: assuming you have a TextBox, and a Button, on it, and the Button Click Event wired-up:
public partial class InitialForm : Form
{
    public InitialForm() { InitializeComponent(); }

    // this 'exposes a string to the 'outside
    public string UserEnteredText { get; private set; }

    // see below for why you need to implement this
    //private void InitialForm_FormClosing(object sender, FormClosingEventArgs e)
    //{
        //UserEnteredText = textBox1.Text;
    //}

    private void button1_Click(object sender, EventArgs e)
    {
        UserEnteredText = textBox1.Text;

        Close();
    }
}

因此,PrimaryForm(您启动应用程序)包含:

So, the PrimaryForm (that you start your Application with) contains:

// this code would not work in the Form Load Event
private void PrimaryForm_Shown(object sender, EventArgs e)
{
    Visible = false;

    InitialForm iform = new InitialForm();

    iform.FormClosing += iform_FormClosing;

    iform.Show();
}

// this will contain the text the user entered on the InitialForm
private string UserEnteredText;

// note that this could be called by either the user pressing
// the Button on the InitialiForm, or by closing the InitialForm
// by clicking its 'CloseBox
private void iform_FormClosing(object sender, FormClosingEventArgs e)
{
    // note that we have to cast to 'InitialForm here
    // to access its public property
    UserEnteredText = (sender as InitialForm).UserEnteredText;
    
    // show the Primary Form
    Show();

    // for testing only
    MessageBox.Show("The user entered: " + UserEnteredText);
}

但是,等一下:如果InitialForm有一个CloseBox,并且用户关闭它会发生什么? InitialForm 的公共字符串属性永远不会被设置:所以你需要将一个FormClosing EventHandler添加到InitialForm,然后连接它(如上面注释掉的InitialForm代码所示) :

But, wait: what happened if the InitialForm had a CloseBox, and the user closed it ? The public string property of the InitialForm would never have been set: so you need to add a FormClosing EventHandler to the InitialForm, and "wire it up" (as shown in the commented out code above for InitialForm):

private void InitialForm_FormClosing(object sender, FormClosingEventArgs e)
{
    UserEnteredText = textBox1.Text;
}

考虑使用此处显示的技术的好处和缺点:



1.上行:InitialForm被处理掉:你永远不会引用它。您不必在InitialForm中编写自定义事件,并在主窗体中订阅它。你不需要(表扬)使用WinForm棘手的财产变更通知。你不需要做任何莫代尔。



2.缺点:你没有保留InitialForm可能重复使用:这可能是(? )。在InitialForm中编写自定义事件,由您在PrimaryForm中订阅的Button Click事件触发,可能会产生更干净,更易理解,可维护的代码。



最后:通过简单地使InitialForm没有CloseBox,你可以消除这里显示的花式舞蹈用户关闭InitialForm的情况,以防止你从其TextBox获取信息。

Consider the upside, and downside, of using the technique shown here:

1. upside: InitialForm is disposed of: you never "kept a reference to it." You didn't have to write a custom Event in the InitialForm, and subscribe to it in the Primary Form. You didn't need (praise be) to muck around with WinForm's tricky Property Change notification. You didn't need to do anything "modal."

2. downside: You don't keep InitialForm around for possible re-use: and that's likely (?). Writing a custom Event in InitialForm, triggered by the Button Click Event, that you subscribed to in the PrimaryForm, might well result in "cleaner," more understandable, maintainable code.

Finally: by simply making InitialForm have no CloseBox, you can eliminate the fancy-dancing shown here the handle the case of the user closing InitialForm in a way that prevents you from "getting" the information from its TextBox.


这篇关于如何在c#中使用多个表单。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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