Results 1 to 20 of 20

Thread: [RESOLVED]Set A Commands Function In a Module?

  1. #1

    Thread Starter
    Hyperactive Member wiccaan's Avatar
    Join Date
    Apr 2004
    Location
    127.0.0.1
    Posts
    475

    Resolved [RESOLVED]Set A Commands Function In a Module?

    Is it possible to set a command buttons function to do something through a module? I have a module read an INI setting, and if its a certain value, I want it to set a command buttons function to do a certain thing. I have it in a module cause it does it for multiple command buttons, since it reads 10 sections of an INI file for settings of each button.

    Just wondering if this is possible. If not, what would be the next solution to this? Thanks in advance.
    Last edited by wiccaan; Nov 21st, 2004 at 05:35 PM.
    If my post was helpful please rate it

  2. #2
    Fanatic Member TDQWERTY's Avatar
    Join Date
    Oct 2003
    Location
    Oporto & Leiria, Portugal / Luanda, Angola
    Posts
    972
    U may find this thread usefull:

    http://www.vbforums.com/showthread.p...hreadid=301877

    A nice and working code that BuggyProgrammer made using
    Microsoft Script Control
    ::Winamp 5.xx id3v2 & modern skin support::
    ::NetCF DataGrid Programatically Scroll Example::
    Don't forget to rate posts from those who helped you solving your problem, clicking on and rating it.

  3. #3

    Thread Starter
    Hyperactive Member wiccaan's Avatar
    Join Date
    Apr 2004
    Location
    127.0.0.1
    Posts
    475
    Not exactly what Im trying to do. I dont want the users to see the code in files. Just want them to have the exe and the settings.ini file.

    My code reads an INI file for settings and sets a command button's function from a module, but thats where the problem is. Setting the command buttons function from a module's if then statements.

    Basicly I have 4 settings in the ini file for each button. (10 of them)

    The settings are: Caption, LoadForm, Program, Webpage.

    If the person sets Program=INTERNET then they have to set Webpage also. So it would be like:

    [BUTTON 1]
    Caption=My Site
    Program=INTERNET
    Website=www.mysite.com

    and would set the command1 button to shell internet explorer and open their site. What I got so far will read the ini file, put the settings into strings, and check them.

    But I cant get it to set the command buttons functions. I know that the if then statments work for each setting so far since I tested it with just the button instead of reading the function from a module.

    Any idea's?
    If my post was helpful please rate it

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    If each button could do more than one thing, then you'd have to have IF statements in each button to handle each action.

    Code:
    Sub button1.click()
    If Program=Internet Then 
      Shell "iexplore.exe"
    End If
    ' insert other choices here
    End Sub
    and use the same code for each button

  5. #5

    Thread Starter
    Hyperactive Member wiccaan's Avatar
    Join Date
    Apr 2004
    Location
    127.0.0.1
    Posts
    475
    Well I would rather have 1 function for all the command buttons so that I dont have to have that much code, nor have code for each command button. Instead, just have it look at the function, check the if then statments, and then find out what it should do.

    This is the function that I have so far for it. Its based on Command 1 right now so thats why there isnt any arrays or anything yet.

    Code:
    Public Sub LoadFunctions(INI As String)
    Dim Webpage
    Dim Program
    Dim LoadForm
    
    Program = INIGetSettingString("BUTTON 1", "Program", INI)
    Webpage = INIGetSettingString("BUTTON 1", "Webpage", INI)
    LoadForm = INIGetSettingString("BUTTON 1", "LoadForm", INI)
    
    If Program = "INTERNET" Then
        MDIForm1.Command1 = ShellExecute(0&, vbNullString, Webpage, vbNullString, vbNullString, vbNormalFocus)
        Exit Sub
        ElseIf Program <> Null Then
        ShellExecute 0&, vbNullString, Program, vbNullString, vbNullString, vbNormalFocus
    End If
    
    If LoadForm <> Null Then
        If LoadForm = "Drive" Then
            Form1.Show
        ElseIf LoadForm = "System" Then
            Form2.Show
        ElseIf LoadForm = "Network" Then
            Form3.Show
        ElseIf LoadForm = "Processor" Then
            Form4.Show
        ElseIf LoadForm = "Motherboard" Then
            Form5.Show
        ElseIf LoadForm = "Memory" Then
            Form6.Show
        ElseIf LoadForm = "Video" Then
            Form7.Show
        ElseIf LoadForm = "About" Then
            Form8.Show
        ElseIf LoadForm = "Exit" Then
            End
        Else
            MsgBox "There was an error with your config!", vbCritical, "Error!"
            ShellExecute 0&, vbNullString, INIFile, vbNullString, vbNullString, vbNormalFocus
        End If
    End If
    
    If Program <> Null And LoadForm <> Null Then
            MsgBox "There was an error with your config!", vbCritical, "Error!"
            ShellExecute 0&, vbNullString, INIFile, vbNullString, vbNullString, vbNormalFocus
    End If
    
    End Sub
    Kinda messy I know, but I just wanted to get it to work then fix it up and make it look nice.

    Basicly Im trying to have each command button just do :

    Code:
    Private Sub Command1_Click()
    LoadFunctions INIFile
    End Sub
    So it will look at that function, read the if-thens, find out what the value of the function that it will be using is, then set itself to that setting, or just have it look to that when clicked and do the desired function.
    If my post was helpful please rate it

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    Use a control array of buttons, and keep track of the Index.

    btw, this will always execute if Program has anything in it. Is that what you want?

    Code:
       ElseIf Program <> Null Then
        ShellExecute 0&, vbNullString, Program, vbNullString, vbNullString, vbNormalFocus

  7. #7

    Thread Starter
    Hyperactive Member wiccaan's Avatar
    Join Date
    Apr 2004
    Location
    127.0.0.1
    Posts
    475
    Like I said the code is messy right now and Ill fix it later. Just wanted to know if its possible to set the commands function at runtime when it reads the ini file the first time?
    If my post was helpful please rate it

  8. #8
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    I added to my last post.

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    You could set the captions when you read the file, but you still need the code that is generic for all buttons.

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    The actual code would be in the click event of the array of buttons. Just set the captions in the LoadFunction sub.

  11. #11

    Thread Starter
    Hyperactive Member wiccaan's Avatar
    Join Date
    Apr 2004
    Location
    127.0.0.1
    Posts
    475
    The button captions are read at the MDIForm load. So thats not a problem. And they all work from the INI. Which loads no problem. Ill mess around with the array of buttons and see what I can do with them. I just need to get the Array of buttons and the function to work together.

    so if CommandButton(3) is clicked then it will read the ini for the Button 3 settings and run it through the if then check for the function.
    If my post was helpful please rate it

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    Code:
    Private Sub Command1_Click(Index As Integer)
    
    End Sub

    I believe.

  13. #13

    Thread Starter
    Hyperactive Member wiccaan's Avatar
    Join Date
    Apr 2004
    Location
    127.0.0.1
    Posts
    475
    Ok so I changed my commands to an array. So there are 10 buttons in the array. 0 to 9. So, this is what I have the LoadCaptions Function do..

    It will take the INI file, Read the 10 Button lines:

    Code:
    [BUTTON 1]
    Caption=1
    
    [BUTTON 2]
    Caption=2
    
    [BUTTON 3]
    Caption=3
    
    [BUTTON 4]
    Caption=4
    
    [BUTTON 5]
    Caption=5
    
    [BUTTON 6]
    Caption=6
    
    [BUTTON 7]
    Caption=7
    
    [BUTTON 8]
    Caption=8
    
    [BUTTON 9]
    Caption=9
    
    [BUTTON 10]
    Caption=10
    How would I use the INIGetSettingString function with this in a for statment to set the captions to the correct buttons using an array for them?

    I was thinking something like:

    Code:
    Dim ReadButton(0 To 9)
    Dim z As String
    
    z = "BUTTON" & x
    For x = 0 To 9
    z = "BUTTON" & x
    ReadButton(x) = INIGetSettingString(z, "Caption", INI)
    MDIForm1.Command1(x).Caption = ReadButton(x)
    Next

    But it doesnt work. Any idea's? Also, this is the function for reading from an INI that Im using:

    Code:
    Public Function INIGetSettingString( _
      strSection As String, _
      strKeyName As String, _
      strFile As String) _
      As String
    
      Dim strBuffer As String * 256
      Dim intSize As Integer
    
      On Error GoTo PROC_ERR
      
      intSize = GetPrivateProfileString(strSection, strKeyName, "", strBuffer, 256, strFile)
    
      INIGetSettingString = Left$(strBuffer, intSize)
    
    PROC_EXIT:
      Exit Function
      
    PROC_ERR:
      MsgBox "Error: " & Err.Number & ". " & Err.Description, , _
        "INIGetSettingString"
      Resume PROC_EXIT
      
    End Function
    If my post was helpful please rate it

  14. #14

    Thread Starter
    Hyperactive Member wiccaan's Avatar
    Join Date
    Apr 2004
    Location
    127.0.0.1
    Posts
    475
    Ok got it to read the Captions from an Array.

    Code:
    Public Sub LoadCaptions(INI As String)
    Dim ReadButton(0 To 9)
    Dim z As String
    
    For i = 0 To 9
    z = "BUTTON " & i
    ReadButton(i) = INIGetSettingString(z, "Caption", INI)
    MDIForm1.Command1(i).Caption = ReadButton(i)
    MDIForm1.Text1.Text = MDIForm1.Text1.Text & z & vbCrLf
    Next
    
    End Sub
    Now to get it to read the functions like this...
    If my post was helpful please rate it

  15. #15

    Thread Starter
    Hyperactive Member wiccaan's Avatar
    Join Date
    Apr 2004
    Location
    127.0.0.1
    Posts
    475
    Ok now that the Command buttons are in an array, 0 to 9 for 10 command buttons. Im stuck at the functions part.

    I need each button when clicked to read the LoadFuntions function to check inside the ini file for their command.

    Each command button is defined in the ini file like above.

    So if Command1(2) is clicked then it would read the ini for [BUTTON 2] and find what to do.

    Then for Command1(5) it would read for BUTTON 5. How would I get it to work correctly?

    I tried simply doing the INIGetSettingString with z as the Button number so it would check for the button, but it didnt correctly read the function. No errors, just nothing happened. The buttons command was set to load a form but nothing happened. Any idea's?

    EDIT:: Meaning, how about I get it to read the certain ini area for the command button pressed?
    Last edited by wiccaan; Nov 21st, 2004 at 04:10 AM.
    If my post was helpful please rate it

  16. #16

    Thread Starter
    Hyperactive Member wiccaan's Avatar
    Join Date
    Apr 2004
    Location
    127.0.0.1
    Posts
    475
    Ok I got it to send the correct index of the command to the module now, so I think Ill be able to get it to work from here. If not Ill post again. Ill leave this thread as unresolved for now, but I will come back to it again in a sec.

    This is what I did:

    Changed my LoadFunctions function to:

    Code:
    Public Sub LoadFunctions(INI As String, z As String)
    Msgbox z, vbOkOnly, "You Pressed:"
    End sub
    Then on the main form I did:

    Code:
    Dim z As String
    
    Private Sub Command1_Click(Index As Integer)
    z = Cstr(Index)
    LoadFunction INIFile, z
    End Sub
    Making it send the index to the function and have the function display it. Ill work on the whole function now. Thanks again.
    If my post was helpful please rate it

  17. #17

    Thread Starter
    Hyperactive Member wiccaan's Avatar
    Join Date
    Apr 2004
    Location
    127.0.0.1
    Posts
    475
    Alright I got the function to work properly now yay.

    Thanks again for the help guys.
    If my post was helpful please rate it

  18. #18

    Thread Starter
    Hyperactive Member wiccaan's Avatar
    Join Date
    Apr 2004
    Location
    127.0.0.1
    Posts
    475
    Heres my complete function. Works perfectly. No problems no errors. Nothing. Just smooth work.

    Code:
    Public Sub LoadFunctions(INI As String, z As String)
    On Error Resume Next
    Dim a As String
    Dim ReadLoadForm
    Dim ReadProgram
    Dim ReadWebpage
    
    INIFile = "buttonconf.ini"
    ProgPath = App.Path
    If Right(App.Path, 1) <> "\" Then ProgPath = App.Path & "\"
    
    INI = ProgPath & INIFile
    
    a = "BUTTON " & z
    ReadLoadForm = INIGetSettingString(a, "LoadForm", INI)
    GetForm = ReadLoadForm
    
    If GetForm = "" Then
            GoTo CheckProgram
        ElseIf GetForm = "Drive" Then
            Form1.Show
            Form1.SetFocus
        ElseIf GetForm = "System" Then
            Form2.Show
            Form2.SetFocus
        ElseIf GetForm = "Network" Then
            Form3.Show
            Form3.SetFocus
        ElseIf GetForm = "Processor" Then
            Form4.Show
            Form4.SetFocus
        ElseIf GetForm = "Motherboard" Then
            Form5.Show
            Form5.SetFocus
        ElseIf GetForm = "Memory" Then
            Form6.Show
            Form6.SetFocus
        ElseIf GetForm = "Video" Then
            Form7.Show
            Form7.SetFocus
        ElseIf GetForm = "About" Then
            Form8.Show
            Form8.SetFocus
        ElseIf GetForm = "Finish" Then
            End
        Else
            MsgBox "Your configuration is incorrect. Please fix the changes.", vbCritical, "Error!"
     End If
    
    CheckProgram:
    ReadProgram = INIGetSettingString(a, "Program", INI)
        GetProgram = ReadProgram
            If GetProgram = "" Then
                'Blah
                ElseIf GetProgram = "INTERNET" Then
                    ReadWebpage = INIGetSettingString(a, "Webpage", INI)
                        If ReadWebpage = "" Then
                            Webpage = "http://www.uo.clankoa.com"
                            ShellExecute 0&, vbNullString, Webpage, vbNullString, vbNullString, vbNormalFocus
                                Else
                                    Webpage = ReadWebpage
                                    ShellExecute 0&, vbNullString, Webpage, vbNullString, vbNullString, vbNormalFocus
                        End If
                        Else
                            Open App.Path & "\temp.bat" For Output As #1
                            Print #1, GetProgram
                            EndBatFile = "end"
                            Print #1, EndBatFile
                            Close
                            TempLoad = ProgPath & "temp.bat"
                            Shell TempLoad, vbHide
                            Sleep (100)
                            Kill TempLoad
            End If
    
    End Sub
    If ya got any idea's on how I could make it smaller, or something else. Let me know. Thanks again to anyone that helped me while I was working on this.
    If my post was helpful please rate it

  19. #19
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901
    Code:
    If GetForm = "" Then
            GoTo CheckProgram
        ElseIf GetForm = "Drive" Then
            Form1.Show
            Form1.SetFocus
        ElseIf GetForm = "System" Then
            Form2.Show
            Form2.SetFocus
    could be smaller, or at least more readable by using a Select Case statement:

    Code:
    Select Case GetForm
      Case ""
         GoTo CheckProgram
      Case "Drive"
          Form1.Show
          Form1.SetFocus
      Case "System"
          Form2.Show
          Form2.SetFocus
      Case Else
    '      whatever
      End Select

  20. #20

    Thread Starter
    Hyperactive Member wiccaan's Avatar
    Join Date
    Apr 2004
    Location
    127.0.0.1
    Posts
    475
    Heh good idea. Ill change that. I should probably leave the other if-then statment since it has a lot branching and checking to do.

    Can you do like a Else case with select case? I never really used Case's that much unless Im using list box's and such.
    If my post was helpful please rate it

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