Membuat kotak input kustom

Sampel ini hanya berlaku untuk platform Windows.

Buat skrip kotak input kustom grafis menggunakan fitur pembuatan formulir Microsoft .NET Framework di Windows PowerShell 3.0 dan rilis yang lebih baru.

Membuat kotak input grafis kustom

Salin lalu tempelkan yang berikut ini ke WINDOWS PowerShell ISE, lalu simpan sebagai file skrip PowerShell (.ps1).

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,120)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter the information in the space below:'
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)

$form.Topmost = $true

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x = $textBox.Text
    $x
}

Skrip dimulai dengan memuat dua kelas .NET Framework: System.Drawing dan System.Windows.Forms. Anda kemudian memulai instans baru kelas .NET Framework System.Windows.Forms.Forms. Yang menyediakan formulir atau jendela kosong tempat Anda dapat mulai menambahkan kontrol.

$form = New-Object System.Windows.Forms.Form

Setelah Anda membuat instans kelas Formulir, tetapkan nilai ke tiga properti kelas ini.

  • Teks. Ini menjadi judul jendela.
  • Ukuran. Ini adalah ukuran formulir, dalam piksel. Skrip sebelumnya membuat formulir yang lebarnya 300 piksel dengan tinggi 200 piksel.
  • StartingPosition. Properti opsional ini diatur ke CenterScreen dalam skrip sebelumnya. Jika Anda tidak menambahkan properti ini, Windows memilih lokasi saat formulir dibuka. Dengan mengatur StartingPosition ke CenterScreen, Anda secara otomatis menampilkan formulir di tengah layar setiap kali dimuat.
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

Selanjutnya, buat tombol OK untuk formulir Anda. Tentukan ukuran dan perilaku tombol OK . Dalam contoh ini, posisi tombol adalah 120 piksel dari tepi atas formulir, dan 75 piksel dari tepi kiri. Tinggi tombol adalah 23 piksel, sedangkan panjang tombol adalah 75 piksel. Skrip menggunakan jenis Formulir Windows yang telah ditentukan sebelumnya untuk menentukan perilaku tombol.

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,120)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

Demikian pula, Anda membuat tombol Batalkan . Tombol Batalkan adalah 120 piksel dari atas, tetapi 150 piksel dari tepi kiri jendela.

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,120)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

Selanjutnya, berikan teks label di jendela Anda yang menjelaskan informasi yang Anda inginkan untuk diberikan pengguna.

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter the information in the space below:'
$form.Controls.Add($label)

Tambahkan kontrol (dalam hal ini, kotak teks) yang memungkinkan pengguna memberikan informasi yang telah Anda jelaskan dalam teks label Anda. Ada banyak kontrol lain yang dapat Anda terapkan selain kotak teks. Untuk kontrol lainnya, lihat Namespace Layanan System.Windows.Forms.

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)

Atur properti Paling Atas ke $true untuk memaksa jendela membuka di atas jendela dan kotak dialog lain yang terbuka.

$form.Topmost = $true

Selanjutnya, tambahkan baris kode ini untuk mengaktifkan formulir, dan atur fokus ke kotak teks yang Anda buat.

$form.Add_Shown({$textBox.Select()})

Tambahkan baris kode berikut untuk menampilkan formulir di Windows.

$result = $form.ShowDialog()

Terakhir, kode di dalam blok If menginstruksikan Windows apa yang harus dilakukan dengan formulir setelah pengguna menyediakan teks dalam kotak teks, lalu klik tombol OK atau tekan tombol Enter .

if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
    $x = $textBox.Text
    $x
}

Lihat juga