Select to view content in your preferred language

Start "session" on click?

449
1
09-08-2025 10:30 AM
AlfredBaldenweck
MVP Regular Contributor

Hi all,

I'd like to use a button to start a "session" in which stuff happens until I click the button again (like an edit session only different behavior). 

As best I can tell, when I click the button, it speeds through the behaviour and immediately exits. That is, it views it as a one-time thing instead of as a switch.

How can I change this to more of a turn-on, turn-off thing?

 

Edit to give a (not the actual thing) example: a counter between button clicks. Count until I click the button again.

0 Kudos
1 Reply
coryeicher
Frequent Contributor

Hi Alfred,

To implement a switch/toggle like this, your code needs to manage the "state" of the switch (on or off).

Regardless of which way you go, essentially:

  • When your add-in initializes, initialize your state to desired default, e.g. `stateMyButtonEnabled = true`
  • When user clicks button flip the switch e.g. `stateMyButtonEnabled = !stateMyButtonEnabled `

At any point you can check `stateMyButtonEnabled` to know for example if the button is currently enabled or not.

If you go with Pro "conditions", the syntax for getting/setting state is a bit different, but conceptually similar. 

```

// from Gemini with prompt "arcgis pro .net toggle button state"

protected override void OnClick()
{
// Check if the state is currently active
if (ArcGIS.Desktop.Framework.State.Contains("stateMyButtonEnabled "))
{
// If active, deactivate it
ArcGIS.Desktop.Framework.State.Deactivate("stateMyButtonEnabled ");
}
else
{
// If inactive, activate it
ArcGIS.Desktop.Framework.State.Activate("stateMyButtonEnabled ");
}
}

```

Good Luck

CORY EICHER
www.eichcorp.com
cory@eichcorp.com