2016-12-04 81 views
1

我正在編寫一個代碼,要求用戶標記他想要的某些文件,然後創建一個文件並對其進行相應的更改,現在就寫信給你。如果在If-elseif語句中不起作用

問題是它只適用於if部分,而不適用於else if。我無法在網上找到答案。

這裏是我的代碼:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName ("System.Windows.Forms") 

#This creates the path for the Json 
New-Item c:\users\$env:USERNAME\documents -ItemType directory -Name Json 

#creating the form 
$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Ofir`s script" 
$objForm.Size = New-Object System.Drawing.Size(270,200) 
$objForm.StartPosition = "CenterScreen" 

#creating the label 
$objLabel = New-Object System.Windows.Forms.Label 
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Please check the relevant boxes:" 
$objForm.Controls.Add($objLabel) 

#This creates a checkbox called dsp.z 
$objDspCheckbox = New-Object System.Windows.Forms.Checkbox 
$objDspCheckbox.Location = New-Object System.Drawing.Size(10,40) 
$objDspCheckbox.Size = New-Object System.Drawing.Size(500,20) 
$objDspCheckbox.Text = "dsp.z" 
$objDspCheckbox.TabIndex = 0 
$objForm.Controls.Add($objDspCheckbox) 

#This creates a checkbox called fpga.bin 
$objFpgaCheckbox = New-Object System.Windows.Forms.Checkbox 
$objFpgaCheckbox.Location = New-Object System.Drawing.Size(10,60) 
$objFpgaCheckbox.Size = New-Object System.Drawing.Size(500,20) 
$objFpgaCheckbox.Text = "fpga.bin" 
$objFpgaCheckbox.TabIndex = 1 
$objForm.Controls.Add($objFpgaCheckbox) 

#This creates a checkbox called bootrom_uncmp.bin 
$objBootCheckbox = New-Object System.Windows.Forms.Checkbox 
$objBootCheckbox.Location = New-Object System.Drawing.Size(10,80) 
$objBootCheckbox.Size = New-Object System.Drawing.Size(500,20) 
$objBootCheckbox.Text = "bootrom_uncmp.bin" 
$objBootCheckbox.TabIndex = 2 
$objForm.Controls.Add($objBootCheckbox) 

#ok Button 
$OKButton = New-Object System.Windows.Forms.Button 
$OKButton.Location = New-Object System.Drawing.Size(40,120) 
$OKButton.Size = New-Object System.Drawing.Size(75,23) 
$OKButton.Text = "OK" 
$OKButton.Add_Click({if ($objDspCheckbox.Checked -eq $true) 
{ 
New-Item c:\users\$env:USERNAME\documents\Json -itemtype file -name file.json -value "Hello" ;$objForm.close()} 
} 
) 

elseif ($objFpgaCheckbox.Checked -eq $true) 
{ 
New-Item c:\users\$env:USERNAME\documents\Json -itemtype file -name file.json -value "Hello2" ;$objForm.close() 
} 

elseif ($objBootCheckbox.Checked -eq $true) 
{ 
New-Item c:\users\$env:USERNAME\documents\Json -itemtype file -name file.json -value "Hello3" ;$objForm.close() 
} 

$objForm.Controls.Add($OKButton) 



#cancle Button 
$CancelButton = New-Object System.Windows.Forms.Button 
$CancelButton.Location = New-Object System.Drawing.Size(140,120) 
$CancelButton.Size = New-Object System.Drawing.Size(75,23) 
$CancelButton.Text = "Cancel" 
$CancelButton.Add_Click({$objForm.Close()}) 
$objForm.Controls.Add($CancelButton) 


#makes the form appear on top of the screen 
$objForm.Topmost = $True 

$objForm.Add_Shown({$objForm.Activate()}) 
[void] $objForm.ShowDialog() 
+0

檢查它。你只關閉'if-loop'這裏:'$ OKButton.Add_Click({if ...})' –

+0

我不明白。我在if語句中只有「{」,所以這是我唯一需要關閉的地方..不是?正如我在網上看到的其他結構如果是「else if(condition){action} – ofribouba

+0

我的意思是''''應該在'else-if'之後? –

回答

0

這是工作現在用elseif。我做了一些改進,如果json文件夾已經存在,那麼它會拋出錯誤。所以我在之前檢查,如果該文件夾已經存在,那麼不要創建或覆蓋。使用相同的文件夾並在其下創建Json文件。如果該文件夾不存在,那麼只創建它。

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

#This creates the path for the Json and also check if it is already there. 
If(!(Test-Path -Path C:\Users\$env:USERNAME\documents\Json)) 
{ 
New-Item c:\users\$env:USERNAME\documents -ItemType directory -Name Json 
} 
#creating the form 
$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Ofir`s script" 
$objForm.Size = New-Object System.Drawing.Size(270,200) 
$objForm.StartPosition = "CenterScreen" 

