[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 115
  • Last Modified:

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.
0
Dodsworth
Asked:
Dodsworth
  • 2
  • 2
1 Solution
 
SStoryCommented:
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.
0
 
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.
0
 
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.
0
 
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 ?
0
 
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.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now