简体   繁体   English

如何从另一个表格打开一个新表格

[英]How to open a new form from another form

I have form which is opened using ShowDialog Method.我有使用 ShowDialog 方法打开的表单。 In this form i have a Button called More.在这种形式中,我有一个名为“更多”的按钮。 If we click on More it should open another form and it should close the current form.如果我们点击更多,它应该打开另一个表单并关闭当前表单。

on More Button's Click event Handler i have written the following code在 More Button 的 Click 事件处理程序上,我编写了以下代码

MoreActions objUI = new MoreActions (); 
objUI.ShowDialog();
this.Close();

But what is happening is, it's not closing the first form.但正在发生的事情是,它并没有关闭第一个表单。 So, i modified this code to所以,我将这段代码修改为

MoreActions objUI = new MoreActions (); 
objUI.Show();
this.Close();

Here, The second form is getting displayed and within seconds both the forms getting closed.在这里,第二个表格正在显示,并且在几秒钟内 forms 都关闭了。

Can anybody please help me to fix issue.谁能帮我解决问题。 What i need to do is, If we click on More Button, it should open another form and close the first form.我需要做的是,如果我们点击更多按钮,它应该打开另一个表单并关闭第一个表单。

Any kind of help will be really helpful to me.任何形式的帮助都会对我很有帮助。

In my opinion the main form should be responsible for opening both child form.在我看来,主窗体应该负责打开两个子窗体。 Here is some pseudo that explains what I would do:这是一些伪代码,解释了我会做什么:

// MainForm
private ChildForm childForm;
private MoreForm moreForm;

ButtonThatOpenTheFirstChildForm_Click()
{
    childForm = CreateTheChildForm();
    childForm.MoreClick += More_Click;
    childForm.Show();
}

More_Click()
{
    childForm.Close();
    moreForm = new MoreForm();
    moreForm.Show();
}

You will just need to create a simple event MoreClick in the first child.您只需要在第一个子项中创建一个简单的事件 MoreClick。 The main benefit of this approach is that you can replicate it as needed and you can very easily model some sort of basic workflow.这种方法的主要好处是您可以根据需要复制它,并且您可以非常轻松地对某种基本工作流程进行建模。

If I got you right, are you trying like this?如果我猜对了,你会这样尝试吗?

替代文字

into this?成这个?
替代文字

in your Form1 , add this event in your button:在您的Form1 中,将此事件添加到您的按钮中:

    // button event in your Form1
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.ShowDialog(); // Shows Form2
    }

then, in your Form2 add also this event in your button:然后,在您的Form2 中,在您的按钮中也添加此事件:

    // button event in your Form2
    private void button1_Click(object sender, EventArgs e)
    {
        Form3 f3 = new Form3(); // Instantiate a Form3 object.
        f3.Show(); // Show Form3 and
        this.Close(); // closes the Form2 instance.
    }

ok so I used this:好的,所以我用了这个:

public partial class Form1 : Form
{
    private void Button_Click(object sender, EventArgs e)
    {
        Form2 myForm = new Form2();
        this.Hide();
        myForm.ShowDialog();
        this.Close();
    }
}

This seems to be working fine but the first form is just hidden and it can still generate events.这似乎工作正常,但第一种形式只是隐藏,它仍然可以生成事件。 the "this.Close()" is needed to close the first form but if you still want your form to run (and not act like a launcher) you MUST replace it with需要“this.Close()”来关闭第一个表单,但如果您仍然希望表单运行(而不像启动器一样),则必须将其替换为

this.Show();

Best of luck!祝你好运!

I would use a value that gets set when more button get pushed closed the first dialog and then have the original form test the value and then display the the there dialog.我会使用一个值,当更多按钮被按下时关闭第一个对话框,然后让原始表单测试该值,然后显示那里的对话框。

For the Ex对于前任

  1. Create three windows froms从创建三个窗口
  2. Form1 Form2 Form3表格 1 表格 2 表格 3
  3. Add One button to Form1向 Form1 添加一个按钮
  4. Add Two buttons to form2将两个按钮添加到 form2

