Results 1 to 14 of 14

Thread: Read this - interesting thought really...

  1. #1

    Thread Starter
    Addicted Member Xenonic_Rob's Avatar
    Join Date
    Jun 2000
    Location
    England, UK
    Posts
    213

    Exclamation

    Hey Guys.
    I've gotta question.

    Say there was a control of a form. A command button - called Command1. I also have a text file, and I have got one line from it in a string - called MyString$. That string has the text "command1" in it. I want some way of doing something like this:

    MyString$.Backcolor = RGB(255, 0, 0)

    Except this wouldn't work, because Strings don't have backcolors. But I want it to use the text in the string (command1) and do the backcolor to that. C what I mean?
    Help me out peoples!

    Rob Wright
    E-mail: [email protected]
    Website: http://www.xenonic.com
    The First Member of Honeybee's Club
    Favourite words: Zugzwang and Empiric

  2. #2
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    Try this: (it works fine for me)
    Start a new project.
    Add two command buttons. (Let the name Command1 and Command2)
    Add this code to command2_click
    Code:
    Dim Text As String
    Text = "Command1"
    Form1.Controls(text).BackColor = vbYellow
    Run the project and enjoy the code!


  3. #3
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    I forgot something to say: set the style of Command1 to graphical.

  4. #4
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    You can't do it with a string variable, but you can do it with an Object variable:

    Dim objControl As Object
    ...
    Set objControl = Command1
    objControl.Caption = "Hi There"

    The above code should change the caption of the command button in question.
    "It's cold gin time again ..."

    Check out my website here.

  5. #5
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    Actually it works great for me as a string.
    Wade

  6. #6
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    Is there a way to extend this to read the form name from the file too?? Something like:
    Code:
    Dim Text As String
    Dim Text2 As String
    Text = "Command1"
    Text2 = "Form1"
    Forms(Text2).Controls(Text).BackColor = vbYellow
    With a little modification??

    Thanks.
    Wade

  7. #7
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    Try this: (untested)
    Create a txt file called names.txt in c:\
    In the first line put:
    Command1
    And in the second line:
    Form1
    Save the file
    Try this code:

    Code:
    Dim Text As String
    Dim Text2 As String
    Dim Line As String
    
    Open "C:\names.txt" For Input As #1
       Line Input #1, Line
       Line = RTrim(Line)
       Text = Line
       Line Input #1, Line
       Line = RTrim(Line)
       Text2 = Line
    Close #1
    
    For i = 0 To Forms.Count - 1
        If Forms(i).Name = Text2 Then
            Forms(i).Controls(Text).BackColor = vbYellow
        End If
    Next

  8. #8
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    You can iterate thru the forms or any form's controls and check the name...I'm doing that now but it's not as clean or as efficient as going straight to the form and control. What you gave initially for the control will speed me up but I don't see an equivalent for forms.
    Wade

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Just index into the Forms collection.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    The code Forms("Form1") doesn't work. That's why I use the for...next loop!

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169

    Oops

    Sorry...I just remembered that my copy of VB has a few 'additions' someone gave me, such as a more useful Forms collection. I'll be more careful in future.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    Actually, the index you mentioned works if you don't mind remembering the indices:
    Code:
    Dim sCtrlNm As String
    Dim bFrmIndx As Byte
    
    sCtrlNm = "Command1"
    bFrmIndx = 0
    Forms(bFrmIndx).Controls(sCtrlNm).BackColor = vbYellow
    But I was hoping for something where I could use the form name stored in a file instead of a form number because it would be more meaningful and easier to reference in the file itself.
    Wade

  13. #13
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715
    My code does that!

  14. #14
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    I know, and I'm currently using a for...loop that iterates forms and then the controls within them. It's just not as fast as going directly to the form and control without the loop. Like I said, what you gave for the control works great and will speed things up. Just wish that there were an equivalent for forms without going through a form loop.

    Thanks for the control code.
    Wade

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