Jump to content

Two Forms are running at the same time even though I have set the condition


NhiDC
 Share

Go to solution Solved by Andreik,

Recommended Posts

I have a main Form like this. When I click on one of the 4 items, I want it will open the 2nd Form as shown in Figure 2

image.png.fd9beb3923688735714aa749ba30c6aa.png

image.png.523e1c87a35b9bd712c29d37fe20a691.png

But when I run, both Forms appear at the same time. Even when I turn off Form 2, it turns on by itself, even though I have set the conditions to open the 2nd Form. 

image.png.4e80f96fd0292e7def567c1c641566fc.png

I don't have any ideas for this, please help me. Thanks in advance.

Below is my source code

 

#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 437, 192, 124)
$ListView1 = GUICtrlCreateListView("Column 1|Column 2|Column 3", 0, 56, 840, 510)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 100)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 100)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 100)
; Add items
        _GUICtrlListView_AddItem($ListView1, "Row 1: Col 1")
        _GUICtrlListView_AddSubItem($ListView1, 0, "Row 1: Col 2", 1)
        _GUICtrlListView_AddSubItem($ListView1, 0, "Row 1: Col 3", 2)
        _GUICtrlListView_AddItem($ListView1, "Row 2: Col 1")
        _GUICtrlListView_AddSubItem($ListView1, 1, "Row 1: Col 3", 2)
        _GUICtrlListView_AddSubItem($ListView1, 1, "Row 2: Col 2", 1)
        _GUICtrlListView_AddItem($ListView1, "Row 3: Col 1")
        _GUICtrlListView_AddItem($ListView1, "Row 4: Col 1")
GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
Global $getItemClick 
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $getItemClick > -1
            $getItemClick = -1
            GUISetState(@SW_DISABLE, $Form1)
            DetailService()
            GUISetState(@SW_ENABLE, $Form1)
    EndSwitch
WEnd

Func DetailService()
    #Region ### START Koda GUI section ### Form
    $GUI2 = GUICreate("GUI2", 617, 229, 184, 147)
    $Form2 = GUICtrlCreateLabel("Form 2", 8, 8, 105, 29)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
            Case $GUI_EVENT_CLOSE
                GUIDelete($GUI2)
                ExitLoop

        EndSwitch
    WEnd
EndFunc

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
    $hWndListView = $ListView1
    If Not IsHWnd($hWndListView) Then $hWndListView = GUICtrlGetHandle($ListView1)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)

    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
                    Local $iItem = DllStructGetData($tInfo, "Item")
;~                     If $iItem <> -1 Then ConsoleWrite("!> Click on item " & $iItem & @LF)

                    If $iItem <> -1 Then
                        $getItemClick = $iItem


                    EndIf
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

 

Link to comment
Share on other sites

  • Solution

What does $nMsg have to do with $getItemClick > -1?

Global $getItemClick = -1

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    If $getItemClick > -1 Then
        $getItemClick = -1
        GUISetState(@SW_DISABLE, $Form1)
        DetailService()
        GUISetState(@SW_ENABLE, $Form1)
    EndIf
WEnd

or

Global $getItemClick = -1

While 1
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            Exit
        Case $getItemClick > -1
            $getItemClick = -1
            GUISetState(@SW_DISABLE, $Form1)
            DetailService()
            GUISetState(@SW_ENABLE, $Form1)
    EndSelect
WEnd

 

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

34 minutes ago, Andreik said:

What does $nMsg have to do with $getItemClick > -1?

Global $getItemClick = -1

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    If $getItemClick > -1 Then
        $getItemClick = -1
        GUISetState(@SW_DISABLE, $Form1)
        DetailService()
        GUISetState(@SW_ENABLE, $Form1)
    EndIf
WEnd

or

Global $getItemClick = -1

While 1
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            Exit
        Case $getItemClick > -1
            $getItemClick = -1
            GUISetState(@SW_DISABLE, $Form1)
            DetailService()
            GUISetState(@SW_ENABLE, $Form1)
    EndSelect
WEnd

 

Oh! My bad. Switch Case is used for values, and Select is used for expressions 🤦‍♂️Sorry I just learned AutoIT. Thank you very much.

Link to comment
Share on other sites

47 minutes ago, Andreik said:

What does $nMsg have to do with $getItemClick > -1?

Global $getItemClick = -1

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    If $getItemClick > -1 Then
        $getItemClick = -1
        GUISetState(@SW_DISABLE, $Form1)
        DetailService()
        GUISetState(@SW_ENABLE, $Form1)
    EndIf
WEnd

or

Global $getItemClick = -1

While 1
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            Exit
        Case $getItemClick > -1
            $getItemClick = -1
            GUISetState(@SW_DISABLE, $Form1)
            DetailService()
            GUISetState(@SW_ENABLE, $Form1)
    EndSelect
WEnd

 

$nMsg has nothing to do with $getItemClick > -1. I just want to catch the event when the item is clicked so that it will show Form 2

Link to comment
Share on other sites

8 minutes ago, NhiDC said:

Oh! My bad. Switch Case is used for values, and Select is used for expressions 🤦‍♂️Sorry I just learned AutoIT. Thank you very much.

In your particular case $nMsg value is compared with each case of your switch expression. Since $nMsg will contain value 0 most of the time when there is no message to be processed and $getItemClick > -1 it's evaluated as false most of the time in your particular case, the code will be called each time when these conditions are met. Select allows you to conditionally run statements based on different expressions.

When the words fail... music speaks.

Link to comment
Share on other sites

7 minutes ago, Andreik said:

In your particular case $nMsg value is compared with each case of your switch expression. Since $nMsg will contain value 0 most of the time when there is no message to be processed and $getItemClick > -1 it's evaluated as false most of the time in your particular case, the code will be called each time when these conditions are met. Select allows you to conditionally run statements based on different expressions.

Thank you. Can i ask more? If don't set the $getItemClick variable to 1, then 2 Forms will also appear at the same time, so the $getItemClick variable initially has a value greater than -1, but I tried to print it, it didn't print anything. Can you explain more?

Link to comment
Share on other sites

You have to set $getItemClick to -1 or something less or the code that make your second form to appear will be called. If you just declare $getItemClick but don't assign an implicit value it will be set to 0 which is greater than -1 so the code will still be called. Actually you don't even need this variable. Here is how I would write your code:

#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

Global $Form1, $ListView1, $hDummy

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 437, 192, 124)
$ListView1 = GUICtrlCreateListView("Column 1|Column 2|Column 3", 0, 56, 840, 510)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 100)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 100)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 100)
; Add items
        _GUICtrlListView_AddItem($ListView1, "Row 1: Col 1")
        _GUICtrlListView_AddSubItem($ListView1, 0, "Row 1: Col 2", 1)
        _GUICtrlListView_AddSubItem($ListView1, 0, "Row 1: Col 3", 2)
        _GUICtrlListView_AddItem($ListView1, "Row 2: Col 1")
        _GUICtrlListView_AddSubItem($ListView1, 1, "Row 1: Col 3", 2)
        _GUICtrlListView_AddSubItem($ListView1, 1, "Row 2: Col 2", 1)
        _GUICtrlListView_AddItem($ListView1, "Row 3: Col 1")
        _GUICtrlListView_AddItem($ListView1, "Row 4: Col 1")
$hDummy = GUICtrlCreateDummy()
GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While True
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hDummy
            ConsoleWrite('idItem: ' & GUICtrlRead($hDummy) & @CRLF)
            DetailService()
    EndSwitch
WEnd

Func DetailService()
    GUISetState(@SW_DISABLE, $Form1)
    #Region ### START Koda GUI section ### Form
    $GUI2 = GUICreate("GUI2", 617, 229, 184, 147)
    $Form2 = GUICtrlCreateLabel("Form 2", 8, 8, 105, 29)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    While True
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd
    
    GUISetState(@SW_ENABLE, $Form1)
    GUIDelete($GUI2)
EndFunc

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
    $hWndListView = $ListView1
    If Not IsHWnd($hWndListView) Then $hWndListView = GUICtrlGetHandle($ListView1)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
                    Local $iItem = DllStructGetData($tInfo, "Item")
                    If $iItem <> -1 Then GUICtrlSendToDummy($hDummy, $iItem)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

 

Edited by Andreik

When the words fail... music speaks.

Link to comment
Share on other sites

32 minutes ago, Andreik said:

You have to set $getItemClick to -1 or something less or the code that make your second form to appear will be called. If you just declare $getItemClick but don't assign an implicit value it will be set to 0 which is greater than -1 so the code will still be called. Actually you don't even need this variable. Here is how I would write your code:

#include <GUIConstantsEx.au3>
#include <ListViewConstants.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>

Global $Form1, $ListView1, $hDummy

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 437, 192, 124)
$ListView1 = GUICtrlCreateListView("Column 1|Column 2|Column 3", 0, 56, 840, 510)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 0, 100)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 1, 100)
GUICtrlSendMsg(-1, $LVM_SETCOLUMNWIDTH, 2, 100)
; Add items
        _GUICtrlListView_AddItem($ListView1, "Row 1: Col 1")
        _GUICtrlListView_AddSubItem($ListView1, 0, "Row 1: Col 2", 1)
        _GUICtrlListView_AddSubItem($ListView1, 0, "Row 1: Col 3", 2)
        _GUICtrlListView_AddItem($ListView1, "Row 2: Col 1")
        _GUICtrlListView_AddSubItem($ListView1, 1, "Row 1: Col 3", 2)
        _GUICtrlListView_AddSubItem($ListView1, 1, "Row 2: Col 2", 1)
        _GUICtrlListView_AddItem($ListView1, "Row 3: Col 1")
        _GUICtrlListView_AddItem($ListView1, "Row 4: Col 1")
$hDummy = GUICtrlCreateDummy()
GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###
GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

While True
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hDummy
            ConsoleWrite('idItem: ' & GUICtrlRead($hDummy) & @CRLF)
            DetailService()
    EndSwitch
WEnd

Func DetailService()
    GUISetState(@SW_DISABLE, $Form1)
    #Region ### START Koda GUI section ### Form
    $GUI2 = GUICreate("GUI2", 617, 229, 184, 147)
    $Form2 = GUICtrlCreateLabel("Form 2", 8, 8, 105, 29)
    GUISetState(@SW_SHOW)
    #EndRegion ### END Koda GUI section ###

    While True
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
    WEnd
    
    GUISetState(@SW_ENABLE, $Form1)
    GUIDelete($GUI2)
EndFunc

Func WM_NOTIFY($hWnd, $Msg, $wParam, $lParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView
    $hWndListView = $ListView1
    If Not IsHWnd($hWndListView) Then $hWndListView = GUICtrlGetHandle($ListView1)

    $tNMHDR = DllStructCreate($tagNMHDR, $lParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_CLICK
                    Local $tInfo = DllStructCreate($tagNMLISTVIEW, $lParam)
                    Local $iItem = DllStructGetData($tInfo, "Item")
                    If $iItem <> -1 Then GUICtrlSendToDummy($hDummy, $iItem)
            EndSwitch
    EndSwitch

    Return $GUI_RUNDEFMSG
EndFunc

 

Thank you so much again 😲

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...