Jump to content

Func question


Bert
 Share

Recommended Posts

I wrote a example script to do the following:

If the example.ini has the following key- tabnumber = 2, then the GUI would have 2 tabs. any other number, it would have one tab.

The problem I'm having is I'm getting a error on my Func. It says it is badly formatted. ?????? It works fine if I just use the line 7 where I got it remarked out, and remark out anything with the ini. :P

#include <GuiConstants.au3>
#include <File.au3>

$main_gui = GUICreate("example", 600, 115)
$tab = GUICtrlCreateTab(-1, -1, 600)
$tab1 = GuiCtrlCreateTabItem("tab1")
;$tab2 = GuiCtrlCreateTabItem("tab2")
$ini = iniread("example.ini", "tab number", "tab")
if $ini == 2 then _tab_2
GUISetState()

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
    endselect
wend
            
Func _tab_2
    $tab2 = GuiCtrlCreateTabItem("tab2")
EndFunc
Link to comment
Share on other sites

OK, that fix that problem, but now I got a new issue. I get a error on line 16 saying the variable not being declared. :P

#include <GuiConstants.au3>
#include <File.au3>

$main_gui = GUICreate("example", 600, 115)
$tab = GUICtrlCreateTab(-1, -1, 600)
$tab1 = GuiCtrlCreateTabItem("tab1")
$ini = iniread("unplus.ini", "tab number", "tab", "2")
if $ini == 2 then _tab_2()   
;
GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $button1
        msgbox(0, "ok", "it worked")    
    endselect
wend
            
Func _tab_2()
    $tab2 = GuiCtrlCreateTabItem("tab2")
    $button1 = GUICtrlCreateButton("OK", 530, 35, 60, 20)
EndFunc
Link to comment
Share on other sites

OK, that fix that problem, but now I got a new issue. I get a error on line 16 saying the variable not being declared. :P

#include <GuiConstants.au3>
#include <File.au3>

$main_gui = GUICreate("example", 600, 115)
$tab = GUICtrlCreateTab(-1, -1, 600)
$tab1 = GuiCtrlCreateTabItem("tab1")
$ini = iniread("unplus.ini", "tab number", "tab", "2")
if $ini == 2 then _tab_2()   
;
GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $button1
        msgbox(0, "ok", "it worked")    
    endselect
wend
            
Func _tab_2()
    $tab2 = GuiCtrlCreateTabItem("tab2")
    $button1 = GUICtrlCreateButton("OK", 530, 35, 60, 20)
EndFunc

that's becaues you haven't assigned a anything to $button1

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Get in the habit of declaring (see global, local, dim) your variables before you use them and you'll be set.

- Steven

Edited by s_mack
Link to comment
Share on other sites

Oh, I see now... and that's why you probably got a compilation "warning" and not an "error", right? If I'm following right, you should be able to ignore the "warning" and it should work... but its still not kosher practice. Just declare the variable first... leave it the way you have it but add:

Dim $button1
somewhere near the top

- Steven

Link to comment
Share on other sites

I take it back... it probably was an error. This is because of your "if"... with the code as you have it, if $ini doesn't == 2 then you continue on without calling the function that declares the $button1 variable... then you end up with a variable being used that your script has no idea what it is. Again, adding the code I wrote will fix this. (other solutions and practices exist)

- Steven

Link to comment
Share on other sites

I just figured it out.

#include <GuiConstants.au3>
#include <File.au3>

$main_gui = GUICreate("example", 600, 115)
$tab = GUICtrlCreateTab(-1, -1, 600)
$tab1 = GuiCtrlCreateTabItem("tab1")
$button = GUICtrlCreateButton("OK", 530, 35, 60, 20)
$ini = iniread("unplus.ini", "tab number", "tab", "2")
if $ini == 2 then _tab_2()   

GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $button
        msgbox(0, "ok", "it worked")
        exit    
    endselect
wend
            
Func _tab_2()
GUISetState()
    $tab2 = GuiCtrlCreateTabItem("tab2")
    $button1 = GUICtrlCreateButton("OK", 530, 35, 60, 20)
    While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $button1
        msgbox(0, "ok", "it worked")
        exit    
    endselect
wend
EndFunc
Edited by vollyman
Link to comment
Share on other sites

Couple of changes as commented. Try it.

#include <GuiConstants.au3>
#include <File.au3>

$main_gui = GUICreate("example", 600, 115)
$tab = GUICtrlCreateTab(-1, -1, 600)
$tab1 = GUICtrlCreateTabItem("tab1")
$button = GUICtrlCreateButton("OK", 530, 35, 60, 20)
$ini = IniRead("unplus.ini", "tab number", "tab", "2")
If $ini == 2 Then _tab_2()   

GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $button
            MsgBox(0, "ok", "it worked")
            Exit    
    EndSelect
WEnd

Func _tab_2()
    $tab2 = GUICtrlCreateTabItem("tab2")
    $button1 = GUICtrlCreateButton("OK", 530, 35, 60, 20)
    GUISetState(); Moved this down
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                ExitLoop
            Case $msg = $button; Added this
                MsgBox(0, "ok", "it worked"); Added this
                Exit; Added this
            Case $msg = $button1
                MsgBox(0, "ok", "it worked")
                Exit    
        EndSelect
    WEnd
EndFunc
Link to comment
Share on other sites

Well, 1 message loop would be easier if possible. I'm guessing this may suit your cause ?

