|
-
Jun 6th, 2000, 07:00 PM
#1
Thread Starter
Lively Member
Hi there,
I've been looking around for examples on 'Select Case', changed around bits of code, but it hasn't helped.
Simply, what I have is a command button and a text box. When the command button is clicked, new text appears in the textbox - depending on the code in that particular part of the case statement.
What am I doing wrong?
Thank you
Private Sub Command1_Click()
Select Case Text1.Text
Case 1
Text1.Text = "aaaa"
Case 2
Text1.Text = "bbbb"
Case 3
Text1.Text = "cccc"
End Select
End Sub
-
Jun 6th, 2000, 07:11 PM
#2
Frenzied Member
There doesn't appear to be anything technically wrong with your code - you may find that this helps;
Private Sub Command1_Click()
Select Case Trim(Cstr(Text1.Text))
Case "1"
Text1.Text = "aaaa"
Case "2"
Text1.Text = "bbbb"
Case "3"
Text1.Text = "cccc"
End Select
End Sub
The Cstr command forces the text1.text to be a string - then you compare it against "1", "2", "3" - it may be just that although you had typed 1 into the text box VB internally thought of it as a string, not as a number.
The Trim function removes any spaces before or after the text1.text which would also prevent it from working.
Let me know how you get on.
'Buzby'
Visual Basic Developer
"I'm moving to Theory. Everything works there."
-
Jun 6th, 2000, 07:39 PM
#3
transcendental analytic
Christophe, that code worked without problems, maybe you want it to work in another way?
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jun 6th, 2000, 08:27 PM
#4
Thread Starter
Lively Member
Thanks for your help, Buzby and Kedaman.
Buzby,
I tried your code, and then mine again, but still it doesn't work. I'm using VB5, but I don't think that has anything to do with it.
What I'm trying to do:
When the user clicks Command1, "aaaa" appears in text1.
When they click it again, "bbbb" appears in text1.
When they click it again, "cccc" appears in text1.
Any ideas?
-
Jun 6th, 2000, 08:32 PM
#5
Frenzied Member
Aha - it all becomes clear...
So you want it to toggle round AAAA, BBBB and CCCC each time you press the button...
to do that you need to do this...
SELECT CASE Text1.Text
CASE "AAAA"
Text1.Text="BBBB"
CASE "BBBB"
Text1.Text="CCCC"
CASE ELSE
Text1.Text="AAAA"
END SELECT
Now, if the text box has "AAAA" in it it will be changed to BBBB - if it had BBBB in it it will be changed to CCCC, and if it has CCCC (or anything else in it) it will be changed to AAAA.
I think this may be what you wanted.
'Buzby'
Visual Basic Developer
"I'm moving to Theory. Everything works there."
-
Jun 6th, 2000, 08:38 PM
#6
Frenzied Member
To further explain this, a SELECT CASE statment is just a tidier version of an IF statement - the one I wrote above is the equivalent to
IF Text1.Text="AAAA" Then
Text1.Text="BBBB"
ElseIf Text1.Text="BBBB" Then
Text1.Text="CCCC"
Else
Text1.Text="AAAA"
End If
The bit after the SELECT CASE statement is evaluated by VB - then it looks for the appropriate CASE line that matches the evaluation; eg;
SELECT CASE myAge
CASE IS <5
MsgBox "Under 5"
CASE = 10
MsgBox "10 Years Old"
CASE IS >30
Msgbox "Over 30"
CASE ELSE
Msgbox "Another age!"
END SELECT
This would display "Under 5" if myAge was less than 5, "10 Years Old" if myAge=10, "Over 30" if myAge was greater than 30, and "Another Age" if myAge didn't fall into any of these categories.
Hope this helps to explain it a bit.
'Buzby'
Visual Basic Developer
"I'm moving to Theory. Everything works there."
-
Jun 6th, 2000, 08:39 PM
#7
transcendental analytic
Or:
Static i%:i=i+1
text1=choose(i,"AAAA","BBBB","CCCC")
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jun 6th, 2000, 08:44 PM
#8
Frenzied Member
Indeed!! 
I was in the 'must use a SELECT CASE' statement mindset!!
'Buzby'
Visual Basic Developer
"I'm moving to Theory. Everything works there."
-
Jun 6th, 2000, 10:34 PM
#9
Thread Starter
Lively Member
Thanks,
I've got it sorted now.
-
Jun 7th, 2000, 01:57 AM
#10
Thread Starter
Lively Member
It's me again!
I'm curious. There must be a more practical way of acheiving what I want.
SELECT CASE Text1.Text
CASE "AAAA"
Text1.Text="BBBB"
CASE "BBBB"
Text1.Text="CCCC"
etc....
So if my textbox contains "AAAA", it will change to "BBBB" and so on. That's good. But I only gave "AAAA" as an example. What if the content of the textbox is actually much longer than that?
For example:
Select Case text1.text
Case "Shakespeare lived in a village with his wife in Stratford van Avon. However, he was only there for short stretches of time. Most of the time he was in London with a bunch of other playwrights, writing thirty odd plays and hundreds of sonnets"
Text1.Text = "Occassionaly, they would discuss the Queen of the time etc etc..."
How can I have it so it is simply Case 1, Case 2 etc.
Thanks for any help!
-
Jun 7th, 2000, 03:27 AM
#11
transcendental analytic
You can put all text in an array:
' in declarations
Dim txt$(2)
' in form load
txt(0)="Blablabla"
txt(1)="Here is some more text"
txt(2)="Here comes much more"
' in commandbutton click
Static i%:if i<=2 then i=i+1
text1=txt(i)
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
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
|