1

I am searching a way to add checkbox checkedchanged event. What I want is if my CB_Consultant (CB= Checkbox) is checked, it enable the CB_EndDate and TB_Company (Textbox) I don't know how to handdle that kind of even on a live click

Here's my code thanks for helping me

function GenerateForm {
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null



$Form_UC_Main = New-Object System.Windows.Forms.Form
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Point = New-Object System.Drawing.Point
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
    $Form_UC_Main.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form User Creation Main Windows Code
$Form_UC_Main.Text = "User Creation software"
$Form_UC_Main.Name = "form1"
$Form_UC_Main.DataBindings.DefaultDataSourceUpdateMode = 0
$Form_UC_Main.size = new-object System.Drawing.Size(300,250)

#End of Main form windows code


#initialize the Checkboxe, if check should activate CB_EndDate and TB_Company
$CB_Consultant = New-Object System.Windows.Forms.CheckBox
$CB_Consultant.location =  new-object System.Drawing.Size (124,130)
$CB_Consultant.Size =  new-object System.Drawing.Size(15,14)
$CB_Consultant.UseVisualStyleBackColor = $True
$CB_Consultant.TabIndex = 0
$CB_Consultant.checkAlign = "MiddleRight"
#$CB_Consultant.Text = "Site groups change"
$CB_Consultant.DataBindings.DefaultDataSourceUpdateMode = 0
$CB_Consultant.Name = "Consultant"
$CB_Consultant.CheckState

$Form_UC_Main.Controls.Add($CB_Consultant)



$CBL_AccType = new-object System.Windows.Forms.Label
$CBL_AccType.Location = new-object System.Drawing.Size(20,130)
$CBL_AccType.size = new-object System.Drawing.Size(120,20)
$CBL_AccType.Text = "Enable if Checked :"

$Form_UC_Main.Controls.Add($CBL_AccType)

#This checkbox is disable by default, should be enable by checking CB_Consultant
$CB_EndDate = New-Object System.Windows.Forms.CheckBox
$CB_EndDate.location =  new-object System.Drawing.Size(125,150)
$CB_EndDate.Size =  new-object System.Drawing.Size(20,20)
$CB_EndDate.UseVisualStyleBackColor = $True
$CB_EndDate.TabIndex = 0
$CB_EndDate.enabled = $false
#$CB_EndDate.Text = "Site groups change"
$CB_EndDate.DataBindings.DefaultDataSourceUpdateMode = 0
$CB_EndDate.Name = "EndDateActivation"

$Form_UC_Main.Controls.Add($CB_EndDate)


$TBL_EndDate2 = new-object System.Windows.Forms.Label
$TBL_EndDate2.Location =  new-object System.Drawing.Size(50,154)
$TBL_EndDate2.Size =   new-object System.Drawing.Size(60,20)
$TBL_EndDate2.Text = "End Date:"

$Form_UC_Main.Controls.Add($TBL_EndDate2)

#This TextBox is disable by default, should be enable by checking CB_Consultant
$TB_Company = new-object System.Windows.Forms.Textbox
$TB_Company.Location =  new-object System.Drawing.Size(125,170) #was 125,130
$TB_Company.Size =   new-object System.Drawing.Size(150,27)
$TB_Company.DataBindings.DefaultDataSourceUpdateMode = 0
$TB_Company.enabled = $false
$TB_Company.Name = "Company Name"

$Form_UC_Main.Controls.Add($TB_Company)


$TBL_Company = new-object System.Windows.Forms.Label
$TBL_Company.Location =  new-object System.Drawing.Size(50,175) #was 50,122
$TBL_Company.Size =   new-object System.Drawing.Size(60,35)
$TBL_Company.Text = "Company:"

$Form_UC_Main.Controls.Add($TBL_Company)


#Save the initial state of the form
$InitialFormWindowState = $Form_UC_Main.WindowState
#Init the OnLoad event to correct the initial state of the form
$Form_UC_Main.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$Form_UC_Main.ShowDialog()| Out-Null
} #end of function
GenerateForm

4 Answers 4

2

This approach works if you're using a System.Windows.Forms Checkbox, but not if you're using a System.Windows.Controls checkbox, which is what you'll have if you make your GUI in XAML.

For XAML, you'll need to add separate states, as seen in the below example. In this example, we have a checkbox named tabcheckBox and a button named TabVMBtnDeleteVM. When the box is checked, the button is disabled. When the box is unchecked, the button is enabled.

$tabcheckBox.Add_Checked({$TabVMbtnDeleteVM.IsEnabled = $false})
$tabcheckBox.Add_Unchecked({$TabVMbtnDeleteVM.IsEnabled = $true})
Sign up to request clarification or add additional context in comments.

Comments

1

It was so easy to find, but it took me so many time to find that process. Here's what I did

I took this line

$CB_Consultant.CheckState

and replace it by this code :

$CB_Consultant.Add_CheckStateChanged({
    $TB_Company.enabled = $CB_Consultant.Checked
    $CB_EndDate.enabled = $CB_Consultant.Checked
    #and so on
})

The Result is a checkbox with two grayout field, when you click the Checkbox, the two other field turn to active state.

1 Comment

Hi there. Your answer has greatly helped me today but I want to understand what I was missing when trying to work this out for myself. I went through msdn.microsoft.com/en-us/library/… many times but never saw "Add_" before any method or event. Im a self taught scripter so this might be obvious knowledge to you but I'd love to understand this better. Is it something to do with event handling?
1
#Add below code after the checkBox code which you are created.
$CB_Consultant.Add_CheckStateChanged({
     if($CB_Consultant.checked){
          $CB_EndDate.enabled = $CB_Consultant.checked
          $TB_Company.enabled = $CB_Consultant.checked
     } else {
          $CB_EndDate.enabled = $CB_Consultant.checked
          $TB_Company.enabled = $CB_Consultant.checked
    }  
})

Comments

0

Perhaps Windows Forms is not the best way to do this in powershell. Have you seen this?

http://showui.codeplex.com/

Or maybe use WPF to actually create a rich client app instead of scripting in powershell.

1 Comment

for sure I know that it's not the better way to do it, but so far I can do what I want. I will take a look at this cmdlet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.