Results 1 to 15 of 15

Thread: Can't reference control in MDI window from modal window

  1. #1

    Thread Starter
    Hyperactive Member greg_quinn's Avatar
    Join Date
    Nov 2002
    Location
    South Africa
    Posts
    366

    Can't reference control in MDI window from modal window

    This is wierd,

    I have an MDI child form created. Now when I open a modal from this child form to change some properties, I get a system.nullreference exception. Yet in the VS IDE it seems I am referencing the control correctly...

    i.e

    Dim frmChild as frmChildWindow

    frmChild.TextBox1.Text = "Hello"

    The editor sees textbox1 as being a control that exists, so why when I try to set its text value does it give ma a NullReferenceException?

    Thanks

  2. #2
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    plz post the full block of offending code, I can't tell anything from this.

  3. #3

    Thread Starter
    Hyperactive Member greg_quinn's Avatar
    Join Date
    Nov 2002
    Location
    South Africa
    Posts
    366
    Hey Cyberhawke, up and working again eh, cool.

    Attached is a basic MDI project.

    To see the problem I am having, run the app, then select

    File - New Window

    Then right click in the new window, and select properties. Now what will happen here is the child window textbox value should be updated with the value set in the properties window.

    But thats when it throws an exception.

    Thanks main
    Attached Files Attached Files

  4. #4
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Simple, you didn't instantiate your reference to the child window

    When you open the properties window, you should have a reference to the instance of the child window of the mdi form.


    When you create the properties window you need to create it like this

    VB Code:
    1. Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
    2.     Dim frmFormat As New frmProperties
    3.     Me.AddOwnedForm(frmFormat)
    4.     frmFormat.ShowDialog()
    5. End Sub
    Then in your properties window you need to set a reference to set a reference to it's owner:

    VB Code:
    1. Private frmChildWindow As frmChild  'Form level variable...
    2. Private Sub frmProperties_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.     frmChildWindow = Me.Owner
    4. End Sub
    5.  
    6. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.     frmChildWindow.TextBox1.Text = Me.TextBox1.Text
    8. End Sub

  5. #5

    Thread Starter
    Hyperactive Member greg_quinn's Avatar
    Join Date
    Nov 2002
    Location
    South Africa
    Posts
    366
    Hey Cyberhawke, you are the man!

    Thanks very much for helping me out.

    This was not so simple though. How do you get to know this stuff? I didn't see it mentioned in any MDI tutorials I did!

    Thanks again

  6. #6
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    It came to me in a dream

    Actually, the first time I looked at MDI stuff was when you asked, I have been working with OOP for so long that figuring out the appropriate properties and methods to use was just a matter of glancing at the available list. I'm just glad I could help

  7. #7
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    btw, some things to be cautious of...

    A lot of this code creates circular references, Form1 has a reference to Form2 and Form2 has a reference to Form1. VB .NET is very good about cleaning up behind itself, but in a production system it is still a good idea to do a little housekeeping when you close an object.

    When Form1 creates an instance of Form2 and shows it, and Form2 has it's reference back to Form1, when closing Form2, you should probably write something like this:

    VB Code:
    1. Private frmChildWindow As frmChild
    2. Private Sub frmProperties_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.     frmChildWindow = Me.Owner
    4. End Sub
    5. Private Sub frmProperties_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    6.     frmChildWindow = Nothing
    7. End Sub

  8. #8

    Thread Starter
    Hyperactive Member greg_quinn's Avatar
    Join Date
    Nov 2002
    Location
    South Africa
    Posts
    366
    Thanks, I'll start putting that in now.

    One last dumbaas question though,
    how do I reference a control that is in the main MDI parent window.

    I can't create a reference to the frmMain,
    i.e Dim frmParent as frmMain

    So how do I reference a control in the parent application window?

    I've tried Me.MdiParent and Me.Parent etc. but it doesn't see the controls in the main window.

    Thanks

  9. #9
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Same concept, you need to create an object reference to your main form in each of the children...

    VB Code:
    1. Public Class frmChild
    2.     Inherits System.Windows.Forms.Form
    3.     Private fMain As frmMain
    4.  
    5.     Private Sub frmChild_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         fMain = Me.MdiParent
    7.     End Sub
    8.  
    9.     Private Sub cmdCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCommit.Click
    10.         Me.txtNotes.Text = fMain.Button1.Text
    11.     End Sub
    12. End Class

  10. #10
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    HI CyberHawke,

    Pardon my ignorance but what is the difference between an Owned form and a Child form?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  11. #11
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Depends on how the form was created, an owned form is a form that was created as a non-mdi child form, but has the creating form as it's owner, while a child form is created as an mdichild form and has an mdi form as it's mdiparent

    And btw, I caught your little comment about the quick responses to spoiledkids posts, and I assure you it had nothing to do with her icon, if it even is a her...

    I just have any responses to my posts sent to my yahoo email account and messenger notifies me when I receive a response

  12. #12
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,


    To me it looks as if the practical effect and usage is the same. I can't see anything in MSDN which helps on this.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  13. #13

    Thread Starter
    Hyperactive Member greg_quinn's Avatar
    Join Date
    Nov 2002
    Location
    South Africa
    Posts
    366
    I can't do that. frmMain is the name of my main app window, but I can't declare it...

    i.e Private fMain as frmMain

    It tells me frmMain is not defined. Yet frmMain.vb is the name of the window.

    Thanks

  14. #14
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    I assure you it isn't, if you create a form as an mdichild using code in the MDI container form and set the child forms mdiparent property to the MDI container form, it doesn't have an owner, it has an mdiparent. If on the other hand you were to create these same forms and not set the mdiparent property but use rather the MDI containers AddOwnedForms method for each of the new forms, they would not be contained in the MDI container form.

  15. #15
    Hyperactive Member CyberHawke's Avatar
    Join Date
    May 2004
    Location
    Washington DC
    Posts
    477
    Originally posted by greg_quinn
    I can't do that. frmMain is the name of my main app window, but I can't declare it...

    i.e Private fMain as frmMain

    It tells me frmMain is not defined. Yet frmMain.vb is the name of the window.

    Thanks
    This did work for me, here is the code...
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width