Calling an Instance of a Custom Control

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.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
DodsworthAuthor Commented:
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.
Miguel OzSoftware EngineerCommented:
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.
DodsworthAuthor Commented:
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 ?
Miguel OzSoftware EngineerCommented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.