Results 1 to 3 of 3

Thread: How to reference a control on an MDI child form

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    How to reference a control on an MDI child form

    I want to set the contents of a control on a form from within another form. The problem is the target control is on a form that is a child form on an MDI parent form and I don't know the format for doing that. I do know the format for hitting a control on the parent form:

    Code:
    My.Forms.MDIMainParentForm.CommentTextBox.Text = "abcde"
    This works as far as putting "abcde" in CommentTextBox if it is on MDIMainParentForm. What I want to do is put "abcde" in a text box called CommentTextBox on a form called StockItemsForm which I place on MDIMainParentForm as a child form. I'm sure this is simple. I just don't know the format and can't find it in searching.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: How to reference a control on an MDI child form

    While it can be done, it's really not good practice to be modifying controls on other forms directly. Only the form that owns the control should be modifying a control. If another form wants to prompt that change then the "proper" way to go about it depends on the relationship between the forms. If a parent form wanted to affect a child form, the parent should have a reference to the child and call a method on that reference, passing any required data as arguments. Inside the method, the callee then modifies its own control(s) using that data. If a child form wanted to affect its parent form then it should raise an event that the parent handles. In the event handler, the parent modifies its own control. If one child wanted to affect another child then the first child would raise an event that the parent would handle and then the parent would call a method on the other child. That's best practice.

    If you want to do it the quick and dirty way then you can. The code you posted would only work if the MDI parent form was the default instance of its type. Basically the same code will work if the MDI child form is the default instance of its type, e.g.
    vb.net Code:
    1. My.Forms.MDIChildForm.CommentTextBox.Text = "abcde"
    If it's not default instance, i.e. if you created the child form explicitly with the New keyword, then you can't affect it via the default instance, which is a different object. You need to get a reference to the object you created. If you didn't keep a reference to that object explicitly, e.g. in a field in the parent form, then you can access it via MdiChildren collection of the parent, e.g.
    vb.net Code:
    1. Dim childForm = parentForm.MdiChildren.OfType(Of MDIChildForm)().SingleOrDefault()
    2.  
    3. If childForm IsNot Nothing Then
    4.     'Use childForm here.
    5. End If
    SingleOrDefault will obviously not work if there can ever be more than one instance of that type of for at the same time. In that case, you'd have to use something about the instances to differentiate them and get the one you want. I can't tell you what that would be because it depends on your app.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: How to reference a control on an MDI child form

    It is not a default instance. So I tried the 2nd code. And it works! So, thank you very much.

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