
Access VB Control via Variables?
Ok, I figured it out. Here's a working version.
Now you only have to call SCV or GCV.
(short for SetControlValue and GetControlValue).
Old Method: label(i) = farray(i)
New Method: SCV "label" & i, farray(i)
I CAN LIVE WITH IT.
OK, I'm Back to working with VB.Net
Ed
=================================================================
Private Sub Form_Load()
Rem SIMPLE TEST. CREATE ONE FORM WITH THESE 3 LABELS,
Rem PASTE ALL THIS CODE IN IT, AND RUN IT !
Rem LABEL1, LABEL2, LABEL3
Rem NOTE: DON'T MAKE THEM CONTROL ARRAYS. USE THE DEFAULT NAMES.
Dim fieldname As String
Dim farray(3) As String
farray(1) = "Ed"
farray(2) = "is"
farray(3) = "here"
For i = 1 To 3
SCV "label" & CStr ( i ), farray( i )
Next i
End Sub
========== YOU NEVER HAVE TO LOOK AT THESE FUNCTIONS AGAIN =========
AFTER YOU ADD THEM TO YOUR CODE
==================================================================
Public Sub SCV(fieldname As String, svalue As String)
Dim myControl As Control, ctrlname As String
Set myControl = GetControlFromName(fieldname)
myControl = svalue
End Sub
Public Sub GCV(fieldname As String, svalue As String)
Dim myControl As Control, ctrlname As String
Set myControl = GetControlFromName(fieldname)
svalue = myControl
End Sub
Public Function GetControlFromName(ByVal ControlName As String) As Control
Dim tempControl As Control
For Each tempControl In Me.Controls
If UCase(tempControl.Name) = UCase(ControlName) Then
Set GetControlFromName = tempControl
End If
Next
End Function
=================================================================
Quote:
>Hi,
>VB.net doesn't support Control Arrays.
>Does anyone have a more efficient method for accessing
>controls, than the (New VB Method shown below) which uses Select
>Statements.
>Something like creating the name of the control in a variable
>and then using the variable to access the field.
>example:
> dim controlname as string, fa(10) as string
> for i = 1 to 10
> controlname = "screencontrol_" & cstr(i)
> fa(i) = Me.controlname
> next i
>Thanks,
>Ed
>************************************************
>Old VB Method:
> dim fa(10) as string
> for i = 1 to 10
> fa(i) = screencontrol(i)
> next i
>************************************************
>New VB.Net Method:
> dim fa(10) as string
> for i = 1 to 10
> select case i
> case 1
> fa(i) = screencontrol_1
> case 2
> fa(i) = screencontrol_2
> case 3
> fa(i) = screencontrol_3
> ...
> end select
> next i
>************************************************