Results 1 to 7 of 7

Thread: [RESOLVED] Retrieving data from user control in mdi child

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Resolved [RESOLVED] Retrieving data from user control in mdi child

    Hello,

    It's been a while since I last tackled VB, so I'm kind of rusty.

    I'm adding a custom control to a child form like so:

    vb.net Code:
    1. Private Sub ShowNewForm(ByVal sender As Object, ByVal e As EventArgs) Handles NewToolStripMenuItem.Click, NewToolStripButton.Click, NewWindowToolStripMenuItem.Click
    2.         ' Create a new instance of the child form.
    3.         Dim ChildForm As New System.Windows.Forms.Form
    4.         ' Make it a child of this MDI form before showing it.
    5.         ChildForm.MdiParent = Me
    6.  
    7.         Dim XMLBook As New EditBook
    8.  
    9.         m_ChildFormNumber += 1
    10.         ChildForm.Text = "Untitled " & m_ChildFormNumber
    11.  
    12.         ChildForm.Controls.Add(XMLBook)
    13.         ChildForm.Show()
    14.     End Sub

    I need to retrieve the info that the user entered in that control (two textboxes and a listbox) and save it into a xml (I know how to do the latter).

    I just can't remember how to do it.

    Thanks.
    Last edited by tassa; Feb 23rd, 2010 at 12:02 PM.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Retrieving data from user control in mdi child

    Well, if you expose some method to get the data you're after from your usercontrol, you can loop thru the child form controls collection and test for the type of the control. If it's an EditBook, you get the data from that control using exposed methods
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Retrieving data from user control in mdi child

    Thanks for your reply.

    I have a couple of questions:
    -I loop through the collection of controls using this method: ChildForm.ActiveMdiChild.Controls.Item

    -By exposed methods do you mean shared subs or functions?

    The user control is called EditBook, and it has the controls I listed in post #1,
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Retrieving data from user control in mdi child

    A sub cannot return any data, so it out of the question. A shared method means all instances of the object will share that same method. So if your method is to return some data, unless you want all instances of the object to return the same data (which I'm pretty sure that you don't want because it doesn't work for what you described. The user inputs some data into your usercontrol, thus each time the data will be different, not the same all the time), shared methods are also out of the question. So now what you've left is functions, variables and properties. And to expose something, you have to declare it with the Public or Friend access modifier.
    Based on what you want, I suggest a single public readonly property that returns a structure or class object which contains the 3 public fields. Of course you can have 3 public readonly properties, one return the textbox1.text, one returns textbox2.text and the 3rd one returns the listbox.selecteditem.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Retrieving data from user control in mdi child

    I have these:

    vb.net Code:
    1. Public ReadOnly Property Author As String
    2.         Get
    3.             Return Me.TextBox1.Text
    4.         End Get
    5.     End Property
    6.  
    7.     Public ReadOnly Property Title As String
    8.         Get
    9.             Return Me.TextBox2.Text
    10.         End Get
    11.     End Property
    12.  
    13.     Public ReadOnly Property Notes As List(Of String)
    14.         Get
    15.             Dim noteList As New List(Of String)
    16.             For i As Integer = 0 To Me.ListBox1.Items.Count - 1
    17.                 noteList.Add(Me.ListBox1.Items.Item(i))
    18.             Next
    19.             Return noteList
    20.         End Get
    21.     End Property

    So to access them from the main form should be this?

    Dim var As String = EditBook.Author
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  6. #6
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Retrieving data from user control in mdi child

    Where are those properties declared in? Are they in the usercontrol (and they should be)? And the usercontrol is contained within the mdi child form, right? So the parent mdi form cannot access directly to the usercontrol yet. It can access to the child form only. If you want to read these properties from the mdi parent, you have to access them via the mdi child. Something like this:
    Code:
    'this code is in the mdi parent form
    Dim editBk As EditBook = Nothing
            Dim author As String = String.Empty
            Dim title As String = String.Empty
            Dim notes As List(Of String) = Nothing
            For Each child As Form In Me.MdiChildren
                For Each ctrl As Control In child.Controls
                    If TypeOf ctrl Is EditBook Then
                        editBk = DirectCast(ctrl, EditBook)
                        author = editBk.Author
                        title = editBk.Title
                        notes = editBk.Notes
                        Exit For
                    End If
                Next
            Next
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Retrieving data from user control in mdi child

    Thanks stanv. I changed the first for... next so I would only get the data of the active MDI child form. It worked great.

    THANKS!
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

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