|
-
May 10th, 2007, 02:24 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] [2005] Getting Child controls from a user control CF 2.0
Hello,
I have trying to get the controls that are on a user control. The user control has been placed on a tab. I am looping through the tabs getting the controls so I can get the values that have been input in that control.
In .Net 2.0 you can do this:
Code:
'Get the user control on this tab
uc = tp.Controls("QuickIncidentTasks")
'Get all the details for the controls contained the in user control in the current tab.
scheduledDate = uc.Controls("dtScheduledDate")
But in the CF it is getting the values is more of a problem. I have trying very hard to do this in CF.
Many thanks for any assistance,
Code:
Try
'All the controls that are on the user control
Dim taskDetails As New Windows.Forms.TextBox
Dim completionNotes As New Windows.Forms.TextBox
Dim users As New ComboBox
Dim supportType As New ComboBox
Dim status As New ComboBox
Dim scheduledDate As New Windows.Forms.DateTimePicker
Dim scheduledTime As New Windows.Forms.DateTimePicker
Dim taskID As Integer = 0
Dim uc As Control
Dim rowsAffected As Integer = 0
'Loop through all the tab and get the user control
For Each tp As TabPage In Me.tbcTasks.TabPages
'Get the user control on this tab
uc = tp.Controls(0)
'Get all the details for the controls contained the in user control in the current tab.
'Trail and error below - so a bit of a mess
'scheduledDate.Value = CDate(uc.Controls(3).Text)
'scheduledTime.Value = CDate(uc.Controls(4).Text)
taskDetails = DirectCast(uc.Controls.Item(4), TextBox)
taskDetails.Text = uc.Controls(8).ToString()
completionNotes.Text = uc.Controls(9).Text
users.Text = uc.Controls(5).ToString()
supportType.Text = uc.Controls(6).ToString()
status.Text = uc.Controls(7).ToString()
'Get the taskID number of the task on the tab
taskID = CInt(tp.Name)
Next tp
Catch ex As Exception
MsgBox(ex.ToString())
End Try
-
May 10th, 2007, 02:40 AM
#2
Re: [2005] Getting Child controls from a user control CF 2.0
Getting controls by name is dodgy at the best of times, and exposing the child controls outside a UserControl is also a bit dodgy. I strongly recommend that you declare all controls within your UserControl private and then declare properties in the UC itself to expose only those values that actually need to be used externally. For instance, if you have a TextBox on your UC then declare it private and declare a public property to expose its Text and ONLY its Text:
vb Code:
Public Property TextProperty() As String
Get
Return Me.TextBox1.Text
End Get
Set(ByVal value As String)
Me.TextBox1.Text = value
End Set
End Property
Now the calling code can get and set the Text of the TextBox but it can't do something silly like dispose the TextBox. You should generally keep visibility as narrow as possible to provide the required functionality.
-
May 10th, 2007, 03:04 AM
#3
Thread Starter
Frenzied Member
Re: [2005] Getting Child controls from a user control CF 2.0
Hello,
Just a quick question to declare the control in the user control private, i would change its modifier property to "Private"?
Another quick question. Now that I have all my user controls placed on the different tabs.
I was doing this to get the user control:
vb Code:
For Each tp As TabPage In Me.tbcTasks.TabPages
'Get the user control on this tab
uc = tp.Controls(0)
'Then finding the control and getting its value that have been input.
Next tp
However, the public properties in the user control will only be exposed through the object that was created by it.
So my problem is how do i get that object from the UC for the property that I want.
I hope you understand what I am trying to say. I.e. I have 3 tabs, so the UC on tab 1, how would i get that object.
Many thanks,
-
May 10th, 2007, 03:26 AM
#4
Re: [2005] Getting Child controls from a user control CF 2.0
I would suggest the following solution if it's feasible, which basically means if you're adding the tabs at run time.
Inherit the TabPage class. In the constructor, create and add an instance of your UC. Declare a property to expose that UC:
vb.net Code:
Public Class TabPageEx
Inherits System.Windows.Forms.TabPage
Private _uc As UserControl1
Public ReadOnly Property UC() As UserControl1
Get
Return Me._uc
End Get
End Property
Public Sub New()
Me._uc = New UserControl1
Me._uc.Dock = DockStyle.Fill
Me.Controls.Add(Me._uc)
End Sub
End Class
Now to build your TabControl you just do this:
vb.net Code:
'Add three tab pages.
Me.TabControl1.TabPages.AddRange(New TabPage() {New TabPageEx, _
New TabPageEx, _
New TabPageEx})
Now to get the text from the TextBox on the second page it's just:
vb Code:
MessageBox.Show(DirectCast(Me.TabControl1.TabPages(1), TabPageEx).UC.TextProperty)
You could even inherit the TabControl and provide a property to return the pages as TabPageEx references.
Just note that if you do choose to go this way I would expect you to use a better naming convention than my example. The only real drawback here is that you cannot add the TabPages at design time, although that may be a big drawback in certain situations.
-
May 10th, 2007, 03:38 AM
#5
Thread Starter
Frenzied Member
Re: [2005] Getting Child controls from a user control CF 2.0
Hello,
Thanks for the useful code. I never actually inherited the tab page in this project. I am not sure if I have time to redo and get this finished. That would be good when I have more time.
But one thing I am not sure about is that the code i have at the moment I get the user control from the tab using the following.
vb Code:
For Each tp As TabPage In Me.tbcTasks.TabPages
'Get the user control on this tab
uc = tp.Controls(0)
'Then finding the control and getting its value that have been input.
uc.TextProperty() 'The 'TextProperty' does not come up in intellisence
Next tp
As i have made the public property as in your earlier post, should i not be able to gain access to it?
Thanks
-
May 10th, 2007, 04:00 AM
#6
Re: [2005] Getting Child controls from a user control CF 2.0
How is your 'uc' variable declared because this:will only return a Control reference. If you've declared 'uc' as type Control then you won't get Intellisense because the Control class doesn't have a TextProperty property. If you have declared 'uc' as the correct type then you must have Option Strict turned Off or this:wouldn't compile.
I'd rather see safer code like this:
vb.net Code:
For Each page As TabPage In Me.tbcTasks.TabPages
For Each ctl As Control In page.Controls
If TypeOf ctl Is MyUserControl Then
MessageBox.Show(DirectCast(ctl, MyUserControl).TextProperty)
Exit For
End If
Next ctl
Next page
-
May 10th, 2007, 05:06 AM
#7
Thread Starter
Frenzied Member
Re: [2005] Getting Child controls from a user control CF 2.0
Hello,
Thanks for the example code you have given me.
Yes, i know my mistake i was declaring like this. Option strict turned ON
No wonder it didn't work.
My user controls is called PDATaskControl.ViewTasks
I have declared like this
Code:
dim uc as PDATaskControl.ViewTask
'Shouldn't I be able to do something like this
uc tp.controls(0)'The index should give me the user control as it is the same type
Thanks again,
-
May 10th, 2007, 05:20 AM
#8
Re: [RESOLVED] [2005] Getting Child controls from a user control CF 2.0
Like I said, this:returns a Control reference. You can't assign a Control to a PDATaskControl.ViewTasks becaise a Control reference can refer to any type of control. You know that it will refer to a PDATaskControl.ViewTasks object but the compiler doesn't. That's what casting is for: to tell the compiler that it should assume that the object referred to is of a particular type. If you know for a fact that there will be one and only on control and it will be that type then this will do the job:
vb Code:
uc = DirectCast(tp.controls(0), PDATaskControl.ViewTasks)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|