Form 1 Code表格 1 代码

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private bool DrawText = false;

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.ShowDialog();
        if (f2.ShowMoreActions)
        {
            Form3 f3 = new Form3();
            f3.ShowDialog();
        }

    }

Form2 code表格2代码

 public partial class Form2 : Form
 {
        public Form2()
        {
            InitializeComponent();
        }

        public bool ShowMoreActions = false;
        private void button1_Click(object sender, EventArgs e)
        {
            ShowMoreActions = true;
            this.Close();
        }


        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

Leave form3 as is保持 form3 不变

Try this..尝试这个..

//button1 will be clicked to open a new form
private void button1_Click(object sender, EventArgs e)
{
    this.Visible = false;     // this = is the current form
    SignUp s = new SignUp();  //SignUp is the name of  my other form
    s.Visible = true;
}

you may consider this example你可以考虑这个例子

//Form1 Window
//EventHandler
Form1 frm2 = new Form1();
{
    frm2.Show(this); //this will show Form2
    frm1.Hide();  //this Form will hide
}
private void Button1_Click(object sender, EventArgs e)
{
    NewForm newForm = new NewForm();    //Create the New Form Object
    this.Hide();    //Hide the Old Form
    newForm.ShowDialog();    //Show the New Form
    this.Close();    //Close the Old Form
}

For example, you have a Button named as Button1 .例如,你有一个Button命名为Button1 First click on it it will open the EventHandler of that Button2 to call another Form you should write the following code to your Button.首先单击它,它将打开该Button2EventHandler以调用另一个Form您应该将以下代码写入您的 Button。

your name example=form2.

form2 obj=new form2();

obj.show();

To close form1, write the following code:要关闭 form1,请编写以下代码:

form1.visible=false; or form1.Hide();form1.Hide();

You could try adding a bool so the algorithm would know when the button was activated.您可以尝试添加一个布尔值,以便算法知道按钮何时被激活。 When it's clicked, the bool checks true, the new form shows and the last gets closed.单击它时,布尔值检查为真,显示新表单并关闭最后一个表单。

It's important to know that forms consume some ram (at least a little bit), so it's a good idea to close those you're not gonna use, instead of just hiding it.重要的是要知道表单会消耗一些内存(至少一点点),所以最好关闭那些你不会使用的,而不是仅仅隐藏它。 Makes the difference in big projects.在大项目中有所作为。

You need to control the opening of sub forms from a main form.您需要控制从主窗体打开子窗体。

In my case I'm opening a Login window first before I launch my form1.在我的情况下,我在启动我的 form1 之前首先打开一个登录窗口。 I control everything from Program.cs.我从 Program.cs 控制一切。 Set up a validation flag in Program.cs.在 Program.cs 中设置验证标志。 Open Login window from Program.cs.从 Program.cs 打开登录窗口。 Control then goes to login window.控制然后进入登录窗口。 Then if the validation is good, set the validation flag to true from the login window.然后,如果验证良好,则从登录窗口将验证标志设置为 true。 Now you can safely close the login window.现在您可以安全地关闭登录窗口。 Control returns to Program.cs.控制权返回到 Program.cs。 If the validation flag is true, open form1.如果验证标志为真,则打开 form1。 If the validation flag is false, your application will close.如果验证标志为 false,您的应用程序将关闭。

In Program.cs:在 Program.cs 中:

   static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// 

        //Validation flag
        public static bool ValidLogin = false;

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            Application.Run(new Login());

            if (ValidLogin)
            {
                Application.Run(new Form1());
            }
        }

    }

In Login.cs:在 Login.cs 中:

       private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtUsername.Text == "x" && txtPassword.Text == "x")
            {
                Program.ValidLogin = true;
                this.Close();
            }
            else
            {
                MessageBox.Show("Username or Password are incorrect.");
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

使用 this.Hide() 而不是 this.Close()

Do this to Program.cs对 Program.cs 执行此操作

using System;

namespace ProjectName 
{
    public class Program
    {
        [STAThread]
        public static void Main(string[] args) 
        {
            Application.EnableVisualStyles();
            Application.SetDefaultCompatibleTextRendering(false);

            new Form1().Show();

            Application.Run();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM