Jump to content

Stopping a while loop when it gets to a round number and fixing a gui radio toggle problem


Hyflex
 Share

Recommended Posts

Hi,

My code so far is:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>


Local $InputListItems[3] = ["item 1", "item 2", "item 3"]
Local $test[1] = ["dog"]
Local $test1[1] = ["cat"]


; #### MAIN FORM ####
$Form1 = GUICreate("Form1", 651, 250, 192, 124)


; ##### INPUT MAIN START #####
$InputMain = GUICtrlCreateGroup("Input Main", 8, 0, 137, 137)

; List
$InputListMain = GUICtrlCreateList("", 16, 16, 121, 118)
GuiCtrlSetData($InputListMain, _ArrayToString($InputListItems))
; ##### INPUT MAIN END #####


; ##### INPUT ALT START #####
$InputAlt = GUICtrlCreateGroup("Input Alt", 151, 0, 137, 137)

; List
$InputListAlt = GUICtrlCreateList("", 159, 16, 121, 118)
GuiCtrlSetData($InputListAlt, _ArrayToString($InputListItems))
; ##### INPUT ALT END #####


; ##### RATES START #####
$Rates = GUICtrlCreateGroup("Rates", 6, 139, 281, 49)
$MainRateInput = GUICtrlCreateInput("1", 14, 155, 121, 21)
$AltRateInput = GUICtrlCreateInput("3", 158, 155, 121, 21)
; ##### RATES END #####


; ##### LEFT OUTPUTS START #####
$LeftOutputs = GUICtrlCreateGroup("Left Option",  296, 0, 169, 185)
$LeftOutputList = GUICtrlCreateList("", 304, 16, 153, 162)
GuiCtrlSetData($LeftOutputList, _ArrayToString($test))
; ##### LEFT OUTPUTS End #####


; ##### RIGHT OUTPUTS START #####
$RightOutputs = GUICtrlCreateGroup("Right Option",  472, 0, 169, 185)
$RightOutputList = GUICtrlCreateList("", 480, 16, 153, 162)
GuiCtrlSetData($RightOutputList, _ArrayToString($test1))
; ##### RIGHT OUTPUTS End #####


; ##### OUTPUT START #####
$Output = GUICtrlCreateGroup("Main Output", 8, 192, 281, 49)
$OututInput = GUICtrlCreateInput("", 16, 208, 265, 21)
; ##### OUTPUT END #####

$Picker = GUICtrlCreateGroup("Option", 294, 192, 345, 49)
$Radio1 = GUICtrlCreateRadio("Left", 312, 216, 113, 17)
$Radio2 = GUICtrlCreateRadio("Right", 488, 216, 113, 17)


GUISetState(@SW_SHOW)
;GUICtrlSetState($Radio1, $GUI_CHECKED)

Func GenerateRates($MainRate, $AltRate)

    Local $Start = $MainRate
    Local $End = $AltRate
    Local $PossibleValues[0] = []

    Local $PossibleValue = $Start & "/" & $End
    _ArrayAdd($PossibleValues, $PossibleValue)
    For $i = 1 to 29
        $Start = $Start
        $End = $End + 0.1
        If StringInStr(".", String($End)) Then
            ConsoleWrite($Start & @CRLF)
            $End = $End * 10
            $Start = $Start * 10
        EndIf
        ;While IsInt($End) <> $End ;Mod($End, 1) = 0 ;IsInt(Number($End))
        ;   ConsoleWrite($Start & @CRLF)
        ;   ConsoleWrite($End & @CRLF)
        ;   Sleep(1000)
        ;   $Start = $Start + $MainRate
        ;   $End = $End + $AltRate + 0.1
        ;Wend
        $PossibleValue = $Start & "/" & $End
        _ArrayAdd($PossibleValues, $PossibleValue)
    Next
    _ArrayDisplay($PossibleValues, "")

