Refer to custom button using its name instead of its index

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
John_C
Posts: 336
Joined: 16 May 2018 20:04

Refer to custom button using its name instead of its index

Post by John_C »

I have three custom buttons on the toolbar to switch between "Details", "Small Icons" and "Thumbnails #1" views.

Button 1 code:

Code: Select all

"Toggle Details View"
    $mode = get("view");
    if ($mode != 0) {
        #302;
        ctbstate(1);
        ctbstate(0, 2); // Turns off the "Small Icons" button.
        ctbstate(0, 3); // Turns off the "Thumbnails #1" button.
    }
Button 2 code:

Code: Select all

"Toggle Small Icons View"
    $mode = get("view");
    if ($mode != 3) {
        #305;
        ctbstate(1);
        ctbstate(0, 1); // Turns off the "Details" button.
        ctbstate(0, 3); // Turns off the "Thumbnails #1" button.
    }
Button 3 code:

Code: Select all

"Toggle Thumbnails #1 View"
    $mode = get("view");
    if ($mode != 4) {
        #306;
        ctbstate(1);
        ctbstate(0, 1); // Turns off the "Details" button.
        ctbstate(0, 2); // Turns off the "Small Icons" button.
    }
It works, but I don't like that buttons are referring each other using their index (for obvious reasons). I have tried to use ctbname(), but it seems it doesn't intended for such referring. Have I missed something? Does ctbname() work for this task? Or is it exist another way?

highend
Posts: 13327
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Refer to custom button using its name instead of its index

Post by highend »

Write two helper functions,

Code: Select all

GetRealButtonNames()
ToggleButtonState($state, $buttonName)
where GetRealButtonNames()
get's all toolbar icons with toolbar()
and fills a string with button names, depending on if the button has a name
beginning with ctb and the number behind is the id that you query with ctbname()

ToggleButtonState($state, $buttonName)
should be self explanatory after this...
One of my scripts helped you out? Please donate via Paypal

John_C
Posts: 336
Joined: 16 May 2018 20:04

Re: Refer to custom button using its name instead of its index

Post by John_C »

@highend Thanks, I will try to write it and then I will post it here for other users.

Post Reply