Disable Editbox with the chosen DDL variable Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jlleva
Posts: 20
Joined: 01 Jan 2022, 07:48

Disable Editbox with the chosen DDL variable

Post by jlleva » 29 Nov 2023, 18:10

Hi thanks for viewing.

I think it's possible i just don't have any idea how to do it.

I want to disable the editbox (vMyName) and the dropdown (vThemName) as soon as I select the Banana from the 2nd dropdown (vFruits).

My script only disables them after i press the Send button.

Sorry for my script it's messy. If you have suggested workarounds that can simplify my code, It would really help me a lot.

TIA

Code: Select all

Name = Aron|Billy|Caitlyn|Donna


Gui, Add, Text, x25 y37, Name
Gui, Add, Edit, x+5 y35 w90 vMyName,
Gui, Add, Button, x+10 y34 gSend, Send
Gui, Add, Text, x25 y76, Name
Gui, Add, Text, x38 y109, Fruits
Gui, Add, DropdownList, x75 y71 w120 vThemName, %Name%
Gui, Add, DropdownList, x75 y103 w120 vFruits, Apple|Banana|Mango|Orange
Gui, Show
return

Send:
Gui, Submit, NoHide

var1 := % MyName
var1 = %var1%


if Fruits = Apple
{
 if ( MyName = "" )
{
SoundPlay *-1
MsgBox, 4096, ERROR, Name field is empty.
return
}

MsgBox, My name is %MyName%`nHis name is %ThemName%`nI love eating Apple
}

if Fruits = Banana
{
GuiControl, Disable, MyName
GuiControl, Disable, ThemName
MsgBox, I love eating Banana
}
return

User avatar
mikeyww
Posts: 27165
Joined: 09 Sep 2014, 18:38

Re: Disable Editbox with the chosen DDL variable  Topic is solved

Post by mikeyww » 29 Nov 2023, 21:17

Use a g-label. An example is on line 6 of your script.

Code: Select all

#Requires AutoHotkey v1.1.33
Gui Font, s10
Gui Add, Text  , w50  y11, Name:
Gui Add, Text  , wp      , Name:
Gui Add, Text  , wp      , Fruits:
Gui Add, Edit  , w150 ym   vmyName   gCheck
Gui Add, DDL   , wp   y+9  vthemName gCheck  , Aron|Billy|Caitlyn|Donna
Gui Add, DDL   , wp   y+9  vfruit    gCheck  , Apple|Banana|Mango|Orange
Gui Add, Button, w214 xm   Default   Disabled, Send
Gosub F3

F3::Gui Show, x1200, Names

Check:
Gui Submit, NoHide
GuiControl % myName = "" || themName = "" || fruit = "" ? "Disable" : "Enable", Send
state := fruit = "Banana" ? "Disable" : "Enable"
GuiControl % state, myName
GuiControl % state, themName
Return

ButtonSend:
Gui Submit
MsgBox 64, Result, % "My name    : " myName "`nThem name: " themName "`nFruit           : " fruit
Return

User avatar
jlleva
Posts: 20
Joined: 01 Jan 2022, 07:48

Re: Disable Editbox with the chosen DDL variable

Post by jlleva » 29 Nov 2023, 23:02

mikeyww wrote:
29 Nov 2023, 21:17
Use a g-label. An example is on line 6 of your script.
Hi mikeyww, thank you for your time writing this code. the gui is clean and simple. the code itself is a bit too advance for me :lol: but this sample will help me a lot.
again, Thank you very much! :cookie:

LymeRhyme
Posts: 1
Joined: 02 Dec 2023, 11:44

Re: Disable Editbox with the chosen DDL variable

Post by LymeRhyme » 02 Dec 2023, 12:11

Switching to v2 made such a huge difference for me when it comes to creating Guis. I find it much cleaner looking and easier to read. I'm learning by searching for Gui questions/requests. Here is yours in v2.

Code: Select all

#Requires Autohotkey v2 
#SingleInstance Force ; prevents an error message if you run it again

Names := ["Aron", "Billy", "Caitlyn", "Donna"] ; create an array of names

Fruits := ["Apple", "Banana", "Mango", "Orange"] ; create an array of fruits

; Create the Gui ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

bananaGui := Gui() ; Call the Gui object
bananaGui.Title := "Fruits and Hoes" ; name the window
bananaGui.AddText(, "Name:") ; add column of text controls
bananaGui.AddText(, "Name:")
bananaGui.AddText(, "Fruits:")

EditName := bananaGui.AddEdit("ym vMyName") ; start a new column with ym - notice these controls seem to have 2 names - this will come into play later
DropDownListNames := bananaGui.AddDropDownList("vThemName", Names) ; create dropdownlists
DropDownListFruits := bananaGui.AddDropDownList("vFruits", Fruits)

ButtonSubmit := bananaGui.AddButton(, "Submit") ; create the submit button

; Create Gui Events ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

DropDownListFruits.OnEvent("Change", FruitsChangeHandler) ; calls the FruitsChangeHandler function whenever a fruit is chosen
ButtonSubmit.OnEvent("Click", SubmitHandler) ; the event for a button is a click
bananaGui.OnEvent("Close", (*) => ExitApp()) ; this event is for the Gui itself, not a control - terminates the script instead of leaving it running hidden

; Gui Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

bananaGui.Show ; make Gui visible

; Define your functions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

FruitsChangeHandler(*)
{
    if(DropDownListFruits.Text = "banana") ; disable the other fields when banana is chosen
        {
            EditName.Enabled := 0
            DropDownListNames.Enabled := 0
        }
        else ; without an else statement the other fields are not reenabled when you choose a different fruit
            {
                EditName.Enabled := 1
                DropDownListNames.Enabled := 1
            }
}

SubmitHandler(*)
{
    if(EditName.Text = "")
        {
            MsgBox("Enter a name to continue", "Error!", "iconx")
            return
        }
    
    Saved := bananaGui.Submit() ; once the Gui is submitted, you start using the vNames

    MsgBox "My name is " Saved.MyName "`n" ; sends a message box with a summary of the field values
    . "His name is " Saved.ThemName "`n"
    . "I love eating " Saved.Fruits
}
This second code is a demonstration of as many guicontrol techniques as I could fit and does a lot more. It may look like a lot but it is just explanation and example of various Gui control techniques. You should also check out EasyAuto Gui if you are interested. It basically makes Guis drag and drop.

Code: Select all

#Requires Autohotkey v2 
#SingleInstance Force ; prevents an error message if you run it again

Names := ["Aron", "Billy", "Caitlyn", "Donna"] ; create an array of names

Fruits := ["Apple", "Banana", "Mango", "Orange"] ; create an array of fruits

; Create the Gui ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

bananaGui := Gui() ; Call the Gui object
bananaGui.Title := "Fruits and Hoes" ; name the window
bananaGui.AddText(, "Name:") ; add column of text controls
bananaGui.AddText(, "Name:")
bananaGui.AddText(, "Fruits:")

EditName := bananaGui.AddEdit("ym vMyName") ; start a new column with ym - notice these controls seem to have 2 names - this will come into play later
DropDownListNames := bananaGui.AddDropDownList("vThemName", Names) ; create dropdownlists
DropDownListFruits := bananaGui.AddDropDownList("vFruits", Fruits)

ButtonSubmit := bananaGui.AddButton(, "Submit") ; create the submit button

bananaGui.AddText("x5","") ; create a hint section at the bottom - use x5 to align it left instead of with second column - blank text for spacer
bananaGui.AddText("x5","Want to add your own fruit?")
bananaGui.AddText("x5","Hint: try typing 'secret' in the name field")
bananaGui.AddText("x5","") 
bananaGui.AddText("x5","Can you unlock the special StarFruit?") 
bananaGui.AddText("x5","Hint: check the code!") 

; Create events to call a different function when each field is changed ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

EditName.OnEvent("Change", NameHandler) ; calls the function NameHandler whenever there is a change to the name edit box
DropDownListNames.OnEvent("Change", NamesChangeHandler) 
DropDownListFruits.OnEvent("Change", FruitsChangeHandler)
ButtonSubmit.OnEvent("Click", SubmitHandler) ; the event for a button is a click
bananaGui.OnEvent("Close", (*) => ExitApp()) ; this event is for the Gui itself, not a control - terminates the script instead of leaving it running hidden

; Gui Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

bananaGui.Show ; make Gui visible

; Define your functions for each field ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

NameHandler(*)
{
    ; some example if statements for this field
    ; note not using the vName but the named variable: .Text gets you the text and .Value the index for DDL control
    ; this function allows the user to enter a custom, "secret" fruit when the name is "secret"
    ; it then adds the fruit to the dropdownlist - with special handling for "banana"

    if(EditName.Text = "Secret")
        {
            Input := InputBox("Enter your special fruit`n...not banana please!", "Special Fruit") ; prompt user to enter a custom fruit
            if(Input.Value = "Banana")
                {
                    MsgBox("How dare you!?") ; if they try to enter bananas send this message box
                    DropDownListFruits.Text := "banana"
                    DropDownListFruits.Delete(DropDownListFruits.Value) ; conspicuously deletes banana from the options when hovered over
                    DropDownListFruits.Add(["Looking for something?"]) ; otherwise add their fruit to the dropdownlist
                }
                else
                    {
                        DropDownListFruits.Add([Input.Value]) ; otherwise add their fruit to the dropdownlist
                        DropDownListFruits.Text := Input.Value
                    }
        }
    
    if(EditName.Text = "Sorry!") ;
        {
            try
                {
                    DropDownListFruits.Text := "banana" ; if banana is in the list, changes option to banana -> otherwise throws an error
                    MsgBox("Cheer up!`n`nHave a banana!", "Yayy bananas!!") ; add a title to this msgbox
                }
                catch ; catches the error if thrown
                    {
                        MsgBox "And let's hope you mean it too!" ; you really better be sorry
                        DropDownListFruits.Text := "Looking for something?" ; sets dropdownlist to "looking for something"
                        DropDownListFruits.Delete(DropDownListFruits.Value) ; deletes "looking for something" after setting it
                        DropDownListFruits.Add(["Banana"]) ; adds banana back to the list
                        DropDownListFruits.Text := "banana"
                    }
        } 

}

NamesChangeHandler(*)
{
    ; Some example if statements for each value in this field

    if(DropDownListNames.Text = "Aron")
        {
            MsgBox('Here you dropped this:`n`n`t"a"', "A-A-R-O-N", "icon!") ; add a title and an icon to this msgbox
            DropDownListNames.Delete(DropDownListNames.Value) ; have to use the index for delete - .value
            DropDownListNames.Add(["Aaron"])
            DropDownListNames.Text := "Aaron"
        }

    if(DropDownListNames.Text = "Donna")
        {
            Result := MsgBox("Donna won't you blow your horn!?", "Feeling Horny?", "YesNo icon?") ; add title, icon and yes/no options to this msgbox
            if(Result = "Yes")
                {
                    SoundPlay "*48" ; exclamation sound
                }
            if(Result := "No")
                {
                    SoundPlay "*16" ; error sound
                }
        }
    
    if(DropDownListNames.Text = "Billy")
        {
            MsgBox("You again!?",, "iconx")
        }

    if(DropDownListNames.Text = "Caitlyn")
        {
            for value in Fruits ; uses a loop to guess each fruit
                {
                    Result := MsgBox(value "?", "Let me guess?", "YesNo")
                    if(Result = "No") ; continue to next value if result is no
                        {
                            continue
                        }
                    if(Result = "Yes") ; stop the loop if the result is yes
                        {
                            DropDownListFruits.Text := Value ; sets dropdownlist to chosen value
                            if(value = "banana")
                                {
                                    MsgBox("Banana nanna nanna... fee fie foe fanna", "It's Always Banana!")
                                }
                            break
                        }
                }
        }
}

FruitsChangeHandler(*)
{
    ; Some example if statements for this field

    if(DropDownListFruits.Text = "orange" and EditName.Text = "") ;  if name is blank, changes names when orange is chosen
        {
            EditName.Text := "Donald Trump"
            DropDownListNames.Add(["Vladamir Putin"])
            DropDownListNames.Text := "Vladamir Putin"
            MsgBox("Who coulda seen that comin'?", "World War 3")
        }

    if(DropDownListFruits.Text = "apple" and EditName.Text = "") ;  same for apples
        {
            EditName.Text := "Adam"
            DropDownListNames.Add(["Eve"])
            DropDownListNames.Text := "Eve"
            MsgBox("You were warned!", "Adam & Eve")
        }
    
    if(DropDownListFruits.Text = "banana") ; disable the other fields when banana is chosen
        {
            EditName.Enabled := 0
            DropDownListNames.Enabled := 0
        }
        else ; without an else statement the other fields are not reenabled when you choose a different fruit
            {
                EditName.Enabled := 1
                DropDownListNames.Enabled := 1
            }

    if(DropDownListFruits.Text = "mango" and EditName.Text = "secret") ;  same for apples
        {
            EditName.Text := "Adam"
            DropDownListFruits.Add(["StarFruit"])
            DropDownListFruits.Text := "StarFruit"
            MsgBox("You've unlocked the StarFruit!", "Congratulations!")
        }

    if(DropDownListFruits.Text = "Looking for something?") ; prompt for apology
        {
            MsgBox("Feeling sorry... want a banana?`n`nHINT: type sorry! in name field", "Congratulations!")
        }
}

SubmitHandler(*)
{
    if(EditName.Text = "")
        {
            MsgBox("Enter a name to continue", "Error!", "iconx")
            return
        }
    
    Saved := bananaGui.Submit() ; once the Gui is submitted, you start using the vNames

    MsgBox "My name is " Saved.MyName "`n" ; sends a message box with a summary of the field values
    . "His name is " Saved.ThemName "`n"
    . "I love eating " Saved.Fruits

    for name, value in Saved.OwnProps() ; or you can access the names and values one at a time using .OwnProps
        {
            MsgBox(name ": " value)
        }
    
    If(Saved.Fruits = "banana")
        {
            FileName := A_Desktop "\Grocery List.txt" ; if the chose fruit is banana, creates a shopping list on the desktop
            FileObj := FileOpen(FileName, "w")
            FileObj.WriteLine("Grocery List:")
            loop 10
                {
                    FileObj.WriteLine("banana")
                }
            FileObj.Close
        }
}

Post Reply

Return to “Ask for Help (v1)”