Results 1 to 10 of 10

Thread: making this code neeeaaat....

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    I'm pretty sure my code below doesn't need to be repeated. I'd appreciate if someone could show me how to tidy it up a bit - it works fine, but I know it could be neater.

    Thanks for any help!

    'Two command buttons and a textbox. The command buttons are 'for back and forward, through the text.

    Dim MyText(0 To 5), CurText


    Private Sub Command1_Click()
    MyText(0) = "Yadayada"
    MyText(1) = "Chicaboom"
    MyText(2) = "Spaceman!"
    MyText(3) = "Everybody"
    MyText(4) = "Mr Bombastic"
    MyText(5) = "Get Up, Get Up!!"
    If CurText <> 0 Then
    Form1.Text1.Text = MyText(CurText - 1)
    CurText = CurText - 1
    End If
    End Sub

    Private Sub Command2_Click()
    MyText(0) = "Yadayada"
    MyText(1) = "Chicaboom"

    MyText(2) = "Spaceman!"
    MyText(3) = "Everybody"
    MyText(4) = "Mr Bombastic"
    MyText(5) = "Get Up, Get Up!!"
    If CurText <> 5 Then
    Form1.Text1.Text = MyText(CurText + 1)
    CurText = CurText + 1
    End If
    If CurText = 5 Then
    Form1.Text1.Text = MyText(CurText)
    End If
    End Sub

  2. #2
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Just move the

    MyText(0) = "Yadayada"
    MyText(1) = "Chicaboom"
    MyText(2) = "Spaceman!"
    MyText(3) = "Everybody"
    MyText(4) = "Mr Bombastic"
    MyText(5) = "Get Up, Get Up!!"

    into the Form_Load Event

  3. #3
    Guest

    Talking Tidying up!

    1) You can replace all the Form1.Text1.Text with Text1.Text, VB automatically knows it's on Form1.
    2) Move variable initalization into your Form_Load event.
    3) Try using TAB in order to tell apart nested code snippets.
    Example:
    instead of:

    If i=1 then
    select case
    case b
    b=j
    end select
    end if

    try:

    if i=1 then
    select case
    case b
    b=j
    end select
    end if

    it's much more readable.

    put one empty line between code parts that do different stuff, again, in order to tell apart.

    That's it

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    Thumbs down


    I have 'Form1.text1.text' because the above code is actually in a module which is called when the user clicks the back and forward buttons:

    'In form1:

    Private Sub Command1_Click()
    Call backwards
    End Sub

    Private Sub Command2_Click()
    Call forwards
    End Sub

    So I have to have 'Form1', so module will know what to do. Likewise, Sam, I can't use form_load. Is there any Module_Load event or something?

    Public Sub backwards()

    MyText(0) = "Yadayada"
    MyText(1) = "Chicaboom"
    MyText(2) = "Spaceman!"
    MyText(3) = "Everybody"
    MyText(4) = "Mr Bombastic"
    MyText(5) = "Get Up, Get Up!!"
    If CurText <> 0 Then
    Form1.Text1.Text = MyText(CurText - 1)
    CurText = CurText - 1
    End If
    End Sub

    Public Sub forwards()
    MyText(0) = "Yadayada"
    MyText(1) = "Chicaboom"
    MyText(2) = "Spaceman!"
    MyText(3) = "Everybody"
    MyText(4) = "Mr Bombastic"
    MyText(5) = "Get Up, Get Up!!"
    If CurText <> 5 Then
    Form1.Text1.Text = MyText(CurText + 1)
    CurText = CurText + 1
    End If
    If CurText = 5 Then
    Form1.Text1.Text = MyText(CurText)
    End If
    End Sub

  5. #5
    Member
    Join Date
    Jun 2000
    Location
    North of France
    Posts
    49

    Lightbulb Try this :

    Even if your code is in a module, you can put it in then Form_Load of the Form you first call (see the properties of your project).

    You can remove the .text of your textbox because it is the textbox default property (it's a detail but more easy to read)

    put some tabulations to your code as scOrp said :

    If a=x Then
    a=4
    Else
    a=9
    Endif

    will done:

    If a=x Then
    a=4
    Else: a=9
    Endif

    for now, it's the only thing I can tell you.


  6. #6
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    So make a init sub in the mosule that initilaes the values. Call this from form1's load event.

    Code:
    Private Sub InitTextValues()
      MyText(0) = "Yadayada" 
      MyText(1) = "Chicaboom" 
      MyText(2) = "Spaceman!" 
      MyText(3) = "Everybody" 
      MyText(4) = "Mr Bombastic" 
      MyText(5) = "Get Up, Get Up!!" 
    End Sub
    
    
    'Form1
    Private Sub Form_Load()
      InitTextValues
    End Sub
    Iain, thats with an i by the way!

  7. #7
    Member
    Join Date
    Jun 2000
    Location
    North of France
    Posts
    49

    Archhhhhhh

    ..., the spaces don't appear!!!!!!!!!!


    Eh, Lain17, Can you tell me how you did to insert pretty form code in your reply ?


    [Edited by (B2F)Tom on 07-06-2000 at 05:18 AM]

  8. #8
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    (B2F)Tom,

    Check out :

    http://forums.vb-world.net/index.php?action=bbcode

    The name is Iain17 any way. Thats with an i. Everyone seems to get that wrong. Oh well.
    Iain, thats with an i by the way!

  9. #9
    Member
    Join Date
    Jun 2000
    Location
    North of France
    Posts
    49

    Unhappy

    Sorry Iain17, I'll never do that again.
    Anyway, thanks for your tip !

  10. #10
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658
    No worries mate.

    Maybe i will have to include in my signature that it is an i.
    Iain, thats with an i by the way!

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