Jump to content

Not seeing buttons


Dana
 Share

Recommended Posts

I have a script that gets com port input (using Martin Gibson's CommMG routines) and displays the data in a GUI ListView. That works fine, but I'm having trouble exiting the loop:

#include <CommMg.au3>
#include <GUIConstantsEx.au3>
#include <ListviewConstants.au3>

;<irrelevant stuff snipped here>

$w = 600
$h = 800
GUICreate("Data Collection", $w, $h)
GUICtrlCreateLabel("Part Number: " & $partnum & "   Job Number: " & $job, 30, 10)
GUICtrlCreateLabel(" Plunger Depth Gage (A): " & $gageA & " Ball Depth Gage (B): " & $gageB & " Clearance: " & $clearance, 30, 30)
$okbutton = GUICtrlCreateButton("OK", 30, $h-40, 60)
$cancelbutton = GUICtrlCreateButton("Cancel", $w-30-60, $h-40, 60)
;$testbutton = GUICtrlCreateButton("test", $w/2-30, $h-40, 60)
$t = 1
$datalist = GUICtrlCreateListView("No.|Plunger Depth|Ball Depth|  |Shaft Length",30, 50, 540, $h- 100)
Dim $balldepth[256]
Dim $plungerdepth[256]
Dim $shaftlength[256]
Dim $line[256]
GUISetState(@SW_SHOW)
$anum = 0
$bnum = 0
$dataline = 0
While 1
    $guimsg = GUIGetMsg()
    $instring = StringStripWS(_CommGetLine(@CR,0,500),8)
    
    Select
        Case $guimsg = $GUI_EVENT_CLOSE Or $guimsg = $cancelbutton
            GUIDelete()
            Exit
            
        Case $instring <> ""
    ;Gageport string is number, reading (-.12345), , gage # (01 or 02)
        $readnum = StringLeft($instring, StringInStr($instring, ",")-1); read number from gage; not using this
        $instring = StringTrimLeft($instring, StringInStr($instring, ","))
        $value = StringLeft($instring, StringInStr($instring, ",")-1)
        $instring = StringTrimLeft($instring, StringInStr($instring, ","))
        $gageno = StringTrimLeft($instring, StringInStr($instring, ",")); second trim due to blank value field
    ;debug box
    ;$return = MsgBox(4097, "data received", "Reading: " & $readnum & @CRLF & "Value: " & $value & @CRLF & "Gage: " & $gageno)
    ;If ($return == 2) Then Exit
        
        If ($gageno == "01") Then
        ;MsgBox(4096, "debug", "got here")
            $anum = $anum + 1
            $plungerdepth[$anum] = $value
            If ($anum > $bnum) Then
                $line[$anum] = GUICtrlCreateListViewItem("-|-|-|-|-", $datalist); create a new line
                $balldepth[$anum] = "-"
            EndIf
            $dataline = $anum
        ElseIf ($gageno == "02") Then
            $bnum = $bnum + 1
            $balldepth[$bnum] = $value
            If ($bnum > $anum) Then
                $line[$bnum] = GUICtrlCreateListViewItem("-|-|-|-|-", $datalist); create a new line
                $plungerdepth[$bnum] = "-"
            EndIf
            $dataline = $bnum
        Else
            MsgBox(4096, "debug", "line " & $dataline & " A: " & $anum & " B: " & $bnum)    
        EndIf
        If ($plungerdepth[$dataline] <> "-" And $balldepth[$dataline] <> "-") Then $shaftlength[$dataline] = $plungerdepth[$dataline] + $balldepth[$dataline] + $clearance 
        $itemtext = $dataline & "|" & $plungerdepth[$dataline]  & "|" & $balldepth[$dataline] & "| |" & $shaftlength[$dataline]
    ;MsgBox(4096, "debug", "line " & $dataline & " A: " & $anum & " B: " & $bnum & @CRLF & $itemtext)
    ;GUISetState($GUI_FOCUS, $line[$t])
        GUICtrlSetData($line[$dataline], $itemtext)
    EndSelect
;Sleep(500) 
WEnd

GUIDelete()
Exit

The problem is it doesn't see the press of the cancel button, except occasionally. I've played with the timeout on the CommGetLine function and various sleep values in various places, but nothing is reliable.

I also tried it in OnEvent mode:

$w = 600
$h = 800
Opt("GUIOnEventMode", 1) ; Change to OnEvent mode 
$datawindow = GUICreate("Data Collection", $w, $h)
GUICtrlCreateLabel("Part Number: " & $partnum & "   Job Number: " & $job, 30, 10)
GUICtrlCreateLabel(" Plunger Depth Gage (A): " & $gageA & " Ball Depth Gage (B): " & $gageB & " Clearance: " & $clearance, 30, 30)
$okbutton = GUICtrlCreateButton("OK", 30, $h-40, 60)
$cancelbutton = GUICtrlCreateButton("Cancel", $w-30-60, $h-40, 60)
$datalist = GUICtrlCreateListView("No.|Plunger Depth|Ball Depth|  |Shaft Length",30, 50, 540, $h- 100)
Dim $balldepth[256]
Dim $plungerdepth[256]
Dim $shaftlength[256]
Dim $line[256]
GUISetOnEvent($GUI_EVENT_CLOSE, "CancelClicked")
GUISetOnEvent($cancelbutton, "CancelClicked")
GUISetState(@SW_SHOW)
$anum = 0
$bnum = 0
$dataline = 0
While 1
    $instring = StringStripWS(_CommGetLine(@CR,0,300),8)

    ;Gageport string is number, reading (-.12345), , gage # (01 or 02)
        $readnum = StringLeft($instring, StringInStr($instring, ",")-1); read number from gage; not using this
        $instring = StringTrimLeft($instring, StringInStr($instring, ","))
        $value = StringLeft($instring, StringInStr($instring, ",")-1)
        $instring = StringTrimLeft($instring, StringInStr($instring, ","))
        $gageno = StringTrimLeft($instring, StringInStr($instring, ",")); second trim due to blank value field
        
        If ($gageno == "01") Then
            $anum = $anum + 1
            $plungerdepth[$anum] = $value
            If ($anum > $bnum) Then
                $line[$anum] = GUICtrlCreateListViewItem("-|-|-|-|-", $datalist); create a new line
                $balldepth[$anum] = "-"
            EndIf
            $dataline = $anum
        ElseIf ($gageno == "02") Then
            $bnum = $bnum + 1
            $balldepth[$bnum] = $value
            If ($bnum > $anum) Then
                $line[$bnum] = GUICtrlCreateListViewItem("-|-|-|-|-", $datalist); create a new line
                $plungerdepth[$bnum] = "-"
            EndIf
            $dataline = $bnum
        Else
        ;MsgBox(4096, "debug", "line " & $dataline & " A: " & $anum & " B: " & $bnum)   
        EndIf
        If ($plungerdepth[$dataline] <> "-" And $balldepth[$dataline] <> "-") Then $shaftlength[$dataline] = $plungerdepth[$dataline] + $balldepth[$dataline] + $clearance 
        $itemtext = $dataline & "|" & $plungerdepth[$dataline]  & "|" & $balldepth[$dataline] & "| |" & $shaftlength[$dataline]
        GUICtrlSetData($line[$dataline], $itemtext)
        Sleep(500)
    WEnd
    

Func CancelClicked()
    MsgBox(4096, "debug", "cancel clicked")
    GUIDelete()
    Exit
EndFunc

In this version, the Windows "X" button works reliably to close the window every time, but the cancel button in the GUI still doesn't work.

What am I missing?

Link to comment
Share on other sites

  • Developers

Use:

GUIctrlSetOnEvent($cancelbutton, "CancelClicked")

in stead of:

GUISetOnEvent($cancelbutton, "CancelClicked")

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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