[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:
Private Sub ShowNewForm(ByVal sender As Object, ByVal e As EventArgs) Handles NewToolStripMenuItem.Click, NewToolStripButton.Click, NewWindowToolStripMenuItem.Click
' Create a new instance of the child form.
Dim ChildForm As New System.Windows.Forms.Form
' Make it a child of this MDI form before showing it.
ChildForm.MdiParent = Me
Dim XMLBook As New EditBook
m_ChildFormNumber += 1
ChildForm.Text = "Untitled " & m_ChildFormNumber
ChildForm.Controls.Add(XMLBook)
ChildForm.Show()
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.
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
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,
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.
Re: Retrieving data from user control in mdi child
I have these:
vb.net Code:
Public ReadOnly Property Author As String
Get
Return Me.TextBox1.Text
End Get
End Property
Public ReadOnly Property Title As String
Get
Return Me.TextBox2.Text
End Get
End Property
Public ReadOnly Property Notes As List(Of String)
Get
Dim noteList As New List(Of String)
For i As Integer = 0 To Me.ListBox1.Items.Count - 1
noteList.Add(Me.ListBox1.Items.Item(i))
Next
Return noteList
End Get
End Property
So to access them from the main form should be this?
Dim var As String = EditBook.Author
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
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!