Referencing a TextField with its Name

I’m trying make a generic LostFocus code for various series of 8 fields that are named like this:

MyField1 MyField2 MyField3 MyField4 MyField5 MyField6 MyField7 MyField

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.

You can use the controlcount.

[code]dim result as string

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]

Gee. So complicated. I was hoping there might be something like FileMaker’s Set Field By Name script step that I was missing.

From the code above just take the name and compare with the name that you looking for?

result = result + Self.Control(i).name if result = myname then //do anything you want here else end if
Other solution check by tabindex!!

if textfield.TabIndex =2 then //do anything you want here else end

There is a far better solution.

Book 2
Chapter 2
Section 16

Hmmm…looks like Control Sets make sense here. Thanks, Tim.

there are MANY alternatives

  1. control set
  2. subclass which implements the event
  3. addhandler / removehandler

each has pro’s & con’s

personally I opt for #2 and #3 but I know a lot of people use #1

[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

End Function
[/code]

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))

just did some checks to be more “robust” …

[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

return nil

End Function
[/code]

Thanks, Jean-Ives.

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.

The LR is your friend : http://documentation.xojo.com/index.php/WebView.ControlAtIndex

Perfect. Thanks, Michel. I was looking at http://documentation.xojo.com/index.php/WebPage for the answer.

Ah. Now I see. That’s a Method of WebPage. I was looking at Properties.