• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1321
  • Last Modified:

C# New File Dialog

I have created a new file diaolog and I want the add button to create a new child MDI form on my main form (form1).  While I have the code to create the MDI child, I can't figure out how to reference the Parent form from the addNewFile form.
0
tsmolskow
Asked:
tsmolskow
  • 7
  • 7
  • 3
1 Solution
 
TDSnetCommented:
Assuming that you mean the addNewFile dialog is actually a Windows Form you have built then you could do the following:

1. Before opening the addNewFile form, add it as an OwnedForm of the Parent.
  ie:
    Me.AddOwnedForm(frmAddNewFile) - Where 'Me' is the Parent form
    frmAddNewFile.ShowDialog()

2. Then you can reference the Parent form from the addNewFile form as it's Owner.
  ie.
    Me.Owner - Where 'Me' is the addNewFile form

Not sure if I totally followed but hopefully that's what you were looking for.
0
 
tsmolskowAuthor Commented:
Where would I add that, I tried in the parent form (Form1) cs file, but the intellisense doesn't list the AddOwnedForm method.
0
 
TDSnetCommented:
Sorry, didn't realise you were using C#.  Instead of using 'Me' you need to use 'this'.  If that doesn't work you'll need to tell me what version of VS.NET you are using.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
tsmolskowAuthor Commented:
OK, now I see it, so in the parent form I have this:

             Form newFileDialog = new addNewFormDialog();
            this.AddOwnedForm(newFileDialog);
            newFileDialog.ShowDialog();

In the child I have this

  this.Owner

Now I should be able to reference controls on the parent form from the dialog form?
0
 
TDSnetCommented:
Yes because using 'this.Owner' from the child will return your Parent form.

So, if your Parent form was called frmParent (for example) you could say something like:

  frmParent parent = this.Owner;
0
 
AGBrownCommented:
Note that you will only be able to access fields (including controls), methods and properties on one form from a different form that are declared as either internal or public, not private or protected. This applies both to accessing parent form controls from the child form, and vice versa.

I don't know how far down the road you are with this, but you mentioned that you are possibly doing a dialog form. I don't usually find it appropriate to set an MdiParent for a dialog form, and in fact for a form that isn't an MdiParent that isn't suitable. For dialog forms you can use the "this.ParentForm;" property of the dialog form to get to the parent form instead of this.Owner. I do something like:

      private void cmdShowDialog_Click(object sender, System.EventArgs e)
      {
            string strField1;
            string strField2;
            frmMyDialogForm dialog = new frmMyDialogForm();
            if (frmMyDialogForm.ShowDialog() == DialogResult.OK)
            {
                  strField1 = frmMyDialogForm.txtATextBox.Text;
                  strField2 = frmCityState.txtState.Text;
            }
            frmMyDialogForm.Dispose();
      }

Within the dialog form itself, you need to set the form's DialogResult property. This can either be done with:
      private void cmdOK_Click(object sender, System.EventArgs e)
      {
            ((ParentFormClassType)this.ParentForm).SomePropertyOrControl.DoSomething();
            DialogResult = DialogResult.OK;
      }
or in the IDE through the DialogResult property in the properties window. Setting the DialogResult will cause the form to "close". I've also shown a line which uses the ParentForm property in that button click event handler.

The archetypal reference for .NET dialog winforms is probably something like:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconDialogBoxes.asp

Andy
0
 
AGBrownCommented:
:-) note appalling mistake during translation from my winforms project:
               strField1 = frmMyDialogForm.txtATextBox.Text;
               strField2 = frmCityState.txtState.Text;
should read
               strField1 = frmMyDialogForm.txtATextBox.Text;
               strField2 = frmMyDialogForm.txtAnotherTextBox.Text;
0
 
AGBrownCommented:
:$
And the final horrible mistake (sorry TDSNet) is that in fact it is this.Owner and not this.ParentForm that you need. And to access it you need to do:
frmMyDialogForm.ShowDialog(this) instead of frmMyDialogForm.ShowDialog():

An example would then be a master form (Form1) written as follows:
- with a TextBox called txtToConfirm
- a label called lblConfirmed that shows the result of the dialog box
- a button called cmdConfirm
- a property to expose the value of the textbox that you want to get at on the child form
- an event handler for the cmdConfirm.Click event

      //      Property to get the textbox's text
        internal string TextToConfirm
        {
            get
            {
                return this.txtToConfirm.Text;
            }
        }
       
        private void cmdConfirm_Click(object sender, EventArgs e)
        {
            ConfirmDialog dlg = new ConfirmDialog();
            switch (dlg.ShowDialog(this))
            {
                case DialogResult.Yes:
                    this.lblConfirmed.Text = "value confirmed";
                    break;
                case DialogResult.No:
                    this.lblConfirmed.Text = "value incorrect";
                    break;
                case DialogResult.Cancel:
                    this.lblConfirmed.Text = "";
                    break;
                default:
                    this.lblConfirmed.Text = "unhandled dialog result";
                    break;
            }
            dlg.Dispose();
        }

