Quantcast
Channel: SAPIEN Forums
Viewing all articles
Browse latest Browse all 427

PowerShell GUIs • CheckedListBox prevent user from unchecking all boxes and only allow single checkbox to ever be selected

$
0
0
First let me be clear although i do have a license for Powershell Studio 2022, i'm not using it in this example.. it seems very overwhelming to me, i am just too new at trying to create GUIs to know what the hell i'm doing with this software!!...

I've posted on Stackoverflow [https://stackoverflow.com/questions/782 ... ing-all-ch], and although it hasn't been that long, i suspect i will get more targetted help here..

Hoping somebody has an easy fix.. to reiterate what's posted at stackoverflow, i'll just copy/paste here:

I'm very new to powershell winforms, apologies if this is a basic question...

Basically the title... i have a crappy GUI form (and yes i know i should probably use a radial button, please don't suggest changing that).

I want the user to ONLY be able to check a single checkbox at any one point in time, and i never want them to be able to uncheck all checkboxes.. at the moment if they click on Option 1, when it is already checked, it will successfully allow them to uncheck Option 1 and then nothing is checked and the user has managed to f#$^ themselves (as they always seem to manage to find a way to do).

Please help :(

My understanding is i need to modify the $checkListBox.add_ItemCheck event handler, but i can't wrap my brain around how i would do that, whilst retaining the existing behaviour.

Code:

Add-Type -AssemblyName System.Windows.Forms$form = New-Object System.Windows.Forms.Form$form.Size = New-Object System.Drawing.Size(300, 200)$checkListBox = New-Object System.Windows.Forms.CheckedListBox$checkListBox.Location = New-Object System.Drawing.Point(10, 10)$checkListBox.Size = New-Object System.Drawing.Size(200, 150)# Add items to the checkListBox$checkListBox.Items.Add("Option 1") | Out-Null$checkListBox.Items.Add("Option 2") | Out-Null$checkListBox.Items.Add("Option 3") | Out-Null$checkListBox.SelectionMode = 'One'$checkListBox.CheckOnClick = $true$checkListBox.SetItemChecked(0, $true) # set the first item by default.# Add ItemCheck event handler$checkListBox.add_ItemCheck({    param($sender, $e)    foreach ($index in $checkListBox.CheckedIndices) {        if ($index -ne $e.Index) {            $checkListBox.SetItemChecked($index, $false)        }    }})$form.Controls.Add($checkListBox)$form.ShowDialog() | Out-Null

Statistics: Posted by TheVERYAmateurCoder — Mon Apr 01, 2024 10:20 pm



Viewing all articles
Browse latest Browse all 427

Trending Articles