Calling an Instance of a Custom Control

Posted on 2014-08-18
Last Modified: 2014-09-18
I have this piece of code that creates a sentence using CustomControls...

    Private Sub buildSentence(sentence As String)
        Dim words As String() = sentence.Split(New Char() {" "c})
        For Each word In words
            Dim sp As New StackPanel
            sp.Margin = New Thickness(0, 0, 16, 0)
            sp.Orientation = Controls.Orientation.Horizontal
            sp.HorizontalAlignment = System.Windows.HorizontalAlignment.Center
            Dim letters As Char() = word.ToCharArray
            For Each letter In letters
                Dim lctl As New LetterCustomControl
                lctl.Text = letter
    End Sub

Open in new window

The CustomControl shows a single letter and has some animations built into it.  So the code above splits a sentence into words and then creates a CustomControl for each letter in a word and adds it to a Horizontal StackPanel, so that complete words wrap correctly in a WrapPanel.

The animations were triggered from mouse/touch events in the custom control, but now I need to trigger them through code outside the CustomControl.

So if the original sentence was "Hello World", I need to be able to get a handle to say, letter(3) in the sentence and trigger the animation for the CustomControl that represents that letter.

Hope that makes (some) sense.
Question by:Dodsworth
    LVL 25

    Expert Comment

    I would assume
    sp.children(2) would be letter #3

    As to triggering something we'd need more details on the control.  However, if you can call the event and pass it (nothing, nothing) that will fake the call, probably.
    LVL 1

    Author Comment

    Forget about how to trigger the event.. I can do that fine once I have a reference to the correct control.

    'sp' is garbage after each word is rendered, so I can use it.
    LVL 35

    Expert Comment

    by:Miguel Oz
    A quick code to reference the correct control could be:
    wrapPanel.Children(0).Children(2) would be to access (word #1).(letter #3)

    because the wrapPanel instance has StackPanel instances and each StackPanel instance has LetterCustomControl instances.

    Note: For your final implementation you may consider having a GetAssociateLetterCustomControl function in your custom control to provide some encapsulation to avoid accessing the Children property directly.
    LVL 1

    Author Comment

    I had to break you idea down into two casts..

    Dim stack As StackPanel = TryCast(wrapPanel.Children(1), StackPanel)
    Dim lctl As LetterCustomControl = TryCast(stack.Children(1), LetterCustomControl)

    Open in new window

    I'm assuming that the compiler wouldn't have known that the contents of the wrapPanel where StackPanels.

    How would I go about writing GetAssociateLetterCustomControl ?
    LVL 35

    Accepted Solution

    I was thinking  on adding a method like:
    Private Function GetAssociateLetterCustomControl(ByVal stackIndex As Integer, ByVal letterIndex As Integer) As LetterCustomControl 
            Dim stack As StackPanel = TryCast(wrapPanel.Children(1), StackPanel)
    	Return TryCast(stack.Children(1), LetterCustomControl)
    End Function

    Open in new window

    Just to avoid repeating the same construct all over the place in case you may require to do this in more than once.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now