Jump to content

Can't get GUICtrlRead to work correctly


ifsca
 Share

Recommended Posts

To test the program, I'm printing all checkbox names, states and text to the Notepad file.

I'm expecting the state to be 0 if unchecked and 1 if checked.

I'm expecting the text to be the name of the checkbox.

I'm not getting the state or text values that I expected.

What am I doing wrong?

Thanks for any help.

I'm using the following code:

;

; Open file

; selected by user

; Read file to array

; Display column headers

; Print headers selected by user to notepad

;

#include <file.au3>

Dim $aRecords

$message = "Choose one file."

$var = FileOpenDialog($message, @WindowsDir & "\My Documents", "All (*.*)", 1 )

If @error Then

MsgBox(4096,"","No File(s) chosen")

Else

; $var = StringReplace($var, "|", @CRLF)

; MsgBox(4096,"","You chose " & $var)

EndIf

$file = FileOpen($var, 0)

; Check if file opened for reading OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

If Not _FileReadToArray($var,$aRecords) Then

MsgBox(4096,"Error", " Error reading log to Array error:" & @error)

Exit

EndIf

FileClose($file)

; Save the column headings as variable

$cols = $aRecords[1]

; Save the column headings as array

$col_array = StringSplit($cols, ",")

; Show GUI for user to select columns to include

#include <GUIConstants.au3>

GUICreate("Select Columns to Include") ; will create a dialog box that when displayed is centered

; Create a checkbox and label for each column

For $c = 1 to $col_array[0]

$checkCN = GUICtrlCreateCheckbox($col_array[$c], 10, 10 * $c * 2, 150, 20)

Next

; Create two buttons

$Button_1 = GUICtrlCreateButton ("OK", 10, 10 * $c * 2 + 30, 50)

$Button_2 = GUICtrlCreateButton ( "Cancel", 70, 10 * $c * 2 + 30, 50)

; Define action to take for each button

GUISetState () ; will display an dialog box with $c checkboxes

; Run the GUI until the dialog is closed

dim $str

$str = "Selected Columns" & @CRLF & @CRLF

While 1

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Case $msg = $Button_1

for $r = 1 to $c - 1

$col_nam = $col_array[$r]

$state = GUICtrlRead($r)

$val = GUICtrlRead($r,1)

$str = $str & "Control ID = " & $r & @CRLF & "Column Header = " & $col_nam & @CRLF & "State = " & $state & @CRLF & "Value = " & $val & @CRLF & @CRLF

$val = 0

Next

Run('Notepad.exe') ; Will Run/Open Notepad

Sleep(1000)

ClipPut($str)

Send("^v")

ExitLoop

Case $msg = $Button_2

ExitLoop

EndSelect

Wend

I'm getting the following results:

Selected Columns

Control ID = 1

Column Header = Wynit_Part_Number

State = 0

Value = 0

Control ID = 2

Column Header = Manufacturer_Part_Number

State = 0

Value = 0

Control ID = 3

Column Header = Category_Description

State = 1

Value = Wynit_Part_Number

Control ID = 4

Column Header = Vendor_Name

State = 4

Value = Manufacturer_Part_Number

Control ID = 5

Column Header = Brief_Part_Description

State = 4

Value = Category_Description

Control ID = 6

Column Header = Quantity_Available

State = 4

Value = Vendor_Name

Control ID = 7

Column Header = CA_Quantity_Available

State = 4

Value = Brief_Part_Description

Control ID = 8

Column Header = NY_Quantity_Available

State = 4

Value = Quantity_Available

Control ID = 9

Column Header = TN_Quantity_Available

State = 4

Value = CA_Quantity_Available

Control ID = 10

Column Header = Your_cost

State = 4

Value = NY_Quantity_Available

Control ID = 11

Column Header = UPC_Code

State = 4

Value = TN_Quantity_Available

Control ID = 12

Column Header = MSRP

State = 4

Value = Your_cost

Control ID = 13

Column Header = Weights

State = 4

Value = UPC_Code

Control ID = 14

Column Header = Length

State = 4

Value = MSRP

Control ID = 15

Column Header = Width

State = 4

Value = Weights

Control ID = 16

Column Header = Height

State = 4

Value = Length

Link to comment
Share on other sites

Nice effort, this may help

#include <file.au3>
Dim $aRecords

$message = "Choose one file."
$var = FileOpenDialog($message, @WindowsDir & "\My Documents", "Text (*.txt)", 1)

If @error Then
    MsgBox(4096, "", "No File(s) chosen")
Else
    ; $var = StringReplace($var, "|", @CRLF)
    ; MsgBox(4096,"","You chose " & $var)
EndIf

$file = FileOpen($var, 0)

; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")

    Exit
EndIf

If Not _FileReadToArray($var, $aRecords) Then
    MsgBox(4096, "Error", " Error reading log to Array error:" & @error)
    Exit
EndIf

FileClose($file)

; Save the column headings as variable
$cols = $aRecords[1]
; Save the column headings as array
$col_array = StringSplit($cols, ",")
Dim $checkCN[(UBound($col_array))]

; Show GUI for user to select columns to include
#include <GUIConstants.au3>

GUICreate("Select Columns to Include") ; will create a dialog box that when displayed is centered

; Create a checkbox and label for each column
For $c = 1 To $col_array[0]
    $checkCN[$c] = GUICtrlCreateCheckbox($col_array[$c], 10, 10 * $c * 2, 150, 20)
Next
; Create two buttons
$Button_1 = GUICtrlCreateButton("OK", 10, 10 * $c * 2 + 30, 50)
$Button_2 = GUICtrlCreateButton("Cancel", 70, 10 * $c * 2 + 30, 50)
; Define action to take for each button


GUISetState() ; will display an dialog box with $c checkboxes

; Run the GUI until the dialog is closed
Dim $str
$str = "Selected Columns" & @CRLF & @CRLF
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Button_1
            For $r = 1 To $col_array[0]
                $col_nam = $col_array[$r]
                $state = _IsChecked($checkCN[$r])
                $val = GUICtrlRead($checkCN[$r], 1)
                $str = $str & "Control ID = " & $r & @CRLF & "Column Header = " & $col_nam & @CRLF & "State = " & $state & @CRLF & "Value = " & $val & @CRLF & @CRLF
                $val = 0
            Next
            Run('Notepad.exe') ; Will Run/Open Notepad
            Sleep(1000)
            ClipPut($str)
            Send("^v")
            ExitLoop
        Case $msg = $Button_2
            ExitLoop
    EndSelect
WEnd


Func _IsChecked($control)
    Return BitAND(GUICtrlRead($control), $GUI_CHECKED) = $GUI_CHECKED
EndFunc   ;==>_IsChecked

BTW Welcome to the Forums!!!

8)

NEWHeader1.png

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