#include <GuiConstants.au3>
#include <File.au3>

$main_gui = GUICreate("example", 600, 115)
$tab = GUICtrlCreateTab(-1, -1, 600)
$tab1 = GUICtrlCreateTabItem("tab1")
$button = GUICtrlCreateButton("OK", 530, 35, 60, 20)
$ini = IniRead("unplus.ini", "tab number", "tab", "2")
If $ini == 2 Then
    $tab2 = GUICtrlCreateTabItem("tab2")
    $button1 = GUICtrlCreateButton("OK", 530, 35, 60, 20)
Else
    Global $button1 = -1, $tab2 = -1
EndIf  

GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $button
            MsgBox(0, "ok", "it worked")
            Exit
        Case $msg = $button1
            MsgBox(0, "ok2", "it worked")
            Exit
    EndSelect
WEnd
Link to comment
Share on other sites

I think what I'm saying is if the user switches back and forth between tabs, would it affect how the buttons work. I also plan to have different child windows work off each tab. Would it help if I posted the current script so you can see what I plan to do?

Link to comment
Share on other sites

I got it working. Here is the example code. If you wish to test it, you only need to change the iniread from 5 to whatever number between 1 - 5:

#include <GuiConstants.au3>
#include <File.au3>

$main_gui = GUICreate("example", 600, 115)
$tab = GUICtrlCreateTab(-1, -1, 600)
$tab1 = GUICtrlCreateTabItem("TAB 1")
$button1 = GUICtrlCreateButton("TAB 1", 530, 35, 60, 20)
$ini = IniRead("test.ini", "tab number", "tab", "5")
If $ini == 2 Then _tab_2()
If $ini == 3 Then _tab_3() 
If $ini == 4 Then _tab_4()
If $ini == 5 Then _tab_5() 

GUISetState()
;tab 1
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            Exit
        Case $msg = $button1
            MsgBox(0, "TAB 1", "it worked")
            Exit    
    EndSelect
WEnd

;tab 2
Func _tab_2()
    $tab2 = GUICtrlCreateTabItem("TAB 2")
    $button2 = GUICtrlCreateButton("TAB 2", 530, 35, 60, 20)
    GUISetState()
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                Exit
            Case $msg = $button; Added this
                MsgBox(0, "ok", "it worked")
                Exit 
            Case $msg = $button2
                MsgBox(0, "ok", "it worked")
                Exit    
        EndSelect
    WEnd
EndFunc

;tab3
Func _tab_3()
    $tab2 = GUICtrlCreateTabItem("TAB 2")
    $button2 = GUICtrlCreateButton("TAB 2", 530, 35, 60, 20)
    $tab3 = GUICtrlCreateTabItem("TAB 3")
    $button3 = GUICtrlCreateButton("TAB 3", 530, 35, 60, 20)   
    
    GUISetState()
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                Exit
            Case $msg = $button1; Added this
                MsgBox(0, "ok", "it worked")
                Exit 
            Case $msg = $button2
                MsgBox(0, "ok", "it worked")
            Case $msg = $button3
                MsgBox(0, "ok", "it worked")
                Exit    
        EndSelect
    WEnd
EndFunc

Func _tab_4()
    $tab2 = GUICtrlCreateTabItem("TAB 2")
    $button2 = GUICtrlCreateButton("TAB 2", 530, 35, 60, 20)
    $tab3 = GUICtrlCreateTabItem("TAB 3")
    $button3 = GUICtrlCreateButton("TAB 3", 530, 35, 60, 20)   
    $tab4 = GUICtrlCreateTabItem("TAB 4")
    $button4 = GUICtrlCreateButton("TAB 4", 530, 35, 60, 20)    
    GUISetState()
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                Exit
            Case $msg = $button1; Added this
                MsgBox(0, "ok", "it worked")
                Exit 
            Case $msg = $button2
                MsgBox(0, "ok", "it worked")
                exit
            Case $msg = $button3
                MsgBox(0, "ok", "it worked")
                Exit    
            Case $msg = $button4
                MsgBox(0, "ok", "it worked")
                Exit 
        EndSelect
    WEnd
EndFunc

Func _tab_5()
    $tab2 = GUICtrlCreateTabItem("TAB 2")
    $button2 = GUICtrlCreateButton("TAB 2", 530, 35, 60, 20)
    $tab3 = GUICtrlCreateTabItem("TAB 3")
    $button3 = GUICtrlCreateButton("TAB 3", 530, 35, 60, 20)   
    $tab4 = GUICtrlCreateTabItem("TAB 4")
    $button4 = GUICtrlCreateButton("TAB 4", 530, 35, 60, 20)
    $tab5 = GUICtrlCreateTabItem("TAB 5")
    $button5 = GUICtrlCreateButton("TAB 5", 530, 35, 60, 20)  
       
    GUISetState(); Moved this down
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $GUI_EVENT_CLOSE
                Exit
            Case $msg = $button1; Added this
                MsgBox(0, "ok", "it worked")
                Exit 
            Case $msg = $button2
                MsgBox(0, "ok", "it worked")
                exit
            Case $msg = $button3
                MsgBox(0, "ok", "it worked")
                Exit    
            Case $msg = $button4
                MsgBox(0, "ok", "it worked")
                Exit 
            Case $msg = $button5
                MsgBox(0, "ok", "it worked")
                Exit                 
        EndSelect
    WEnd
EndFunc

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...