EndFunc

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $nMsg = $Radio1 And BitAND(GUICtrlRead($Radio1), $GUI_CHECKED) = $GUI_CHECKED
            If GUICtrlRead($OututInput) <> GUICtrlRead($LeftOutputList) Then
                GUICtrlSetData($OututInput, GUICtrlRead($LeftOutputList))
            EndIf
        Case $nMsg = $Radio2 And BitAND(GUICtrlRead($Radio2), $GUI_CHECKED) = $GUI_CHECKED
            If GUICtrlRead($OututInput) <> GUICtrlRead($RightOutputList) Then
                GUICtrlSetData($OututInput, GUICtrlRead($RightOutputList))
            EndIf

        Case $nMsg = $InputListMain
            GUICtrlSetData($Radio1, "Left: " & GUICtrlRead($InputListMain))
        Case $nMsg = $InputListAlt
            GUICtrlSetData($Radio2, "Right: " & GUICtrlRead($InputListAlt))

        Case $nMsg = $MainRateInput
            If $Radio1 = $GUI_CHECKED Then
                GenerateRates($MainRateInput, $AltRateInput)
            Else
                GenerateRates($AltRateInput, $MainRateInput)
            EndIf
        Case $nMsg = $AltRateInput
            If $Radio2 = $GUI_CHECKED Then
                GenerateRates($MainRateInput, $AltRateInput)
            Else
                GenerateRates($AltRateInput, $MainRateInput)
            EndIf
    EndSwitch
WEnd

 

Problem 1: The left & right radio toggles aren't letting me change the output correctly.
- Start off by selecting the options "dog" and "cat".
- If you choose the "left" radio button it is supposed to output "dog" into the output box.
- If you choose the "right" radio button it is supposed to output "cat" into the output box.

The same is also happening for when I change the rates, it's supposed to generate new rates (and as such new content for the option lists) differently. If left radio is selected then the GenerateRates() function should be GenerateRates($MainRateInput, $AltRateInput) and if right radio is selected then it would be GenerateRates($AltRateInput, $MainRateInput)

 

Problem 2: I can't work out how to get the GenerateRates() function to keep adding the original number to the modified number (inside the loop) up until it reaches a whole number.

- In the example of 1 and 3, when it gets 0.1 added onto the 3 the first whole number would be after 10 attempts OR just times it by 10. In this case it would be

I've tried several different options, including some that you can see in the above code. I've even tried:

Func GenerateRates($MainRate, $AltRate)

    Local $Start = $MainRate
    Local $End = $AltRate
    Local $PossibleValues[0] = []
    Local $FloatTrigger = False

    Local $PossibleValue = $Start & "/" & $End
    _ArrayAdd($PossibleValues, $PossibleValue)
    For $i = 1 to 29
        $Start = $Start
        $End = $End + 0.1
        ;If StringInStr(".", String($End)) Then
        ;   ConsoleWrite($Start & @CRLF)
        ;   $End = $End * 10
        ;   $Start = $Start * 10
        ;EndIf

        If IsInt(Number($End)) Then ; IsInt($End) <> $End ; Mod($End, 1) = 0
            $FloatTrigger = True
            $StartEx = $Start
            $EndEx = $End
        Else
            $FloatTrigger = False
        EndIf

        While $FloatTrigger = True
            $StartEx = $StartEx + $MainRate
            $EndEx = $EndEx + $AltRate + 0.1
            ConsoleWrite($StartEx & @CRLF)
            ConsoleWrite($EndEx & @CRLF)
            Sleep(500)
        Wend

        If $FloatTrigger = True Then ;Mod($End, 1) = 0 ;IsInt(Number($End))
            $PossibleValue = $StartEx & "/" & $EndEx
        Else
            $PossibleValue = $Start & "/" & $End
        EndIf

        _ArrayAdd($PossibleValues, $PossibleValue)


    Next
    _ArrayDisplay($PossibleValues, "")

EndFunc


But nothing is working, it's going to be something simple that I've done wrong. Anyone able to help?
 

Link to comment
Share on other sites

3 hours ago, mikell said:

This looks strange

If $Radio1 = $GUI_CHECKED Then
...
If $Radio2 = $GUI_CHECKED Then

 

How so... the case's for radio 1 and radio 2 were found directly on autoit forums (through google) and as for the if, else statements they're fine other than maybe = $GUI_CHECKED being wrong?

Link to comment
Share on other sites

  • Moderators

Hyflex,

Your Switch Cases are wrongly constructed - you do not need the $nMsg = part of each line.

And you need to use GUICtrlRead($Radio#) = $GUI_CHECKED to detect the state of the radios.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

You need to use GUICtrlRead($Radio1) not just the variable.

If GUICtrlRead($Radio1) = $GUI_CHECKED Then
...
If GUICtrlRead($Radio1) = $GUI_CHECKED Then

 

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Hi,

Even with the code looking like you can see below, it still doesn't work.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>


Local $InputListItems[3] = ["item 1", "item 2", "item 3"]
Local $test[1] = ["dog"]
Local $test1[1] = ["cat"]


; #### MAIN FORM ####
$Form1 = GUICreate("Form1", 651, 250, 192, 124)


; ##### INPUT MAIN START #####
$InputMain = GUICtrlCreateGroup("Input Main", 8, 0, 137, 137)

; List
$InputListMain = GUICtrlCreateList("", 16, 16, 121, 118)
GuiCtrlSetData($InputListMain, _ArrayToString($InputListItems))
; ##### INPUT MAIN END #####


; ##### INPUT ALT START #####
$InputAlt = GUICtrlCreateGroup("Input Alt", 151, 0, 137, 137)

; List
$InputListAlt = GUICtrlCreateList("", 159, 16, 121, 118)
GuiCtrlSetData($InputListAlt, _ArrayToString($InputListItems))
; ##### INPUT ALT END #####


; ##### RATES START #####
$Rates = GUICtrlCreateGroup("Rates", 6, 139, 281, 49)
$MainRateInput = GUICtrlCreateInput("1", 14, 155, 121, 21)
$AltRateInput = GUICtrlCreateInput("3", 158, 155, 121, 21)
; ##### RATES END #####


; ##### LEFT OUTPUTS START #####
$LeftOutputs = GUICtrlCreateGroup("Left Option",  296, 0, 169, 185)
$LeftOutputList = GUICtrlCreateList("", 304, 16, 153, 162)
GuiCtrlSetData($LeftOutputList, _ArrayToString($test))
; ##### LEFT OUTPUTS End #####


; ##### RIGHT OUTPUTS START #####
$RightOutputs = GUICtrlCreateGroup("Right Option",  472, 0, 169, 185)
$RightOutputList = GUICtrlCreateList("", 480, 16, 153, 162)
GuiCtrlSetData($RightOutputList, _ArrayToString($test1))
; ##### RIGHT OUTPUTS End #####


; ##### OUTPUT START #####
$Output = GUICtrlCreateGroup("Main Output", 8, 192, 281, 49)
$OututInput = GUICtrlCreateInput("", 16, 208, 265, 21)
; ##### OUTPUT END #####

$Picker = GUICtrlCreateGroup("Option", 294, 192, 345, 49)
$Radio1 = GUICtrlCreateRadio("Left", 312, 216, 113, 17)
$Radio2 = GUICtrlCreateRadio("Right", 488, 216, 113, 17)


GUISetState(@SW_SHOW)
;GUICtrlSetState($Radio1, $GUI_CHECKED)

Func GenerateRates($MainRate, $AltRate)

    Local $Start = $MainRate
    Local $End = $AltRate
    Local $PossibleValues[0] = []
    Local $FloatTrigger = False

    Local $PossibleValue = $Start & "/" & $End
    _ArrayAdd($PossibleValues, $PossibleValue)
    For $i = 1 to 29
        $Start = $Start
        $End = $End + 0.1
        ;If StringInStr(".", String($End)) Then
        ;   ConsoleWrite($Start & @CRLF)
        ;   $End = $End * 10
        ;   $Start = $Start * 10
        ;EndIf

        If IsInt(Number($End)) Then ; IsInt($End) <> $End ; Mod($End, 1) = 0
            $FloatTrigger = True
            $StartEx = $Start
            $EndEx = $End
        Else
            $FloatTrigger = False
        EndIf

        While $FloatTrigger = True
            $StartEx = $StartEx + $MainRate
            $EndEx = $EndEx + $AltRate + 0.1
            ConsoleWrite($StartEx & @CRLF)
            ConsoleWrite($EndEx & @CRLF)
            Sleep(500)
            If IsInt(Number($EndEx)) Then ; IsInt($EndEx) <> $End ; Mod($EndEx, 1) = 0
                $FloatTrigger = True
            Else
                $FloatTrigger = False
            EndIf
        Wend

        If $FloatTrigger = True Then
            $PossibleValue = $StartEx & "/" & $EndEx
        Else
            $PossibleValue = $Start & "/" & $End
        EndIf

        _ArrayAdd($PossibleValues, $PossibleValue)

    Next
    _ArrayDisplay($PossibleValues, "")

EndFunc

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Radio1
            If GUICtrlRead($OututInput) <> GUICtrlRead($LeftOutputList) Then
                GUICtrlSetData($OututInput, GUICtrlRead($LeftOutputList))
            EndIf
        Case $Radio2
            If GUICtrlRead($OututInput) <> GUICtrlRead($RightOutputList) Then
                GUICtrlSetData($OututInput, GUICtrlRead($RightOutputList))
            EndIf
        ; Working Start
        Case $InputListMain
            GUICtrlSetData($Radio1, "Left: " & GUICtrlRead($InputListMain))
        Case $InputListAlt
            GUICtrlSetData($Radio2, "Right: " & GUICtrlRead($InputListAlt))
        ; Working End
        Case $MainRateInput
            If GUICtrlRead($Radio1) = 1 Then
                GenerateRates(GUICtrlRead($MainRateInput), GUICtrlRead($AltRateInput))
            Else
                GenerateRates(GUICtrlRead($AltRateInput), GUICtrlRead($MainRateInput))
            EndIf
        Case $AltRateInput
            If GUICtrlRead($Radio1) = 1 Then
                GenerateRates(GUICtrlRead($MainRateInput), GUICtrlRead($AltRateInput))
            Else
                GenerateRates(GUICtrlRead($AltRateInput), GUICtrlRead($MainRateInput))
            EndIf
    EndSwitch
WEnd

I took the original code for the radios from the AutoIt Help file when searching for GUICtrlCreateRadio as you can see: https://www.autoitscript.com/autoit3/docs/functions/GUICtrlCreateRadio.htm

Edited by Hyflex
Link to comment
Share on other sites

I edited the code above, it's now correct in terms of the radio buttons. The problem with that was I was using code for Select rather than Switch.

As for the integer testing, I am unable to get anywhere on it. The numbers are coming out as floats even though some aren't... 

I've been trying loads of things to try and figure it out but nothing is working :( here is some attempts:

;ConsoleWrite($End & " = Mod: " & Mod($End, 1) & @CRLF)

        ;ConsoleWrite($End & " = Int: " & Int($End) & @CRLF)
        ;ConsoleWrite($End & " = Number: " & Number($End) & @CRLF)
        ;ConsoleWrite($End & " = Round: " & Round($End) & @CRLF)
        ;ConsoleWrite($End & " = Floor: " & Floor($End) & @CRLF)
        ;ConsoleWrite($End & " = Ceiling: " & Ceiling($End) & @CRLF)

        ;ConsoleWrite($End & " = IsInt: " & IsInt($End) & @CRLF)
        ;ConsoleWrite($End & " = IsFloat: " & IsFloat($End) & @CRLF)
        ;ConsoleWrite($End & " = IsNumber: " & IsNumber($End) & @CRLF)
        ;ConsoleWrite($End & " = IsInt(Number()): " & IsInt(Number($End)) & @CRLF)
        ;ConsoleWrite($End & " = Floor(Mod()): " & Floor(Mod($End, 1)) & @CRLF)

        ;ConsoleWrite($End & " = StringInStr: " & StringInStr(".", String($End)) & @CRLF)

        ConsoleWrite(_WinAPI_FloatToInt($End) & @CRLF)

        If IsFloat($End) = 1 Then
            ConsoleWrite($End & " = Float" & @CRLF)
        ElseIf IsFloat($End) = 0 Then
            ConsoleWrite($End & " = Not Float" & @CRLF)
        EndIf

        ;If Mod($End, 1) = 1 Then
            ;ConsoleWrite($End & " = Mod = 1:: " & Mod($End, 1) & @CRLF)
        ;ElseIf Mod($End, 1) = 0 Then
            ;ConsoleWrite($End & " = Mod = 0: " & Mod($End, 1) & @CRLF)
        ;EndIf



 

Link to comment
Share on other sites

2 minutes ago, mikell said:

You can exit from this loop

While $FloatTrigger = True

only if $FloatTrigger = False, so the following  "If $FloatTrigger = True Then..."  check has no sense

I confess that I don't really understand what you are trying to achieve  :sweating:

My code already has that, It's more of the problem that it's not detecting ints correctly. :/

Link to comment
Share on other sites

Hmmm

For $i = 1 to 29
        $Start = $Start
        $End = Round($End + 0.1, 1)  ;<<<<<<<<<<<<<<<<<<<
        ;If StringInStr(".", String($End)) Then
        ;   ConsoleWrite($Start & @CRLF)
        ;   $End = $End * 10
        ;   $Start = $Start * 10
        ;EndIf

        If IsInt(Number($End)) Then ; IsInt($End) <> $End ; Mod($End, 1) = 0
ConsoleWrite($End & " is int" & @CRLF)

 

Link to comment
Share on other sites

Ahh that helps a lot! 

Am I right in thinking there isn't an option like: GUICtrlSetState($Radio1, $GUI_CHECKED) for lists?

I want to select item 1 (whatever is top of the list) in both input main and input alt.

Do I have to use the following?

ControlClick("", "", "[CLASS:ListBox; INSTANCE:2]", "left", 1, 20, 10)

 

Edited by Hyflex
Link to comment
Share on other sites

You can set the selected item with GuiCtrlSetData. As per its help file:

Quote

For Combo or List control :
If the "data" corresponds to an already existing entry it is set as the default.

Means you have to read or know the data. So if you want to use an array for that data, you'll have to search the array for the index of the intended data and then set that data as the default.

Fun little example:

#include <Array.au3>

HotKeySet("{ESC}", "exiter")

GUICreate("test")

Local $InputListItems[4] = ["item 1", "item 2", "item 3", "item 4"]

$InputListMain = GUICtrlCreateList("", 16, 16, 121, 118)
GUICtrlSetData($InputListMain, _ArrayToString($InputListItems), $InputListItems[1]) ; <-- set second item as default value

GUISetState(@SW_SHOW)

While True
    Sleep(1000)
    GUICtrlSetData($InputListMain, $InputListItems[Mod(_ArraySearch($InputListItems, GUICtrlRead($InputListMain)) + 1, UBound($InputListItems))])

    ; or, easier to read written out:
    ;~  $selectedValue = GUICtrlRead($InputListMain)
    ;~  $selectedIndex = _ArraySearch($InputListItems, $selectedValue) + 1
    ;~  if $selectedIndex >= UBound($InputListItems) then $selectedIndex = 0
    ;~  GUICtrlSetData($InputListMain, $InputListItems[$selectedIndex])
WEnd

Func exiter()
    Exit
EndFunc   ;==>exiter

 

Roses are FF0000, violets are 0000FF... All my base are belong to you.

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