2016-12-04 10 views
1

Imは、ユーザーが必要とする特定のファイルにマークを付け、ファイルを作成してそれに応じて変更するコードを記述しています。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'が '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それはあなたを助けます。 –

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

関連する問題