#creating the label 
$objLabel = New-Object System.Windows.Forms.Label 
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Please check the relevant boxes:" 
$objForm.Controls.Add($objLabel) 

#This creates a checkbox called dsp.z 
$objDspCheckbox = New-Object System.Windows.Forms.Checkbox 
$objDspCheckbox.Location = New-Object System.Drawing.Size(10,40) 
$objDspCheckbox.Size = New-Object System.Drawing.Size(500,20) 
$objDspCheckbox.Text = "dsp.z" 
$objDspCheckbox.TabIndex = 0 
$objForm.Controls.Add($objDspCheckbox) 

#This creates a checkbox called fpga.bin 
$objFpgaCheckbox = New-Object System.Windows.Forms.Checkbox 
$objFpgaCheckbox.Location = New-Object System.Drawing.Size(10,60) 
$objFpgaCheckbox.Size = New-Object System.Drawing.Size(500,20) 
$objFpgaCheckbox.Text = "fpga.bin" 
$objFpgaCheckbox.TabIndex = 1 
$objForm.Controls.Add($objFpgaCheckbox) 

#This creates a checkbox called bootrom_uncmp.bin 
$objBootCheckbox = New-Object System.Windows.Forms.Checkbox 
$objBootCheckbox.Location = New-Object System.Drawing.Size(10,80) 
$objBootCheckbox.Size = New-Object System.Drawing.Size(500,20) 
$objBootCheckbox.Text = "bootrom_uncmp.bin" 
$objBootCheckbox.TabIndex = 2 
$objForm.Controls.Add($objBootCheckbox) 

#ok Button 
$OKButton = New-Object System.Windows.Forms.Button 
$OKButton.Location = New-Object System.Drawing.Size(40,120) 
$OKButton.Size = New-Object System.Drawing.Size(75,23) 
$OKButton.Text = "OK" 
$OKButton.Add_Click(
    { 
    if($objDspCheckbox.Checked -eq $true) 
     { 
      New-Item c:\users\$env:USERNAME\documents\Json -itemtype file -name file.json -value "Hello" ;$objForm.close() 
     } 

    elseif($objFpgaCheckbox.Checked -eq $true) 
     { 
      New-Item c:\users\$env:USERNAME\documents\Json -itemtype file -name file.json -value "Hello2" ;$objForm.close() 
     } 
    elseif($objBootCheckbox.Checked -eq $true) 
     { 
      New-Item c:\users\$env:USERNAME\documents\Json -itemtype file -name file.json -value "Hello3" ;$objForm.close() 
     } 
    } 
    ) 


$objForm.Controls.Add($OKButton) 



#cancle Button 
$CancelButton = New-Object System.Windows.Forms.Button 
$CancelButton.Location = New-Object System.Drawing.Size(140,120) 
$CancelButton.Size = New-Object System.Drawing.Size(75,23) 
$CancelButton.Text = "Cancel" 
$CancelButton.Add_Click({$objForm.Close()}) 
$objForm.Controls.Add($CancelButton) 


#makes the form appear on top of the screen 
$objForm.Topmost = $True 

$objForm.Add_Shown({$objForm.Activate()}) 
[void] $objForm.ShowDialog() 
+0

多數民衆贊成在偉大!感謝您的改進和幫助! – ofribouba

+0

@ofribouba。請接受答案if它可以幫助你。 –

1

你有一個花括號這一行:

New-Item c:\users\$env:USERNAME\documents\Json -itemtype file -name file.json -value "Hello" ;$objForm.close()} 

所以更改單擊處理程序:

$OKButton.Add_Click(
{ 
    if ($objDspCheckbox.Checked -eq $true) 
    { 
     New-Item c:\users\$env:USERNAME\documents\Json -itemtype file -name file.json -value "Hello" ;$objForm.close() 
    } 

    elseif ($objFpgaCheckbox.Checked -eq $true) 
    { 
     New-Item c:\users\$env:USERNAME\documents\Json -itemtype file -name file.json -value "Hello2" ;$objForm.close() 
    } 

    elseif ($objBootCheckbox.Checked -eq $true) 
    { 
     New-Item c:\users\$env:USERNAME\documents\Json -itemtype file -name file.json -value "Hello3" ;$objForm.close() 
    } 
}) 
+0

非常感謝!爲我工作:) – ofribouba