Results 1 to 9 of 9

Thread: modules

  1. #1

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


    I have a module and a form in my project, and the form has 6 command buttons and a Picturebox.

    One of my buttons will have multiple functions, depending on which button has been pressed before it. The name of this button is command2.

    If command1 is pressed, then command2 will carry out a particular task, if command 3 is pressed, then command2 will carry out another task....etc.

    I've instructed the command buttons to call different subs in a module.

    For example, if command1 is pressed, Public sub Randomnumbers is called, and random numbers from 0 to 20 are generated in Picture1.

    Call enablebutton, which is supposed to tell command2 what to do, is also called when command1 is pressed. But the Call enablebutton doesn't seem to work - hence why I'm writing. It's supposed to carry out the same task as Command1, as soon as Command1 is pressed.

    Here's my code. Could someone please tell me what's wrong with it?


    Private Sub Command1_Click()
    'public Randomnumbers is called in the module
    Call Randomnumbers
    'then public enablebutton is called. command2 has had no
    'code up to now. It's code is in Public enablebutton
    Call enablebutton
    End Sub



    'This is the code of 'Randomnumbers' in the module:
    Public Sub Randomnumbers()
    Form1.Picture1.Refresh
    Form1.Picture1.AutoRedraw = False
    Form1.Picture1.CurrentX = 10
    Form1.Picture1.CurrentY = 10
    Form1.Picture1.Print Int(Rnd * 20)
    End Sub

    Public Sub enablebutton()
    With command2
    Form1.Picture1.Refresh
    Form1.Picture1.AutoRedraw = False
    Form1.Picture1.CurrentX = 10
    Form1.Picture1.CurrentY = 10
    Form1.Picture1.Print Int(Rnd * 20)
    End With
    End Sub




  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Location
    Boulder, Colorado, USA
    Posts
    325
    Why do you have 'With command2' in your subroutine enablebutton?
    -Shickadance

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    //// ?????? \\\\

    I am confused...
    you click button 1 and it calls RandomNumbers
    and then it calls enablebutton.
    However, both subs have the identical process

    Instead of call enablebutton you would just
    code this in CommandButton 1
    Call RandomButton
    Call Command2_Click
    there is no need for enablebutton,
    least I can't see one

    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4
    Guest
    You can try a diffeent approach. Create a Public variable called CheckFunction that will check which Sub to call when Command2 is pressed.

    Code:
    Private Sub Command1_Click()
        RandomNumbers
        'Change CheckFunction to 0 to carry out a task
        CheckFunction = 0
    End Sub
    
    Private Sub Command3_Click()
        'Change CheckFunction to 1 to carry out different task
        CheckFunction = 1
    End Sub
    
    Private Sub Command2_Click()
    
        If CheckFunction = 1 Then
            'Do one task if it is 1 (Command3 was pressed)
        Else
            'Do other Task if it is 0 (Command1 was pressed)
        End If
    
    End Sub

  5. #5
    Addicted Member
    Join Date
    Feb 2000
    Location
    CWMBRAN,WALES,UK
    Posts
    146
    Perhaps it would be better to enable/disable your CmdButtons
    outside a Modular routine like:

    Private Sub Cmd1_Click()
    Call RandomNumbers
    CmdWhatever.Enabled = True/False

    Then write an If statement along these lines for Cmd2:

    If CmdWhatever.Enabled = True Then
    Your routine
    Else
    Your other routine
    End If

    Or perhaps a Select Case if numerous buttons are changing
    if you see what I'm getting at.
    Its not very clear what you are trying to achieve overall.

    Your With statement should be something like:

    With Form1!Picture1
    .Refresh
    .Autoredraw = False
    etc
    etc
    End With

    I hope that's some help to you

    GRAHAM

  6. #6

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

    Thumbs up

    What I am trying to acheive and seem to have acheived:

    Lots of buttons on my form. Each one does something different. One button, Command2, does a task depending on the button that was just clicked before it. Coomand2 does nothing until a button is clicked.

    Example:

    Program starts and user clicks command2. Nothing happens because there's no code in command2.

    User clicks command1. Then Command2 does whatever command1 does, when command2 is clicked.

    User clicks command3. Then Command2 does whatever command3 does, when command2 is clicked.

    MartinLiss, I altered your code a bit and got it working:

    'On my form, 3 command buttons and Picturebox

    Public checkfunction

    Private Sub Command1_Click()
    'Randomnumbers is called in the module
    Call Randomnumbers
    'give checkfunction a value
    checkfunction = 1
    End Sub

    Private Sub Command3_Click()
    'Statistics is called
    Call statistics
    'give checkfunction a value
    checkfunction = 2
    End Sub

    Sub Command2_Click()
    If checkfunction = 1 Then
    Call Randomnumbers
    End If
    If checkfunction = 2 Then
    Call statistics
    End If
    End Sub

    'In a module

    'This is the code of 'Randomnumbers' in the module:
    Public Sub Randomnumbers()
    Form1.Picture1.Refresh
    Form1.Picture1.AutoRedraw = False
    Form1.Picture1.CurrentX = 10
    Form1.Picture1.CurrentY = 10
    Form1.Picture1.Print Int(Rnd * 20)
    End Sub

    Public Sub statistics()
    Form1.Picture1.Refresh
    Form1.Picture1.AutoRedraw = False
    Form1.Picture1.CurrentX = 10
    Form1.Picture1.CurrentY = 10
    Form1.Picture1.Print "4 5 2 9"
    End Sub

    It works, but I'm curious:

    How come it works when I put only 'Public Checkfunction' in General Declarations, without As Integer, or As Variable etc...?

    How come it doesn't work when I put Checkfunction=0, instead of 1 or 2?

    Thanks!


  7. #7
    Guest
    MartinLiss? My name is Megatron.

    Anyway, regarding your question, it worked because if you do not specify a datatype to declare it as, it is automatically declared as a Variant.

    Regarding the 1 and 2 vs 0 and 1? I'm not sure. I tried it on my computer an everything worked fine both ways.


  8. #8

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

    Smile

    Megatron. Sorry for calling you MartinLiss.

    That's interesting that everything is declared as a Variant if you don't declare it. It must be because Variants are the most popular or most frequent, which is strange because all I've been declaring up to now is integers and strings.

    I'll try that checkfunction business again, but I'm pretty sure it didn't work for me - I'm using VB6, which I doubt matters.

    Muchos Gracious Senor Megatron!

  9. #9
    Guest
    Yes, Variant is the most flexible because it can be a Number or a String, but it uses up a lot more bytes than other Data types. Since CheckFunction is only using 2 numbers, you do not need to declare it as a Variant. Declare it as a Byte. It only uses up 1 byte whereas a Variant uses 16 bytes.


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