简体   繁体   English

如何在不丢失数据的情况下返回上一个表格

[英]How to go back to the previous form with no loss of data

I have a first form where the user designs a hotel for himself/herself. 我有一个第一种形式,用户可以自己设计一个旅馆。 After clicking on a created room from the created floor, the user introduces information to a second form which opens from the first form in order to reserve that room. 在从创建的楼层上单击创建的房间之后,用户将信息引入第二个表单,该表单从第一个表单打开以保留该房间。 When a button is clicked on the second form the form should disappear sending the data to the first form then save it to a file in the first form. 当单击第二个表单上的按钮时,该表单应该消失,将数据发送到第一个表单,然后将其保存到第一个表单中的文件中。

My issue is that when I am trying to hide or to close the second form, it either exits the application or it just hides the tab. 我的问题是,当我尝试隐藏或关闭第二个表单时,它要么退出应用程序,要么只是隐藏选项卡。

Could anyone tell me how to close the second form an get back to the first form without losing any data, any buttons, any text or any information example in a combo box. 谁能告诉我如何关闭第二个窗体并回到第一个窗体,而又不会丢失组合框中的任何数据,任何按钮,任何文本或任何信息示例。

Code wise that is what I have in the Program.cs: 我在Program.cs中拥有的代码明智:

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

            Form1 myStart = new Form1();

            if (myStart.ShowDialog() == DialogResult.OK)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            else
            {
                Application.Exit();
            }
        }

And that is what I tried to put in the button that closes the second form, but it does not work : 那就是我试图在关闭第二个表单的按钮中放入的内容,但是它不起作用:

Form2Reservations f2 = new Form2Reservations();
            f2.Close();

and I also tried : 而且我还尝试了:

var main = new Form1();
           main.Show();
           main.BringToFront(); , but it gives me a new Form1 and I do not want to lose everything that I already had in Form1.

and I also tried this.Close(); 我也尝试过this.Close(); and this.Hide(); this.Hide(); , but the first one closes the whole application and the second one just puts both tabs on the toolbar. ,但第一个关闭整个应用程序,第二个仅将两个选项卡放在工具栏上。

Also, I am a beginner in C#, I have not taken any University level courses, I am just self-teaching myself with the help of the internet and tutorials. 另外,我是C#的初学者,我还没有参加过任何大学水平的课程,我只是借助互联网和教程自学自己。

Thank you. 谢谢。

SECOND EDIT: When I open up my second form I use this code in form 1: 第二编辑:当我打开第二个表单时,我在表单1中使用此代码:

Form2Reservations f2 = new Form2Reservations();
                f2.receiveDataFromForm1(typeOfRoom, numberOfTheRoom, floor);
                f2.ShowDialog();
                this.Hide();

The edited changeRoom_Click looks like this : 编辑后的changeRoom_Click如下所示:

private void changeRoomButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

And the Program.cs code with static void Main() still looks the same. 带有静态void Main()的Program.cs代码看起来仍然相同。

My idea is the following : Is there any chance that I am not opening the second form correctly, so when I want to close the second form, it closes both of them, because somehow they are closely connected and this.Close() is referring to both forms ? 我的想法如下:是否有可能我没有正确打开第二个窗体,所以当我想关闭第二个窗体时,它会关闭两个窗体,因为它们以某种方式紧密连接并且this.Close()引用两种形式? Is that possible ? 那可能吗 ? Would my mistake be in the first form when I call the opening of the second form ? 当我调用第二个表格的开头时,我的错误是否会出现在第一个表格中?

You are checking if the DialogResult is set on 'OK'. 您正在检查DialogResult是否设置为“ OK”。 But you aren't setting the DialogResult before Closing the form, which is why it will return False on the If statement and exits the application. 但是,您没有在关闭表单之前设置DialogResult,这就是为什么它将在If语句上返回False并退出应用程序的原因。

You need to add this.DialogResult = System.Windows.Forms.DialogResult.OK; 您需要添加this.DialogResult = System.Windows.Forms.DialogResult.OK; before closing the Form in changeRoomButton_Click . changeRoomButton_Click关闭表单changeRoomButton_Click

--EDIT: - 编辑:

Lets give it another try. 让我们再试一次。

You are calling receiveDataFromForm1 before you actually have showed the form, therefor it probably isn't going to return anything. 在实际显示表单之前,您正在调用receiveDataFromForm1 ,因此它可能不会返回任何内容。 Also, you don't need the hide the existing form when calling ShowDialog as it will stay on top until you close it. 另外,在调用ShowDialog时,您不需要隐藏现有表单,因为它将一直位于顶部,直到您将其关闭。

Your code is a real mess so I'll just throw something together as an example to help you progress; 您的代码真是一团糟,因此我将一些示例结合起来以帮助您进行改进;

In Program.cs : Program.cs

        frmReservations reservations = new frmReservations();
        if (reservations.ShowDialog() == DialogResult.OK)
        {
            string result = reservations.yourDesiredResult;
            Application.Run(new frmMain(result));
        }
        else
        {
            Application.Exit();
        }

Now, inside frmReservations : 现在,在frmReservations内部:

public partial class frmReservations : Form
{
    public string yourDesiredResult = string.Empty;
    private void btnClose_Click(object sender, EventArgs e)
    {
        this.DialogResult = System.Windows.Forms.DialogResult.OK;
        this.yourDesiredResult = "I've been set!";
        this.Close();
    }
}

Now inside frmMain you will have to receive that value again we've received from frmReservations and set in Program.cs : 现在在frmMain您将不得不再次从frmReservations接收并在Program.cs设置的该值:

public partial class frmMain : Form
{
      public frmMain(string resultFromReservations)
      {
           InitializeComponent();
           //resultFromReservations holds the string: I've been set
      }
}

This is just an example to show you one of the many ways to do this, you will now have to implement it inside your program. 这只是一个示例,向您展示了执行此操作的许多方法之一,现在您必须在程序中实现它。

I found my bug. 我发现了我的错误。 It was in Program.cs : I had : 它在Program.cs中:我有:

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

            Form1 myStart = new Form1();
            Form2Reservations myLogin = new Form2Reservations();

            if (myStart.ShowDialog() == DialogResult.OK)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(myLogin);
            }
            else
            {
                Application.Exit(); // here was the mistake
            }
        }

All I had to do is to change Application.Exit(); 我要做的就是更改Application.Exit(); to Application.Run(myStart); Application.Run(myStart); , but I also needed to include this line in my second form button that closes the second form : ,但我还需要在第二个表单按钮中包含此行,以关闭第二个表单:

this.DialogResult = System.Windows.Forms.DialogResult.OK;

Thank you guys for help. 谢谢你们的帮助。 I could not have fixed it without your help. 没有您的帮助,我无法修复它。

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

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