I am making one application in which there is one main Form FormMain and one helper Form FormHelper.

For understanding, take an example that in FormMain the user is typing in the orders and frequently the user wants to open the FormHelper and fill some values in it.

When the order is saved, the values in FormMain is saved as well as FormHelper.

What I did was make the FormHelper a field in the definition of FormMain :

public partial class FormMain : Form
{
    FormHelper BillsForm;
}

And, in the constructor :

 public FormReceiptNew(string ReceiptNo)
    {
        InitializeComponent();
        BillsForm = new BillsForm();//just once
    }

Now suppose that the user, while filling the FormMain, wants to enter some values in FormHelper, he/she can press ALT+H and the Form will be shown using ShowDialog() and when it's done, the user will close the FormHelper, and the same process will happen for as many times the FormHelper is required.

The reason why I want the same Form to open multiple times, is that the user is filling certain values in it and I want to persist the values the next time the FormHelper is shown again and when the user is done completely with the FormMain, the values in the FormMain will be saved along with the values in FormHelper.So when the user presses ALT+H each time, the following code will not work:

BillsForm= new FormHelper();
BillsForm.ShowDialog(); 

as it is creating a new form and all the old values will be deleted.

有帮助吗?

解决方案

In your main form, store a reference to the helper form. You instantiate the helper form once (onLoad for example) and keep calling showDialog() on that same object. All the fields should be retained between calls.

其他提示

If you were to store those values in the main form after closing, you could send those values back to the FormHelper upon creation either through the constructor or setters. From your question it already sounds like you're going to be storing those values that you want to persist inside the main form, so setting them upon creation shouldn't be an issue.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top