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!

2 sec question for almost anyone on For...Next

Status
Not open for further replies.

34534534534555

IS-IT--Management
Jan 8, 2003
240
GB
Instead of doing all this:

txt1.Text = BoardArray(1)
txt1.ToolTipText = BoardArray(1)
txt2.Text = BoardArray(2)
txt2.ToolTipText = BoardArray(2)
txt3.Text = BoardArray(3)
txt3.ToolTipText = BoardArray(3)
txt4.Text = BoardArray(4)
txt4.ToolTipText = BoardArray(4)
txt5.Text = BoardArray(5)
txt5.ToolTipText = BoardArray(5)
txt6.Text = BoardArray(6)
txt6.ToolTipText = BoardArray(6)

I want to do:
Dim x As Integer

For x = 1 To 6
txt(x).Text = BoardArray(x)
txt(x).ToolTipText = BoardArray(x)

Next x

But it will not work, who knows why ??

TIA
 
Did you also put the textboxes in an array, or are they still called txt1...txt6 and therefore completely separated from each other ?

If so, could you tell us exactly what goes wrong ?
Rum am Morgen vorkommt Kummer und Sorgen... Cheers !

Mr. Rum
 
In the form designer, go through each control in order and set the Index property (i.e. 0 for the first control, 1 for the second control, 2 for the 3rd control, etc). Then go back and give them all the exact same name (i.e. txt). This creates your control array.

Then change your loop to start with 0 and end with the number of controls minus one.

Let me know if it works.

Chris.
 
MrRum
All the textboxes are seperate but called txt1 - txt12.Values relating to them are in an array but i didn't use a control array for other reasons.

The error is sub or function not defined.

toper0303
I did that but i have on-click events for each text box and it just messes all this up, and i cannot re-write it as they are integral to my program.

Thanks chaps, i was just trying to clean up my code it isn't program threatening ! I think i'll leave it
 

here is a serious hack and you will probably need to do a lot of modification ... but here it is...
[tt]
Private Sub Command1_Click()
Dim C As Control, Cnt As Integer, V(1 To 3) As String

V(1) = "This Is Text Box 0"
V(2) = "This Is Text Box 1"
V(3) = "This Is Text Box 2"

For Each C In Me.Controls
If TypeOf C Is TextBox Then
Cnt = Int(Right(C.Name, 1))
C.Text = V(Cnt)
C.ToolTipText = V(Cnt)
End If
Next

End Sub
[/tt]

The above example use 3 text boxes (text1, text2, text3).

Good Luck

 
OK... to simplify this...

Rename ALL of the textboxes that have similar properties... the same name... (keep SingleLines together and MultiLines...)

if Txt1,Txt2,Txt3... Are all Single line rename them Text1(0), Text1(1), Text1(2)...
when you give a textbox a name that already exist for another textbox... a dialog will popup and ask if you want to create an array... (Say Yes)... Then when you name any others the same the Index Property be will automatically assigned the next index number to it's Index property.

Now you can simplify alot of common task with textboxes...


Dim BoardArray(16) As String

'You Can Assign the array the way you want...
Private Sub Form_Load()
BoardArray(1) = "Text Box 1 is here"
BoardArray(2) = "Text Box 2 is here"
'...

For x = 0 To 1
Text1(x) = BoardArray(x + 1)
Text1(x).ToolTipText = BoardArray(x + 1)
Next
End Sub

'Use Select Case To Deal With Different Textboxes in different ways
Private Sub Text1_Change(Index As Integer)
Dim Pos1 As Integer, Len1 As Integer, Max1 As Integer
Pos1 = Text1(Index).SelStart
Len1 = Len(Text1(Index))
Select Case Index
Case 0
Max1 = 10
Case 1
Max1 = 15
End Select
If Len1 > Max1 Then Text1(Index) = Left(Text1(Index), Max1)
If Pos1 > Max1 Then Pos1 = Max1
Text1(Index).SelStart = Pos1
End Sub

Private Sub Text1_DblClick(Index As Integer)
Select Case Index
Case 0
Case 1
Beep
End Select
End Sub

'And You Can Merge Common tasks into One Sub
Private Sub Text1_GotFocus(Index As Integer)
Text1(Index).SelStart = 0
Text1(Index).SelLength = Len(Text1(Index))
End Sub
Sometimes... the BASIC things in life are the best...
cheers.gif

or at least the most fun ;-)
-Josh Stribling
 
>All the textboxes are seperate but called txt1 - txt12.Values relating to them are in an array but i didn't use
>a control array for other reasons.

>I want to do:
>Dim x As Integer
>For x = 1 To 6
> txt(x).Text = BoardArray(x)
> txt(x).ToolTipText = BoardArray(x)
>Next x


You DO REALIZE that txt(x) is trying to use a control array...
If what you are trying to do is Name them txt1,txt2,txt3...
And call them with txt(1) txt(2) txt(3)... That is Imposible
It is looking for the Control Array txt and the index (x)

If you want to name them all seperately... I suggest you use vb5prgrmr's method above.
Why do you NOT want to use a control array...
They Are VERY useful... once you get the concept down...
In the long run, Control Arrays will save you lots of time Sometimes... the BASIC things in life are the best...
cheers.gif

or at least the most fun ;-)
-Josh Stribling
 
>>In the long run, Control Arrays will save you lots of time

I agree.
It will also save the next programmer that comes along to modify the code from having to figure out what the workaround is trying to do.

My $.0000000000000002 worth.

Chris.
 
Also I would suggest something a bit more useful then just txt as a control name. Control arrays are the way to go as they not only make your code neater in many cases, but they also use less system resources.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top