Shoutbox

N00bish VB question - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: N00bish VB question (/showthread.php?tid=26710)

N00bish VB question by Anubis on 06-04-2004 at 02:51 PM

I'm very new to Visual Basic and I'm making a program that displays text after text in a label when you click a button...but the only code I know is how to make a text in a label change once...like this;

code:
Private Sub Command1_Click()
Label1.Caption = "First Text"
End Sub

Could someone tell me the code to make the text change once again when the button's pressed again...
RE: N00bish VB question by Stigmata on 06-04-2004 at 02:58 PM

my gay way

code:
Private Sub Command1_Click()
if Label1.Caption = "First Text" then
Label1.Caption = "2nd Text"
elseif Label1.Caption = "2nd Text" then
Label1.Caption = "First Text"

End Sub

RE: N00bish VB question by Anubis on 06-04-2004 at 03:01 PM

What about adding like 20 bits of text...which I need to do for this ap
PS. That code seems to be invalid when I try and play it I get an error


RE: N00bish VB question by Stigmata on 06-04-2004 at 03:03 PM

:S

that method...but loads of it


RE: N00bish VB question by Anubis on 06-04-2004 at 03:05 PM

quote:
Originally posted by jackass_wanabe
that method...but loads of it
You sure that's there's no more direct method?
RE: N00bish VB question by Choli on 06-04-2004 at 03:10 PM

declare a global variable (outside any function or sub you put the declaration)

code:
Dim times as long
in the load sub of the form, make sure you initializa that variable to the proper number, 0 for example)
code:
private sub form1_load()
  ' more code
   times=0
end sub

and in the button click sub, you put this code:
code:
private sub button1_click()
   times=times+1'the user clicked the button once more
   select case times
         case 1 ' 1st time the button is clicked
                label1.caption="1st text"
         case 2 ' 2nd time the button is clicked
                label1.caption="2nd text"
         case 3 ' 3rd time the button is clicked
                label1.caption="3rd text"
          ' etc....
         case else ' all the other cases (ie: from the last click you have coded)
                 label1.caption="you've clicked the button more times than what I was expected and didn't coded any other text to be displayed .p"
     end select
end sub



there are other ways to do that too. If the texts to be displayed are always the same, you can create an array of text and use the times variable to access the proper text (label1.caption=label1_texts[times])
RE: N00bish VB question by Sk3tch on 06-04-2004 at 03:10 PM

quote:
Originally posted by Anubis
quote:
Originally posted by jackass_wanabe
that method...but loads of it
You sure that's there's no more direct method?
Yes.. use the case method :P
quote:
Originally posted by Anubis
PS. That code seems to be invalid when I try and play it I get an error
Make sure all the control names are the same.. ect..
RE: N00bish VB question by Anubis on 06-04-2004 at 03:12 PM

quote:
Originally posted by Sk3tch
Yes.. use the case method
case method? What's that?
RE: N00bish VB question by Choli on 06-04-2004 at 03:16 PM

quote:
Originally posted by Anubis
quote:
Originally posted by Sk3tch
Yes.. use the case method
case method? What's that?
what I've just posted ;):P
code:
select case -----
    case -----
    case -----
    case -----
    case else
end select


RE: N00bish VB question by Anubis on 06-04-2004 at 03:21 PM

It doesn't seem to be working...I had to change it slightly because my version of VB uses slightly different codes;

code:
Private Sub Label1_load()
   times = 3
End Sub

Private Sub Command1_Click()
   times = times + 1 'the user clicked the button once more
   Select Case times
         Case 1 ' 1st time the button is clicked
                Label1.Caption = "1st text"
         Case 2 ' 2nd time the button is clicked
                Label1.Caption = "2nd text"
         Case 3 ' 3rd time the button is clicked
                Label1.Caption = "3rd text"
          ' etc....
         Case Else ' all the other cases (ie: from the last click you have coded)
                 Label1.Caption = "you've clicked the button more times than what I was expected and didn't coded any other text to be displayed .p"
     End Select
End Sub
3 times As Long

RE: N00bish VB question by dotNorma on 06-04-2004 at 03:22 PM

quote:
Originally posted by Anubis

case method? What's that?


Private Sub Command1_Click()
   Times = Times + 1
*The user clicked the button once more
         Case 1  (1st time the button is clicked)
                Label1.Caption="First Text"
         Case 2 '(2nd time the button is clicked)
                Label1.Caption="Second Text"
         Case 3 (3rd time the button is clicked)
                Label1.Caption="Third Text"
         Case Else
                 Label1.Caption="Here is where the text goes when all the other cases have been gone threw."
     End select
End sub

Its what Choli said. I cleaned it up a bit :-)

Edit :: You people beat me by a mile :-/
RE: N00bish VB question by Mike on 06-04-2004 at 03:44 PM

Does this help?

Also...
Where to ask your vb questions :
http://www.vbforums.com :spam: :P


RE: N00bish VB question by Choli on 06-04-2004 at 05:15 PM

quote:
Originally posted by Anubis
It doesn't seem to be working...I had to change it slightly because my version of VB uses slightly different codes;
of course, you have to adjust it depending on the name of the button, label and form ;)
quote:
Originally posted by Mike2
Does this help?
of course, but doing "select case"s with strings is way slower than with a long ;)
RE: N00bish VB question by Millenium_edition on 06-04-2004 at 05:37 PM

ehm, why not use an array?

code:
Dim strMyString(20) As String

Private Sub Form_Load()
strMyString(1) = "..."
strMyString(2) = "..."
End Sub

Private Sub Command1_Click()
Static lngTimes As Long
lngTimes = lngTimes + 1
Label1.Caption = strMyString(lngTimes)
End Sub