4
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PowerShellでフォームの作成とファイルパスの取得

Last updated at Posted at 2019-05-28

初めに

みなさん始めまして。
初投稿です。よろしくお願いします。
フォームの使い方の備忘録として書きました。
概要としては、PowerShellにてフォームを作成しユーザにダイアログを用いてファイルを選択をさせ、そのファイルパスを取得していきます。

下図が作成したフォームになります。
1.png

参考

PowerShellをはじめよう ~PowerShell入門~: PowerShellでユーザーフォームを作る - 基礎編 -

フォームの作成

初めに必要なアセンブリを読み込んでいきます。

# アセンブリ読み込み
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

次に必要なフォームを作成していきます。
ここでメインのフォームを作成しサイズを決定します。
設定するプロパティの概要
Size:(横幅,縦幅) で指定
Text:ラベル等に表示される文字

# フォーム作成
$Form = New-Object System.Windows.Forms.Form 
$Form.Size = New-Object System.Drawing.Size(400,130) 
$Form.Text = "フォーム作成サンプル"

次に必要なラベル,テキストボックス,ボタンを作成していき該当のものをフォームに追加します。
設定するプロパティの概要
Location:フォーム内の設置座標を(X,Y)で指定
DialogResult:ボタンを押された時に返す値、次のいずれかとなる(None, OK, Cancel, Abort, Retry, Ignore, Yes, No)

# ラベル作成
$LabelFilePath = New-Object System.Windows.Forms.Label
$LabelFilePath.Location = New-Object System.Drawing.Point(20,10)
$LabelFilePath.Size = New-Object System.Drawing.Size(80,20)
$LabelFilePath.Text = "ファイルパス"
$Form.Controls.Add($LabelFilePath)

# テキストボックス
$TextBoxFilePath = New-Object System.Windows.Forms.TextBox
$TextBoxFilePath.Location = New-Object System.Drawing.Point(20,30)
$TextBoxFilePath.Size = New-Object System.Drawing.Size(300,20)
$Form.Controls.Add($TextBoxFilePath)

# 参照ボタン
$ButtonFilePath = New-Object System.Windows.Forms.Button
$ButtonFilePath.Location = New-Object System.Drawing.Point(320,30)
$ButtonFilePath.Size = New-Object System.Drawing.Size(40,20)
$ButtonFilePath.Text = "参照"
$Form.Controls.Add($ButtonFilePath)

# OKボタン
$ButtonOK = New-Object System.Windows.Forms.Button
$ButtonOK.Location =  New-Object System.Drawing.Point(300,60)
$ButtonOK.Size = New-Object System.Drawing.Size(60,20)
$ButtonOK.Text = "OK"
$Form.Controls.Add($ButtonOK)

# Cancelボタン
$buttonCancel = New-Object System.Windows.Forms.Button
$buttonCancel.Location =  New-Object System.Drawing.Point(230,60)
$buttonCancel.Size = New-Object System.Drawing.Size(60,20)
$buttonCancel.Text = "キャンセル"
$buttonCancel.DialogResult = "Cancel"
$Form.Controls.Add($buttonCancel)

ボタンに動作の追加

参照ボタンをクリックしたときの動作を追加します。
Windows標準のダイアログを使用し、OKを押された時のみテキストボックスにファイルパスを記述します。

# 参照ボタンをクリック時の動作
$ButtonFilePath.add_click{

    #ダイアログを表示しファイルを選択する
    $Dialog = New-Object System.Windows.Forms.OpenFileDialog
    if($Dialog.ShowDialog() -eq "OK"){
        $TextBoxFilePath.Text = $Dialog.FileName
    }
}

ここにバリデーションチェックなどを実装してもよいです。今回は値が入力されていない場合のみテキストボックスの背景色を黄色にする処理を入れました。

# OKボタンをクリック時の動作
$ButtonOK.add_click{

    #ファイルパスが入力されていないときは背景を黄色にする
    if($TextBoxFilePath.text -eq ""){
        $TextBoxFilePath.BackColor = "yellow"
    }else{
        $Form.DialogResult = "OK"
    }
}

フォームの表示

先ほどまで作成したフォームを表示させます。
ファイルを選択しOKボタンを押下するとファイルパスが返ります。

# フォームを表示し処理が完了したらファイルパスを返す
$Result = $Form.ShowDialog()
if($Result -eq "OK"){
    return $TextBoxFilePath.text
}

以上

4
16
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?