2016-12-07 9 views
0

私のスクリプトにはテキストボックスがあります。ユーザーはそのテキストをテキストに挿入します。テキストボックス。変数を使用してテキストをテキストボックスの入力に置き換えます。

問題:ファイル内で変更したい部分が削除されますが、代わりにユーザーのテキストが書き込まれません。私はまた、ifループ内の変数の位置を探そうとしましたが、私が望むようにテキストを変更しましたが、スクリプトを再実行すると、無効なテキストボックスに古いテキストが書き込まれました。

私のスクリプトはちょっと長いので、私はそのすべてを投稿しませんが、ここではインポート部分です。助けてくれてありがとう!

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

#This creates the TextBox1 and put it on disable 
$objTextBox1 = New-Object System.Windows.Forms.TextBox 
$objTextBox1.Location = New-Object System.Drawing.Size(450,40) 
$objTextBox1.Size = New-Object System.Drawing.Size(140,150) 
$objTextBox1.TabIndex = 3 
$objTextBox1.text = $text1 
$objTextBox1.Enabled = $false 
$objForm.Controls.Add($objTextBox1) 

#This creates a checkbox for textbox1 
$objDsp2Checkbox = New-Object System.Windows.Forms.Checkbox 
$objDsp2Checkbox.Location = New-Object System.Drawing.Size(430,40) 
$objDsp2Checkbox.Size = New-Object System.Drawing.Size(150,20) 
$objDsp2Checkbox.TabIndex = 0 
$objForm.Controls.Add($objDsp2Checkbox) 

#Enables the textbox when user check the box: 
#textbox1 
$objDsp2Checkbox_OnClick = { 
if ($objDsp2Checkbox.Checked -eq $true) 
{ 
    $objTextBox1.Enabled = $true 
} 
elseif ($objDsp2Checkbox.Checked -eq $false) 
{ 
    $objTextBox1.Enabled = $false 
} 
} 
$objDsp2Checkbox.Add_Click($objDsp2Checkbox_OnClick) 

#variables 
$text1=$objTextBox1.Text 

#This creates the ok and cancle buttons: 
#ok Button 
$OKButton = New-Object System.Windows.Forms.Button 
$OKButton.Location = New-Object System.Drawing.Size(220,155) 
$OKButton.Size = New-Object System.Drawing.Size(75,23) 
$OKButton.Text = "OK" 
$OKButton.Add_Click(
{ 
if (($objDspCheckbox.Checked -eq $true) -and ($objDsp2Checkbox.Checked -eq $true)) 
{ 
    New-Item $path -itemtype file -name Dsp.json -value "old" ;((Get-Content "c:\users\$env:USERNAME\documents\Json\dsp.json") -replace 'old', $text1 | out-file "c:\users\$env:USERNAME\documents\Json\dsp.json") ;$objForm.close() 
} 

答えて

1

$objTextBox1.Textこのライン($text1をspecifly)変更しよう:私はそれはケースだかはわからないが、あなただけが必要な場合

New-Item $path -itemtype file -name Dsp.json -value "old" ; 
((Get-Content "c:\users\$env:USERNAME\documents\Json\dsp.json") -replace 'old', $objTextBox1.Text | 
Out-file "c:\users\$env:USERNAME\documents\Json\dsp.json") ;$objForm.close() 

:へ

New-Item $path -itemtype file -name Dsp.json -value "old" ; 
((Get-Content "c:\users\$env:USERNAME\documents\Json\dsp.json") -replace 'old', $text1 | 
Out-file "c:\users\$env:USERNAME\documents\Json\dsp.json") ;$objForm.close() 

をテキストボックスのテキストをファイルに保存する方が簡単です。

$objTextBox1.Text | Out-file "c:\users\$env:USERNAME\documents\Json\dsp.json") 
+0

素晴らしい!私が望んでいたもの、私はパラメータを持つビルドされたファイルを持っていて、私はそれらをユーザが書いたものに変更したかったのです。ありがとうございました! – ofribouba

+0

しかし、それは私がそれを試したように動作しなかった理由を知っていますか?なぜなら、私は$ text1 = $ textbox1.textを定義したので、同じではありませんか? – ofribouba

+0

'$ text1 = $ textbox1.text'は' $ textbox.text'の値を変数にコピーする1回限りの操作です。リンクされません。 –

関連する問題