Results 1 to 8 of 8

Thread: [RESOLVED] [2005] Getting Child controls from a user control CF 2.0

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Resolved [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
    steve

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

    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:
    1. Public Property TextProperty() As String
    2.     Get
    3.         Return Me.TextBox1.Text
    4.     End Get
    5.     Set(ByVal value As String)
    6.         Me.TextBox1.Text = value
    7.     End Set
    8. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    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:
    1. For Each tp As TabPage In Me.tbcTasks.TabPages
    2.                 'Get the user control on this tab
    3.                 uc = tp.Controls(0)
    4.                 'Then finding the control and getting its value that have been input.
    5. 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,
    steve

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Public Class TabPageEx
    2.     Inherits System.Windows.Forms.TabPage
    3.  
    4.     Private _uc As UserControl1
    5.  
    6.     Public ReadOnly Property UC() As UserControl1
    7.         Get
    8.             Return Me._uc
    9.         End Get
    10.     End Property
    11.  
    12.     Public Sub New()
    13.         Me._uc = New UserControl1
    14.         Me._uc.Dock = DockStyle.Fill
    15.         Me.Controls.Add(Me._uc)
    16.     End Sub
    17.  
    18. End Class
    Now to build your TabControl you just do this:
    vb.net Code:
    1. 'Add three tab pages.
    2. Me.TabControl1.TabPages.AddRange(New TabPage() {New TabPageEx, _
    3.                                                 New TabPageEx, _
    4.                                                 New TabPageEx})
    Now to get the text from the TextBox on the second page it's just:
    vb Code:
    1. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    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:
    1. For Each tp As TabPage In Me.tbcTasks.TabPages
    2.                       'Get the user control on this tab
    3.                       uc = tp.Controls(0)
    4.                       'Then finding the control and getting its value that have been input.
    5.                       uc.TextProperty() 'The 'TextProperty' does not come up in intellisence
    6.       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
    steve

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Getting Child controls from a user control CF 2.0

    How is your 'uc' variable declared because this:
    vb.net Code:
    1. tp.Controls(0)
    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:
    vb.net Code:
    1. uc = tp.Controls(0)
    wouldn't compile.

    I'd rather see safer code like this:
    vb.net Code:
    1. For Each page As TabPage In Me.tbcTasks.TabPages
    2.     For Each ctl As Control In page.Controls
    3.         If TypeOf ctl Is MyUserControl Then
    4.             MessageBox.Show(DirectCast(ctl, MyUserControl).TextProperty)
    5.             Exit For
    6.         End If
    7.     Next ctl
    8. Next page
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    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

    Code:
    dim uc as control
    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,
    steve

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] [2005] Getting Child controls from a user control CF 2.0

    Like I said, this:
    vb Code:
    1. tp.controls(0)
    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:
    1. uc = DirectCast(tp.controls(0), PDATaskControl.ViewTasks)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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