Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

Powershell Form With Two Text Inputs and drop down selection

I'm very rusty in the area of powershell and trying to pick it back up but I have something bothering me that I can't seem to figure out. I've created a form that has two text inputs for file locations and a drop down list. What I'm attempting to do is compare the two files received in a text input. I've got the script already for the compare but what I'm trying to figure out is how I distinguish the script to be run from the selection of the drop down list and also parse the two text inputs from the form to the relevant compare script. Below is the generic form created. Any help would be much appreciated.

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")  
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
[void] [System.Windows.Forms.Application]::EnableVisualStyles() 
 
 
$Form = New-Object system.Windows.Forms.Form 
$Form.Size = New-Object System.Drawing.Size(700,700) 
#You can use the below method as well 
#$Form.Width = 400 
#$Form.Height = 200 
$form.MaximizeBox = $false 
$Form.StartPosition = "CenterScreen" 
$Form.FormBorderStyle = 'Fixed3D' 
$Form.Text = "IAM Application Comparison Tool" 

#application Drop down list
$Label = New-Object System.Windows.Forms.Label 
$Label.Text = "Please select the application being compared" 
$Label.AutoSize = $true 
$Label.Location = New-Object System.Drawing.Size(10,10) 
$Font = New-Object System.Drawing.Font("Arial",15,[System.Drawing.FontStyle]::Bold) 
$form.Font = $Font 
$Form.Controls.Add($Label) 

#list of applications
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,50)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height = 80

[void] $listBox.Items.Add('Terradata')
[void] $listBox.Items.Add('')
[void] $listBox.Items.Add('')
[void] $listBox.Items.Add('')
[void] $listBox.Items.Add('')
[void] $listBox.Items.Add('')
[void] $listBox.Items.Add('')

$form.Controls.Add($listBox)
 
#File path of previous months file
$Label = New-Object System.Windows.Forms.Label 
$Label.Text = "Enter file path of previous months file" 
$Label.AutoSize = $true 
$Label.Location = New-Object System.Drawing.Size(20,200) 
$Font = New-Object System.Drawing.Font("Arial",15,[System.Drawing.FontStyle]::Bold) 
$form.Font = $Font 
$Form.Controls.Add($Label) 

#Input file path
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(20,300)
$textBox.Size = New-Object System.Drawing.Size(450,20)
$form.Controls.Add($textBox)
 
#$formIcon = New-Object system.drawing.icon ("$env:USERPROFILEdesktopBlogv.ico") 
#$form.Icon = $formicon 

#File path of current month
$Label = New-Object System.Windows.Forms.Label 
$Label.Text = "Enter file path of current months file" 
$Label.AutoSize = $true 
$Label.Location = New-Object System.Drawing.Size(20,400) 
$Font = New-Object System.Drawing.Font("Arial",15,[System.Drawing.FontStyle]::Bold) 
$form.Font = $Font 
$Form.Controls.Add($Label) 

#Input file path
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(20,500)
$textBox.Size = New-Object System.Drawing.Size(450,20)
$form.Controls.Add($textBox)

#Run Required Compare script
$Okbutton = New-Object System.Windows.Forms.Button 
$Okbutton.Location = New-Object System.Drawing.Size(170,600) 
$Okbutton.Size = New-Object System.Drawing.Size(200,30) 
$Okbutton.Text = "Compare Files" 
$Okbutton.Add_Click() 
$Form.Controls.Add($Okbutton) 

$Form.ShowDialog() 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Create a function that will run whenever the button is clicked. for instance, your button event will be:

$Okbutton.Add_Click({ BtnClick }) 

then add the function that will do the compare, whenever the button is clicked the function will be called:

function BtnClick{
# do something /  compare your texts
#compare $TextBox1.Text $TextBox2.Text
}

you have to give a different variable name for each textbox, this how you'll be able to access the values:

$input1 = $TextBox1.Text
$input2 = $TextBox2.Text
$input3 = $ComboBox1.Text

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...