Each set of 8 fields are named with that sort of pattern, where the 8th field shares the name of any of the other fields in that series, minus the final digit. I want to add a generic LostFocus event to each of the first 7 fields. I want it to update the numeric value in the 8th field based on the value found in the Me field.
I’m trying to figure out how to generically access the 8th field. I can get its Name like this:
Dim s As String = Left(Me.Name, Len(Me.Name) - 1)
But at this point, s is just a String, not a TextField. I’m not sure how to generically reference the 8th field. I tried, to no avail, the following, but got a Nil Object Exception on x on the second line below:
Dim x As TextField
x.Name = s
x.Text = Str(Val(Me.Text) + Val(x.Text))
I tried a few other ways, but obviously don’t understand how to generically reference a field when I have its Name property.
for i as integer = 0 to self.ControlCount-1
if Self.Control(i) isa textfield then
result = result + Self.Control(i).name + " "+str(Label(Self.Control(i)).Index ) + EndOfLine
end if
next
[/code]
[code]Function GetControlByName(aWindow as window, aName as String) As RectControl
dim ctrl as RectControl
dim i as Integer
For i=0 To aWindow.ControlCount-1
if aWindow.control(i) isa rectcontrol then
ctrl=RectControl( aWindow.control(i))
If ctrl.Name=aName Then
return ctrl
End If
end if
next
return nil
I think I’ll go with Jean-Ives’ GetControlByName() function and use it in a LostFocus EventHandler in a subclass (a la Norman). The Event Hander looks like this:
Dim n As String = Left(Me.Name, Len(Me.Name) - 1)
Dim t As TextField = TextField(GetControlByName(Self, n))
t.Text = Str(Val(Me.Text) + Val(t.Text))
[code]Function GetControlByName(aWindow as window, aName as String) As RectControl
dim ctrl as RectControl
if aWindow<>nil then
For i as Integer=0 To aWindow.ControlCount-1
if aWindow.control(i) isa rectcontrol then
ctrl=RectControl( aWindow.control(i))
if ctrl<>nil then
If ctrl.Name=aName Then
return ctrl
End If
end if
end if
next
end if
I’m curious about something. I’m using Jean-Ives function in a Web Project, so have to change the the first parameter to be As WebPage and have it return As WebControl. Compiler complains that Control is not a member of WebPage here:
If aWebPage.Control(i) IsA WebControl Then
ctrl = WebControl(aWebPage.Control(i))
Not sure how to convert those two lines for the Web. Using ControlID also fails.