And the dialog form (ConfirmDialog) would have:
- a label to show the value being confirmed called lblTextToConfirm
- three buttons, each with their text and DialogResult set through the property designer to Yes, No, Cancel accordingly:
- one event handler for the load event
        private void ConfirmDialog_Load(object sender, EventArgs e)
        {
            this.lblTextToConfirm.Text = ((Form1)this.ParentForm).TextToConfirm;
        }
       
       
Sorry for all the mistakes, I don't think I've ever made such a hash of a simple comment *skulks back out of the room*.

Andy
0
 
tsmolskowAuthor Commented:
OK, well I have modified the code on my parentform to match your code example - so it looks like this:

             string strField1;
 
            Form newFileDialog = new addNewFormDialog();
            this.AddOwnedForm(newFileDialog);
            if (newFileDialog.ShowDialog() == DialogResult.OK)
            {
                strField1 = newFileDialog.textBox1.text;                
            }
            newFileDialog.Dispose();

But compiling throws this error:

'System.Windows.Forms.Form' does not contain a definition for 'textBox1'      

But textBox1 is a control on the dialog form (addNewFormDialog)  -  so what am I still doing wrong?
0
 
AGBrownCommented:
you have the variable newFileDialog declared as a System.Windows.Forms.Form. You need it declared as the type of form that the dialog is. E.g. if your dialog form is declared as public addNewFormDialog : System.Windows.Forms.Form, then the code should read:

            addNewFormDialog newFileDialog = new addNewFormDialog(); // <-------------- note difference in declaration here
            //this.AddOwnedForm(newFileDialog); // <------------------------- removed this line
            if (newFileDialog.ShowDialog(this) == DialogResult.OK) // <---------------- altered ShowDialog call
            {
                strField1 = newFileDialog.textBox1.text;                
            }
            newFileDialog.Dispose();

I am assuming that your dialog form is called addNewFormDialog, and that that isn't a method name that creates a dialog form?

Andy
0
 
tsmolskowAuthor Commented:
We're close, very close, the scenario is this, the parent form (frmMFWS) is the MDI container, the user chooses File | New | File, this click calls to the dialog form (addNewFormDialog) which presents a VS 2005 type New File template dialog, with a text box for the new file name (textBox1).  When the user choses a file template, and fills in the name, a new new child MDI tab is created in frmMFWS with the name chosen in the textBox1 input box of the addNewFormDialog form.
0
 
AGBrownCommented:
Great. Is that last change working? I'm guessing you might now be getting a different error message which says that textBox1 is not accessible from frmMFWS. If that is the case, then try putting a property into addNewFormDialog like this:
        internal string NewMdiTabName
        {
            get
            {
                return this.textBox1.Text;
            }
        }
and changint the strField1 assignment in the frmMFWS to:
            ... other code
            if (newFileDialog.ShowDialog(this) == DialogResult.OK) // <---------------- altered ShowDialog call
            {
                strField1 = newFileDialog.NewMdiTabName;                
            }
            ... other code

Does that work?

Andy
0
 
tsmolskowAuthor Commented:
OK, let me try that last comment - one moment...
0
 
tsmolskowAuthor Commented:
OK, well that compiles, but when I run the dialog form and choose the file template, and choose the add button, nothing happens, no new MDI child form is created (where it was before just hard coding from the File | New | File event).
0
 
AGBrownCommented:
Do you have the Add button's DialogResult property assigned to DialogResult.OK? You would do this through the properties tab in the designer for the form by clicking on the button, right click and select Properties. Then set the DialogResult.

Also are you then adding your new MDI child in the parent form with something like:
            addNewFormDialog newFileDialog = new addNewFormDialog();
            //this.AddOwnedForm(newFileDialog);
            if (newFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                strField1 = newFileDialog.NewMdiTabName;
            }
            newFileDialog.Dispose();
            //      Add new MDI child here
0
 
tsmolskowAuthor Commented:
OK, I did change the property, and also, I had the MDI child code inside the if statement block, and the good news - IT WORKS!!!  You sir, are a SUPER GENIUS!
0
 
AGBrownCommented:
I think the last fix was changing the DialogResult property of the button, not moving the MDI code. You should put your MDI child code back inside the if statement, otherwise it will also add a child when the dialog is cancelled (that will happen if you click the Close cross button in the top right, and not just if you have explicitly set a DialogResult.Cancel button on the dialog form).

It did take me a while to get going. I'm glad it worked in the end.

Andy
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 7
  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now