2013-05-15 53 views
1

我正在寻找一种方法来添加复选框checkedchanged事件。 我想如果我的CB_Consultant(CB =复选框)被选中,它使CB_EndDate和TB_Company(文本框)是 我不知道该怎么handdle那种即使在现场点击PowerShell/.net CheckedChanged事件处理

这里是我的代码感谢帮助我

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 

回答

1

这很容易找到,但花了我很多时间来找到这个过程。这就是我所做的

我把这个线

$CB_Consultant.CheckState 

,并通过替换为以下代码:

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

结果是有两个灰朦领域的复选框,当你点击该复选框,另外两个字段转为活动状态。

+0

你好。你的回答对我今天有很大的帮助,但是我想了解我在尝试为自己解决问题时错过了什么。我多次浏览http://msdn.microsoft.com/en-us/library/system.windows.forms.checkbox(v=vs.110).aspx,但从未在任何方法或事件之前看到“Add_”。我是一个自学的编剧,所以这对你来说可能是显而易见的知识,但我很想更好地理解这一点。这与事件处理有关吗? – BSAFH

0

也许Windows Forms并不是在PowerShell中执行此操作的最佳方法。你见过这个吗?

http://showui.codeplex.com/

或者,也许使用WPF真正创建一个丰富的客户端应用,而脚本在PowerShell中。

+0

当然,我知道这不是更好的方式去做,但到目前为止,我可以做我想做的。我会看看这个cmdlet。 – lotirthos227

+0

我终于找到了一种方法,我自己谢谢你的时间 – lotirthos227

0

如果您使用的是System.Windows.Forms复选框,但不使用System.Windows.Controls复选框,则这种方法可行,如果您在XAML中制作图形用户界面,则该复选框就是如此。

对于XAML,您需要添加单独的状态,如下面的示例所示。在这个例子中,我们有一个名为tabcheckBox的复选框和一个名为TabVMBtnDeleteVM的按钮。当该框被选中时,该按钮被禁用。当该框未被选中时,该按钮被启用。

$tabcheckBox.Add_Checked({$TabVMbtnDeleteVM.IsEnabled = $false}) 
$tabcheckBox.Add_Unchecked({$TabVMbtnDeleteVM.IsEnabled = $true})