Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

event handler (No Control Arrays?) 3

Status
Not open for further replies.

cyberbiker

Programmer
Mar 16, 2001
431
US
New to VB.net and am becoming confused with event handlers.
I have looked through the posts for a bit and realize that control arrays do not exist in VB.net.

The question I have is that I do not seem to quite understand how to use the event handler to handle something like this (from VB6)

Private sub cmdButton_Click(Index as integer)
select case index
case 1
' call a sub and do something
case 2
'call a sub and do something totally different
case 3
'open something
case 4
'close something
case else
'display error message
end select
If I understand correctly I would not be able to do this in VB.net.
Instead I would either need to put the applicable code in the event handler (in this case the click event) of each control. or call a sub from each event handler.

perhaps something like this

Private sub FakeArray()
dim ctl as control
for each ctl in controls
select case ctl.tag
case "1"
etc, etc, etc

I am wondering if I am misunderstanding this somehow.
Basically, How should I handle something like the first select in VB.net?

And I have looked at the compatability controls. They look good for upgrades, but I have learned from bad experience to write code based on the "newest" and would not want to use them for new development

So far, VB.net seems to tower over VB6 and I can live without control arrays in return for the other benefits of vb.net, but I want to be certain that I understand what I am reading.

If I am not clear let me kow and I will try again.




Terry (cyberbiker)
 
Code:
Dim Txt(2) As TextBox
Txt(0) = New TextBox()
Txt(0).Name = "Txt(0)"
Txt(1) = New TextBox()
Txt(1).Name = "Txt(1)"
AddHandler Txt(0).TextChanged, AddressOf MyHandler
AddHandler Txt(1).TextChanged, AddressOf MyHandler
--------------------------------
Private Sub MyHandler(ByVal sender As Object, ByVal e As System.EventArgs)
     Dim t As TextBox = ctype(sender, TextBox)
     Select Case Sender.Name
          Case "Txt(0)"
               'Do Something to Txt(0)
          Case "Txt(1)"
               'Do Something to Txt(1)
     End Select
End Sub

 
BTW, the code that I wrote up there is an array of controls.
 
Define your controls with names that end in a number at the end, with a preceding non-numeric character e.g. txtPath0 and txtPath1. Start with 0. This number is your Index. Define an array of controls for each simulated Control Array using the same base name i.e. no number.

Write an "Initialize" Sub. Call it from the end of the "InitializeComponent" Sub.
MyInitialize()
End Sub
' Define array on controls
Private txtPath(1) As TextBox

Private Sub MyInitialize()
txtPath(0) = txtPath0
txtPath(1) = txtPath1
End Sub

Use the IDE to generate ONE handler, for each array on controls. You can change the name to remove the number e.g.
txtPath0_Changed to txtPath_Changed and add Event names for all the other controls in the same array of controls to the Handles clause.

Private Sub txtPath_TextChanged(ByVal eventSender _
As System.Object, _
ByVal eventArgs As System.EventArgs) _
Handles txtPath0.TextChanged, _
txtPath1.TextChanged
' Get Index from Sender.Name
Dim Index As Integer = GetIndex(CType(eventSender, Control))
'''' Other Code
End Sub

Put this in a Public module.
Code:
Friend Function GetIndex(ByVal sender As Control) As Integer
        Dim intIndex As Integer
        Dim I As Integer
        Dim strName As String
        Dim strW As String
        strName = sender.name
        For I = strName.Length To 1 Step -1
            strW = Mid(strName, I, 1)
            If Not IsNumeric(strW) Then Exit For
            intIndex += 10 * intIndex + CInt(strW)
        Next
        Return intIndex
    End Function

Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
Thanks to both of you.

Your explanations have cleared up some of my confusion. (I will, of course, create more as I experiment further but that will be a different issue and thread)

I think I now understand the sender argument better.

I have saved this into my "SaveMyRear" directory for future reference.

Terry (cyberbiker)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top