Question How can I refer to another sub??

saichong

Member
Joined
Nov 21, 2009
Messages
10
Programming Experience
Beginner
I have a program that used information form treenodes and put them in the textbox after timer ticks
the problem is that i should use AddHandler for ticking, but i dont know how to refer to the title() and name() because they are not on the same sub private

note, this code does not have the full used codes
VB.NET:
Private Sub myevents(ByVal sender As System.Object, ByVal e As System.EventArgs)
             Dim i As Integer = 0
             Dim title(i) As TextBox
             title(i) = New TextBox
             Dim name(i) As TextBox
             name(i) = New TextBox
             Dim timer(i) As Timer
             timer(i) = New Timer
while i < 5
                    title(i) = New TextBox
                    name(i) = New TextBox  
                    title(i).Text = titlestree.Nodes(i).Text
                    name(i).Text = nametree.Nodes(i).Text
                    timer(i) = New Timer
                    AddHandler timer(i).Tick, AddressOf aftertick
                    timer(i).Interval = 10000 * (i + 1)
                    timer(i).Enabled = True
                    i=i+1
End While

here I want to refer to the title and name from the above sub , what should I write??

VB.NET:
  Private Sub aftertick(ByVal sender As System.Object, ByVal e As System.EventArgs)
'how can i do that?
[COLOR="Red"]textbox1.text=title(i).text
textbox2.text=name(i).text[/COLOR]
    End Sub
 
If you need to refer to variables in more than one method then you have to declare them outside of all methods. That way they can be seen from everywhere.

If you are using concurrent arrays then, in the Tick event handler, you can get the index of the Timer that Ticked:
VB.NET:
Dim index As Integer = Array.IndexOf(timers, sender)
The 'sender' is the object that raised the event, i.e. the Timer that Ticked. You can then use that index to get the corresponding elements from other arrays.
 
Back
Top