Results 1 to 14 of 14

Thread: For... Next with Labels

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    20
    Is it posiable to do a For.. Next statement for more then 2 labels.. ex: I want to beable to get each label caption.

    Ex code: [this is what i was trying to use]

    For X = 1 To 8
    text1 = text1 + Label & X.Caption
    Next

    Label & X.Caption <~ this is where i get my problem, I've tried many things, such as, Label[X].Caption

    Can this be done?
    not all police eat doughnut's and drink coffee.. some of us like milk instead.

  2. #2
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    You just about had it on the last one, but VB uses regular parentheses, not square brackets to reference arrays.
    Try the following code (it is assumed that "MyLabel" is a control array):
    Code:
    For X = 1 To 8 
        text1 = text1 & MyLabel(X).Caption 
    Next
    P.S.: Caption is the default property of a label, so the ".Caption" can be omitted:
    Code:
    For X = 1 To 8 
        text1 = text1 & MyLabel(X)
    Next
    Let me know how you make out.
    "It's cold gin time again ..."

    Check out my website here.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    20
    I get an 'Type mismatch' error on that.. I'm using a good something ike that, but i'm trying to write the capions to an ini file.

    my real code:

    For X = 1 To 8
    Call WriteINI("Mouse", "Option[" & X & "]", Label(X), "C:\windows\desktop\vault.ini")
    Next

    ^--- this gives me the error
    not all police eat doughnut's and drink coffee.. some of us like milk instead.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    20
    can any one help?
    not all police eat doughnut's and drink coffee.. some of us like milk instead.

  5. #5
    Fanatic Member
    Join Date
    Jun 1999
    Location
    California, USA
    Posts
    662
    For X = 1 To 8
    Call WriteINI("Mouse", "Option[" & trim(str(X)) & "]", Label(X), "C:\windows\desktop\vault.ini")
    Next

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    20
    Still, it doesnt work.. Label(X) <-- this is the only part that dont work, I'm tring to do a For.. Next statement instead of writing an WriteINI line for each label.

    For X = 1 To 8
    Call WriteINI("Mouse", "Option[" & X & "]", Label(X), "C:\windows\desktop\vault.ini")
    Next

    not all police eat doughnut's and drink coffee.. some of us like milk instead.

  7. #7
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Lightbulb Declare Variable X

    Hi nopd-officer, I think you may forgot to declare the variable x.

    Code:
    Option Explicit
    Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
    
    Private Sub Form_Load()
    Dim x%
    For x = 1 To 8
    Call WriteINI("Mouse", "Option[" & x & "]", Label(x), "C:\windows\desktop\vault.ini")
    Next
    End Sub
    Private Sub WriteINI(MyAppName As String, MyKeyName As String, MyValue As String, myFileName As String)
    WritePrivateProfileString MyAppName, MyKeyName, MyValue, myFileName
    End Sub

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    20
    This still doesn't work, I get an error saying "Compile error: Sub or Function not defined" and it highlights the word Label from Label(x)
    not all police eat doughnut's and drink coffee.. some of us like milk instead.

  9. #9
    Guest
    If you are using Label(x), that means that you are using an Index property. Check and see if you have all of the Index's from 0 to X. (make sure they exsist)

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    20

    Can Anyone?? For.. Next with Labels

    I've tried all of that, still nothing. Can this be done?
    not all police eat doughnut's and drink coffee.. some of us like milk instead.

  11. #11
    Junior Member
    Join Date
    May 2000
    Location
    New Delhi, India
    Posts
    18

    Thumbs up Perhaps....

    I think that Label is a keyword in visual basic and therefore cannot be used. Try naming in lblLabel or lbl only. Perhaps it would work! Be sure that it is an array of either controls or string.
    Kazim Zaidi (the cracker)

  12. #12
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Lightbulb Check...

    Does you really have some Label control will a proper index assigned in you Form?

    [Edited by Chris on 06-04-2000 at 09:12 AM]

  13. #13
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    For Each ... Next

    Try this:
    Code:
    Private Sub Command1_Click()
        Dim oCTRL As Control
        For Each oCTRL In Me
            If TypeOf oCTRL Is Label Then
                Text1.SelText = oCTRL.Caption & vbCrLf
            End If
        Next
    End Sub
    - Aaron.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    20

    Talking

    YAY.. I got this thing working.. What I did was changed all
    the names of the labels from [Label1, Label2, ect] to
    Label, which also changed each index of each label from 0 to
    7 so now the name of each label is [Label(0), Label(1),
    ect].. Now in the For..Next section the Label(x) now works..

    Thanks everyone who help.. more !thanks! to Chris :P
    not all police eat doughnut's and drink coffee.. some of us like milk instead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width