|
-
Aug 15th, 2000, 01:56 PM
#1
Thread Starter
Addicted Member
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!
-
Aug 15th, 2000, 02:11 PM
#2
Fanatic Member
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!
-
Aug 15th, 2000, 02:14 PM
#3
Fanatic Member
I forgot something to say: set the style of Command1 to graphical.
-
Aug 15th, 2000, 02:14 PM
#4
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.
-
Aug 15th, 2000, 02:18 PM
#5
Hyperactive Member
Actually it works great for me as a string.
-
Aug 15th, 2000, 02:39 PM
#6
Hyperactive Member
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.
-
Aug 15th, 2000, 03:16 PM
#7
Fanatic Member
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
-
Aug 15th, 2000, 03:24 PM
#8
Hyperactive Member
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.
-
Aug 15th, 2000, 03:27 PM
#9
Monday Morning Lunatic
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
-
Aug 15th, 2000, 03:30 PM
#10
Fanatic Member
The code Forms("Form1") doesn't work. That's why I use the for...next loop!
-
Aug 15th, 2000, 03:33 PM
#11
Monday Morning Lunatic
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
-
Aug 15th, 2000, 03:38 PM
#12
Hyperactive Member
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.
-
Aug 15th, 2000, 03:40 PM
#13
Fanatic Member
My code does that!
-
Aug 15th, 2000, 03:44 PM
#14
Hyperactive Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|