[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Find Custom Controls By Name

Posted on 2014-01-03
4
Medium Priority
?
287 Views
Last Modified: 2014-01-03
So I have created a simple custom control that is made up of a number of labels.  I have given the control some properties so I can assign values to those labels.  I am filling a datatable from SQL and now I want to loop through that datatable and set values to the labels of my custom controls from this dt.  I have done this kind of thing in the past quite easily, however, I can't seem to figure this out with the custom control.

Here is my code for the custom control:
Public Class ctClock
    
    Private myColor As Color
    Private txtMold As String
    Private txtTool As String
    Private txtJCIPart As String
    Private txtCustPart As String
    Private txtOrder As String
    Private txtNumbers As String
    Private txtStatus As String
    Private txtName As String

    Property wholecolor() As Color
        Get
            Return myColor
        End Get
        Set(ByVal value As Color)
            myColor = value
            Me.BackColor = myColor
        End Set
    End Property

    Property Mold() As String
        Get
            Return txtMold
        End Get
        Set(ByVal value As String)
            txtMold = value
            lblMold.Text = txtMold
        End Set
    End Property
    Property Tool() As String
        Get
            Return txtTool
        End Get
        Set(ByVal value As String)
            txtTool = value
            lblTool.Text = txtTool
        End Set
    End Property

    Property JCIPart() As String
        Get
            Return txtJCIPart
        End Get
        Set(ByVal value As String)
            txtJCIPart = value
            lblJCIPart.Text = txtJCIPart
        End Set
    End Property

    Property CustPart() As String
        Get
            Return txtCustPart
        End Get
        Set(ByVal value As String)
            txtCustPart = value
            lblCustPart.Text = txtCustPart
        End Set
    End Property


    Property Order() As String
        Get
            Return txtOrder
        End Get
        Set(ByVal value As String)
            txtOrder = value
            lblOrder.Text = txtOrder
        End Set
    End Property

    Property Numbers() As String
        Get
            Return txtNumbers
        End Get
        Set(ByVal value As String)
            txtNumbers = value
            lblNumbers.Text = txtNumbers
        End Set
    End Property

    Property Status() As String
        Get
            Return txtStatus
        End Get
        Set(ByVal value As String)
            txtStatus = value
            lblStatus.Text = txtStatus
        End Set
    End Property

End Class

Open in new window


And here is what I am trying to do to assign values to my properties:

 Dim i As Integer = 1
        For Each row As DataRow In dt.Rows
            Dim controlName As String
            controlName = "CtClock" & i
            Me.Controls(controlName).Mold = (row("Press").ToString)
            Me.Controls(controlName).Tool = (row("Tool").ToString)
            i = i + 1
        Next row

Open in new window


I have never done a custom control (as you can probably tell from my example - ripped right from the MSDN site).  Thanks for any help you can give me on this.
0
Comment
Question by:G Scott
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39753741
What you have should work ok, as long as your control isn't sat inside another container. Are you getting any errors from your code?
0
 
LVL 1

Author Comment

by:G Scott
ID: 39753859
My intellisense does this:
Error
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 39753870
Ah, OK. So it has found it, but you need to cast it to the correct type:
DirectCast(Me.Controls(controlName), ctClock).Mold = row("Press").ToString()

Open in new window

Or, to be a bit more safe:
Dim obj As ctClock = DirectCast(Me.Controls(controlName), ctClock)
If Not obj Is Nothing Then
    obj.Mold = row("Press").ToString()
End If

Open in new window

0
 
LVL 1

Author Closing Comment

by:G Scott
ID: 39753896
Thank you, Carl.  Your safer method worked perfectly!!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question