Link to home
Start Free TrialLog in
Avatar of Dodsworth
Dodsworth

asked on

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
                sp.Children.Add(lctl)
            Next
            wrapPanel.Children.Add(sp)
        Next
    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.
Avatar of SStory
SStory
Flag of United States of America image

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.
Avatar of Dodsworth
Dodsworth

ASKER

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.
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.
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 ?
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial