Switch:Case - Up to 25?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
V0RT3X
Posts: 362
Joined: 20 May 2023, 21:59
Contact:

Switch:Case - Up to 25?

Post by V0RT3X » 04 Jun 2023, 07:48

Hi folks, I just need a little guidance here with the Switch command. I like the way it uses a single hotkey to cycle through a list of commands, but I cannot figure out how to go above 4 Cases and I'm trying to cycle through 25 cases. I'm pretty sure it's just the math behind this, but I'm lost on how to get there.

Code: Select all

#SingleInstance, Force
#Persistent

^T:: 

; Switch ++N:=0 N&3 ; N = 1,2,3,4,1... (4 Cases only)
; Switch ++N:=0 N&24 ; N = 1,2,3-24,1...
; Switch (N - 1) % 25 + 1
Switch ++N:=0+1

{
    Case 1: SplashTextOn,,, Displays a 1st title bar.
                      Soundbeep, 1400, 150
                  SplashTextOff

    Case 2: SplashTextOn,,, Displays a 2nd title bar.
                      Soundbeep, 1450, 150
                  SplashTextOff

    Case 3: SplashTextOn,,, Displays a 3rd title bar.
                      Soundbeep, 1500, 150
                  SplashTextOff

    Case 4: SplashTextOn,,, Displays 4th... Next press goes back to 1st.  ; NEEDS TO CONTINUE ON FROM HERE...
                      Soundbeep, 1550, 150
                  SplashTextOff

    Case 5: SplashTextOn,,, Displays a 5th title bar.
                      Soundbeep, 1600, 150
                  SplashTextOff

    Case 6: SplashTextOn,,, Displays a 6th title bar - and so on...
                      Soundbeep, 1650, 150
                  SplashTextOff

}
Return

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

Re: Switch:Case - Up to 25?

Post by mikeyww » 04 Jun 2023, 07:57

Code: Select all

#Requires AutoHotkey v1.1.33
bar := ["Bar1", "Bar2", "Bar3", "Bar4", "Bar5"]
max := bar.Length()

^t::
n := n++ - max ? n : 1
SoundBeep 1350 + 50 * n
MsgBox % bar[n]
Return

User avatar
V0RT3X
Posts: 362
Joined: 20 May 2023, 21:59
Contact:

Re: Switch:Case - Up to 25?

Post by V0RT3X » 04 Jun 2023, 08:40

Thank you kind sir.

I am not sure what to make of your script though as far as how to implement it. My end goal here is to make a DLL viewer that I can just press the hotkey and cycle through the different DLLs.
What I have works great, but I was only able to get the Switch command to cycle through 4 different cases before it would cycle back to the 1st using script like the below example.
I do like how you incorporated my soundbeeps though, even if they were just for testing to make sure it was a different case being sent. It's almost musical! lol

Code: Select all

Switch ++N:=0 N&3 ; N = 1,2,3,4,1...

{
    Case 1: DllPath := "C:\WINDOWS\SYSTEM32\accessibilitycpl.dll" 
Gui, Margin, 0, 0
Gui, +AlwaysOnTop -MinimizeBox -DPIScale +hwndGuiHwnd
DllCall("uxtheme\SetWindowThemeAttribute", "ptr", GuiHwnd, "int", 1, "int64*", 6 | 6<<32, "uint", 8)
Gui, Add, Button, w300 h50 gIconSizeToggle +Center, Toggle Icon Size
Gui, Add, ListView, w300 h550 vListView +hwndListViewHwnd -0x4000 +LV0x10000 +0x8000, ACCESSIBILITYCPL
DllCall("UxTheme.dll\SetWindowTheme", "Ptr", ListViewHwnd, "WStr", "Explorer", "Ptr", 0)
LV_ModifyCol(1,279)
GoSub, IconSizeToggle
Gui Show, h600 w300, Dll icons
return

    Case 2: DllPath := "C:\WINDOWS\SYSTEM32\compstui.dll" 
Gui, Margin, 0, 0
Gui, +AlwaysOnTop -MinimizeBox -DPIScale +hwndGuiHwnd
DllCall("uxtheme\SetWindowThemeAttribute", "ptr", GuiHwnd, "int", 1, "int64*", 6 | 6<<32, "uint", 8)
Gui, Add, Button, w300 h50 gIconSizeToggle +Center, Toggle Icon Size
Gui, Add, ListView, w300 h550 vListView +hwndListViewHwnd -0x4000 +LV0x10000 +0x8000, COMPSTUI.DLL 
DllCall("UxTheme.dll\SetWindowTheme", "Ptr", ListViewHwnd, "WStr", "Explorer", "Ptr", 0)
LV_ModifyCol(1,279)
GoSub, IconSizeToggle
Gui Show, h600 w300, Dll icons
return

    Case 3:   ; AND SO ON...
 
 ; ----------------------   

IconSizeToggle:
IconSize := !IconSize
GuiControl, -Redraw, ListView
LV_Delete()
if (IconSize)
	ImageListID := IL_Create(1, 1, 1)
else
	ImageListID := IL_Create()
LV_SetImageList(ImageListID, 1)  ; Assign the above ImageList to the current ListView.

Loop { ; Populate the listview
	Check := IL_Add(ImageListID, DllPath, A_Index)
	if (Check != 0) {
		LV_Add("Icon" . A_Index, A_Index)
	}
	else
		Break
}
GuiControl, +Redraw, ListView
Return

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

Re: Switch:Case - Up to 25?

Post by mikeyww » 04 Jun 2023, 08:42

Line 6 in my script shows how you can use a different maximum number. Set the max to whatever you need. You can then use Switch with n.

User avatar
V0RT3X
Posts: 362
Joined: 20 May 2023, 21:59
Contact:

Re: Switch:Case - Up to 25?

Post by V0RT3X » 04 Jun 2023, 08:57

My little testing demonstrated how your script counts and continues well beyond the limit of 4 I'm having, but I do not understand how to use it to implement the various DLLs into the different "Bars", and looks like if I just try to swap out your message box it would only run a single DLL? I don't get why this is causing so much frustration. Seems like it would just be a different formula than ++N:=0 N&3, but I don't have a clue what it would be. Or as is usually the case, I just simply don't understand the Switch command as much as I thought I did.

image.png
image.png (44.01 KiB) Viewed 999 times

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

Re: Switch:Case - Up to 25?

Post by mikeyww » 04 Jun 2023, 09:57

I provided the formula and also provided its line number in my previous post.

An example that you can adjust is below.

Code: Select all

#Requires AutoHotkey v1.1.33
max := 25

^t::
n := n++ - max ? n : 1
SoundBeep 1350 + 50 * n
Switch n {
 Case 1:
  MsgBox DLL xyz
 Case 2:
  MsgBox DLL abc
 ; Etc.
}
Return
As usual, the Return command will cause your hotkey subroutine to end. If you do not want that to happen, then do not use that command. To understand your script, follow it line by line.

For many scripts that do not need to destroy a GUI, the GUI can be built at the script's start, instead of in a subroutine.

Start with a short and simple script to get your hotkey and switch working. After they work, you can expand your script.

User avatar
V0RT3X
Posts: 362
Joined: 20 May 2023, 21:59
Contact:

Re: Switch:Case - Up to 25?

Post by V0RT3X » 04 Jun 2023, 12:17

I attempted to use your example by plugging in a couple of the Cases, but it doesn't cycle past the first one. I know that your example goes well beyond the 4 Cases I had to begin with so I'm confident all my problem comes after your clever use of the soundbeep. I just don't see it or lack the comprehension at this point.
And I think I understand what you mean by building the Gui at the start of the script, but I have no idea how I would do that while including the DLL calls.
Any chance you can stitch together the 2 Case examples I have below? As you can see, I attempted to blow up (spread out) the first case trying to figure it out.

Code: Select all

#SingleInstance, Force
#Persistent
  max := 25
^T:: 

n := n++ - max ? n : 1
SoundBeep 1350 + 50 * n

Switch n { 

 Case 1: 
DllPath := "C:\WINDOWS\SYSTEM32\accessibilitycpl.dll" 	 ; ACCESSIBILITYCPL.DLL

Gui, Margin, 0, 0
Gui, +AlwaysOnTop -MinimizeBox -DPIScale +hwndGuiHwnd

;  DllCall("uxtheme\SetWindowThemeAttribute", "ptr", GuiHwnd, "int", 1, "int64*", 6 | 6<<32, "uint", 8) ; ←←← HOW TO INCLUDE IN GUI? ←←← 

Gui, Add, Button, w300 h50 gIconSizeToggle +Center, Toggle Icon Size
Gui, Add, ListView, w300 h550 vListView +hwndListViewHwnd -0x4000 +LV0x10000 +0x8000, ACCESSIBILITYCPL.DLL

;  DllCall("UxTheme.dll\SetWindowTheme", "Ptr", ListViewHwnd, "WStr", "Explorer", "Ptr", 0) ; ←←← HOW TO INCLUDE IN GUI? ←←← 

LV_ModifyCol(1,279)

GoSub, IconSizeToggle

Gui Show, h600 w300, Dll icons

; Return


    Case 2: 
DllPath := "C:\WINDOWS\SYSTEM32\compstui.dll" 	 ; COMPSTUI.DLL

Gui, Margin, 0, 0
Gui, +AlwaysOnTop -MinimizeBox -DPIScale +hwndGuiHwnd
DllCall("uxtheme\SetWindowThemeAttribute", "ptr", GuiHwnd, "int", 1, "int64*", 6 | 6<<32, "uint", 8)
Gui, Add, Button, w300 h50 gIconSizeToggle +Center, Toggle Icon Size
Gui, Add, ListView, w300 h550 vListView +hwndListViewHwnd -0x4000 +LV0x10000 +0x8000, COMPSTUI.DLL
Gui, Add, ListView, w300 h550 , COMPSTUI.DLL
DllCall("UxTheme.dll\SetWindowTheme", "Ptr", ListViewHwnd, "WStr", "Explorer", "Ptr", 0) 
LV_ModifyCol(1,279)
GoSub, IconSizeToggle
Gui Show, h600 w300, Dll icons

; Return

}

; ╞════════════════  ICON SIZE TOGGLE ═══════════════╡ 

IconSizeToggle:
IconSize := !IconSize
GuiControl, -Redraw, ListView
LV_Delete()
if (IconSize)
	ImageListID := IL_Create(1, 1, 1)
else
	ImageListID := IL_Create()
LV_SetImageList(ImageListID, 1)  ; Assign the above ImageList to the current ListView.

Loop { ; Populate the listview
	Check := IL_Add(ImageListID, DllPath, A_Index)
	if (Check != 0) {
		LV_Add("Icon" . A_Index, A_Index)
	}
	else
		Break
}
GuiControl, +Redraw, ListView
Return

Also, I get this error when running this as is. But the original way that I was able to cycle through the 1st for Cases it worked correctly, as in going through each Case with no error. Just wouldn't go beyond the 4 Cases.
I really don't understand that since it's almost exactly the same thing, at least to my untrained eye. Thank you very much for all your help thus far!!

image.png
image.png (26.27 KiB) Viewed 943 times

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

Re: Switch:Case - Up to 25?

Post by mikeyww » 04 Jun 2023, 12:37

My first question is whether my script works as is, with no changes. We can debug after that.

Here is the overall approach again.

Code: Select all

#Requires AutoHotkey v1.1.33
max := 5
Gui Font, s10
Gui Add, Text, w230 vtxt

^t::
n := n++ - max ? n : 1
Gosub Go
Gui Show, x300, Test
Switch n {
 Case 1:
  MsgBox 1
 Case 2:
  MsgBox 2
 Case 3:
  MsgBox 3
 Case 4:
  MsgBox 4
 Case 5:
  MsgBox 5
}
Return

Go:
GuiControl,, txt, % n
Return
If this script works, then whatever code you add beyond it has nothing to do with the Switch statement directly, but lies in your other code.

What the script does:
Uses a single hotkey to cycle through a list of commands
If you build your GUI once instead of adding controls every time you trigger the hotkey, you can avoid the bug that you have cited. The script here shows an example of how to do that.

User avatar
V0RT3X
Posts: 362
Joined: 20 May 2023, 21:59
Contact:

Re: Switch:Case - Up to 25?

Post by V0RT3X » 04 Jun 2023, 15:08

Thank you for that example. I have it sort of working, but as you can see I'm still having issues with this.
These first images are how it appears with my original 4 Case script, showing the first 2 DLL names and icons, along with icons sizes toggled.

4-Case.png
4-Case.png (70.96 KiB) Viewed 918 times
These other images show that my attempted modification of your example does indeed process through all 5 Cases (with different cases to visually verify it was actually showing 5 different cases), but displays numbers rather than the DLL names no icons.

Multi-Case.png
Multi-Case.png (47.46 KiB) Viewed 918 times
Any further suggestions based on what I have so far?

Code: Select all

max := 5 		 ;  ←←← Set Case Load Limit

 ; --------- Gui Assembly -------------  
Gui, Margin, 0, 0
Gui,  -MinimizeBox -DPIScale +hwndGuiHwnd
Gui Font, s10
Gui Add, Text, w230 vtxt

DllCall("uxtheme\SetWindowThemeAttribute", "ptr", GuiHwnd, "int", 1, "int64*", 6 | 6<<32, "uint", 8)

Gui, Add, Button, w300 h50 gIconSizeToggle +Center, Toggle Icon Size
Gui, Add, ListView, w300 h550 vListView +hwndListViewHwnd -0x4000 +LV0x10000 +0x8000,

DllCall("UxTheme.dll\SetWindowTheme", "Ptr", ListViewHwnd, "WStr", "Explorer", "Ptr", 0) 
LV_ModifyCol(1,279)
 ; --------- Gui Assembly -------------  

^t:: 

n := n++ - max ? n : 1
Gosub Go
Gui Show, x700, Test 	 ; 300 changed to 700 temporarily..
 ; ----------------------   

Switch n {

    Case 1:
        DllPath := "C:\WINDOWS\SYSTEM32\accessibilitycpl.dll" ; ←←← ACCESSIBILITYCPL.DLL

; GoSub, GUI

GoSub, IconSizeToggle

;Gui Show, h600 w300, Dll icons

 ; ----------------------   
     Case 2: 
        DllPath := "C:\WINDOWS\SYSTEM32\compstui.dll" ; ←←← COMPSTUI.DLL
            Soundbeep, 1400, 100

GoSub, IconSizeToggle

 ; ----------------------   
    Case 3:
        Soundbeep, 1900, 100

 ; ----------------------   
    Case 4:
        MsgBox 4

 ; ----------------------   
    Case 5:
        SplashTextOn,,, Displaying SplashText.
            Soundbeep, 1400, 150
            Sleep, 1500
        SplashTextOff
}
Return

; ╞════════════════  

Go:
GuiControl,, txt, % n
Return

 ; ---------------------- 

IconSizeToggle:
IconSize := !IconSize
GuiControl, -Redraw, ListView
LV_Delete()
if (IconSize)
	ImageListID := IL_Create(1, 1, 1)
else
	ImageListID := IL_Create()
LV_SetImageList(ImageListID, 1)  ; Assign the above ImageList to the current ListView.

Loop { ; Populate the listview
	Check := IL_Add(ImageListID, DllPath, A_Index)
	if (Check != 0) {
		LV_Add("Icon" . A_Index, A_Index)
	}
	else
		Break
}

GuiControl, +Redraw, ListView
Return

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

Re: Switch:Case - Up to 25?

Post by mikeyww » 04 Jun 2023, 16:12

When n = 4, the statements under Case 4 will execute. Get it? Statements under each Case statement are independent of the other cases.

Suggestions for your posts:
1. When you post your initial script, indicate what the script should do.
2. When you see a posted script, run it the way it is, and report whether it works.

These two steps can decrease the time to resolution of the problem.

User avatar
V0RT3X
Posts: 362
Joined: 20 May 2023, 21:59
Contact:

Re: Switch:Case - Up to 25?

Post by V0RT3X » 04 Jun 2023, 18:51

Turns out it was as simple as I originally had hoped and literally it was just the math as demonstrated in my first post trying to figure out the formula. It was max := 25 and n := n++ - max ? n : 1 that turned out to be all I needed.
Not sure what the
bar := ["Bar1", "Bar2", "Bar3", "Bar4", "Bar5"]
max := bar.Length()
  or
For many scripts that do not need to destroy a GUI, the GUI can be built at the script's start, instead of in a subroutine.
    items were thrown in for, but weren't needed and I think confusing me. I can now cycle through all the DLL icons however and retrieve their numbers and what they appear as in different sizes.
Thank you for your time!

User avatar
boiler
Posts: 17706
Joined: 21 Dec 2014, 02:44

Re: Switch:Case - Up to 25?

Post by boiler » 04 Jun 2023, 21:57

V0RT3X wrote:
Not sure what the
bar := ["Bar1", "Bar2", "Bar3", "Bar4", "Bar5"]
max := bar.Length()
  or
For many scripts that do not need to destroy a GUI, the GUI can be built at the script's start, instead of in a subroutine.
    items were thrown in for, but weren't needed and I think confusing me.
To try show you better approaches to coding. Just because you were confused by them doesn’t mean there wasn’t a good reason that they were “thrown in.” If you want to learn to code better, you should take the time to learn why they were suggested.

User avatar
V0RT3X
Posts: 362
Joined: 20 May 2023, 21:59
Contact:

Re: Switch:Case - Up to 25?

Post by V0RT3X » 05 Jun 2023, 08:20

And that I can understand. But I had already answered his questions...
1. When you post your initial script, indicate what the script should do.
Pretty well stated in first post with... sure it's just the math behind this...
and...
; Switch ++N:=0 N&3 ; N = 1,2,3,4,1... (4 Cases only)
; Switch ++N:=0 N&24 ; N = 1,2,3-24,1...
; Switch (N - 1) % 25 + 1
Switch ++N:=0+1

Not sure how that can not be understood, but okay.
2. When you see a posted script, run it the way it is, and report whether it works.
..But the original way that I was able to cycle through...

Not trying to be disrespectful. Simply not seeing how I wasn't giving what was asked vs what was being given in return.
While the math was given in the first response... n := n++ - max ? n : 1 ... it also had max := bar.Length() in that same post. Not the max := 25 that was actually needed that was added in their third response. And I haven't been reading code for the last decade+ so while that stuff may jump right out at those that have, those of us just trying to get started are not mind readers so without explicitly stating that we are trying to be shown a way other than what we asked doesn't really help a new guy. I thought my initial post was clear that I just didn't understand the math. I will try making things more crystal going forward.

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

Re: Switch:Case - Up to 25?

Post by mikeyww » 05 Jun 2023, 09:28

Hmm. Miscommunications. I was providing a demo to see if the approach worked. Glad at least you have it working.

Forum responders sometimes offer new ideas about achieving a goal. Sometimes helpful, sometimes not, I guess. I encourage readers to try things they see. In some cases, improvements can be made, or a new technique can be learned. At the least, we can gain shared knowledge about whether it works. I admit that I don't provide as many comments as I could.

Post Reply

Return to “Ask for Help (v1)”