Function GenerateForm {
Add-Type -AssemblyName System.Windows.Forms
$i = 0
$j = 0
$k = 0
$CheckBoxes = @()
#Get the Empty Drives
$aDrives = ls function:[d-z]: -n | ?{ !(test-path $_) }
#Create Form Objects
$Form = New-Object system.Windows.Forms.Form
$Label1 = New-Object System.Windows.Forms.Label
$MapBtn = New-Object System.Windows.Forms.Button
$TextBox1 = New-Object System.Windows.Forms.TextBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#Store the checkbox vaiables into array
$aDrives | %{
$j = [Math]::Floor($i/5)
If ($k -ge 5 ) {$k = 0}
$CheckBoxes += New-Object System.Windows.Forms.CheckBox
$CheckBoxes[-1].UseVisualStyleBackColor = $True
$CheckBoxes[-1].Text = $_
$CheckBoxes[-1].AutoSize = $True
$CheckBoxes[-1].DataBindings.DefaultDataSourceUpdateMode = 0
$CheckBoxes[-1].Location = New-Object System.Drawing.Point((10 +40*$k),(20 + 40*$j))
$Form.Controls.Add($CheckBoxes[-1])
$i++
$k++
}
#Script block for the handlers
$handler_MapBtn_Click = {
$Count = 0
foreach ($CheckBox in $CheckBoxes) {
If ($CheckBox.Checked){$Count++}
}
If ($Count -eq 1) {
If (!$TextBox1.Text.StartsWith("\\")) {
[System.Windows.Forms.MessageBox]::Show("Path to map is missing \\", "Drive Mapping Message", "OK", "Warning")
}
Else {
$aPath = $TextBox1.Text.Split("\")
$PathArg1 = $aPath[2]
$PathArg2 = $TextBox1.Text.Substring($PathArg1.Length + 3)
write-host $PathArg1 $PathArg2
}
}
Elseif ($Count -eq 0 )
{ [System.Windows.Forms.MessageBox]::Show("You have not checked any drive, please check one", "Drive Mapping Message", "OK", "Warning") }
else{[System.Windows.Forms.MessageBox]::Show("You should check only one drive.", "Drive Mapping Message", "OK", "Warning")}
}
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form.WindowState = $InitialFormWindowState
}
#Form Vaiable
$Form.Text = " Drive Mapping"
#Label1 variables
$Label1.Text = " Select the Drive to Map"
$Label1.AutoSize = $True
$Form.Controls.Add($Label1)
#TextBox1 variables
$TextBox1.Text = "Map Path"
$TextBox1.AutoSize = $True
$TextBox1.Location = New-Object System.Drawing.Point(10,(60 + 40*$j) )
$Form.Controls.Add($TextBox1)
#MapBtn variables
$MapBtn.Text = "Map"
$MapBtn.AutoSize = $True
$MapBtn.Location = New-Object System.Drawing.Point(10,(60 + (50*$j)) )
$MapBtn.add_Click($handler_MapBtn_Click)
$Form.Controls.Add($MapBtn)
#Save the initial state of the form
$InitialFormWindowState = $form.WindowState
#Init the OnLoad event to correct the initial state of the form
$form.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$Form.ShowDialog()
}
GenerateForm
Add-Type -AssemblyName System.Windows.Forms
$i = 0
$j = 0
$k = 0
$CheckBoxes = @()
#Get the Empty Drives
$aDrives = ls function:[d-z]: -n | ?{ !(test-path $_) }
#Create Form Objects
$Form = New-Object system.Windows.Forms.Form
$Label1 = New-Object System.Windows.Forms.Label
$MapBtn = New-Object System.Windows.Forms.Button
$TextBox1 = New-Object System.Windows.Forms.TextBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#Store the checkbox vaiables into array
$aDrives | %{
$j = [Math]::Floor($i/5)
If ($k -ge 5 ) {$k = 0}
$CheckBoxes += New-Object System.Windows.Forms.CheckBox
$CheckBoxes[-1].UseVisualStyleBackColor = $True
$CheckBoxes[-1].Text = $_
$CheckBoxes[-1].AutoSize = $True
$CheckBoxes[-1].DataBindings.DefaultDataSourceUpdateMode = 0
$CheckBoxes[-1].Location = New-Object System.Drawing.Point((10 +40*$k),(20 + 40*$j))
$Form.Controls.Add($CheckBoxes[-1])
$i++
$k++
}
#Script block for the handlers
$handler_MapBtn_Click = {
$Count = 0
foreach ($CheckBox in $CheckBoxes) {
If ($CheckBox.Checked){$Count++}
}
If ($Count -eq 1) {
If (!$TextBox1.Text.StartsWith("\\")) {
[System.Windows.Forms.MessageBox]::Show("Path to map is missing \\", "Drive Mapping Message", "OK", "Warning")
}
Else {
$aPath = $TextBox1.Text.Split("\")
$PathArg1 = $aPath[2]
$PathArg2 = $TextBox1.Text.Substring($PathArg1.Length + 3)
write-host $PathArg1 $PathArg2
}
}
Elseif ($Count -eq 0 )
{ [System.Windows.Forms.MessageBox]::Show("You have not checked any drive, please check one", "Drive Mapping Message", "OK", "Warning") }
else{[System.Windows.Forms.MessageBox]::Show("You should check only one drive.", "Drive Mapping Message", "OK", "Warning")}
}
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form.WindowState = $InitialFormWindowState
}
#Form Vaiable
$Form.Text = " Drive Mapping"
#Label1 variables
$Label1.Text = " Select the Drive to Map"
$Label1.AutoSize = $True
$Form.Controls.Add($Label1)
#TextBox1 variables
$TextBox1.Text = "Map Path"
$TextBox1.AutoSize = $True
$TextBox1.Location = New-Object System.Drawing.Point(10,(60 + 40*$j) )
$Form.Controls.Add($TextBox1)
#MapBtn variables
$MapBtn.Text = "Map"
$MapBtn.AutoSize = $True
$MapBtn.Location = New-Object System.Drawing.Point(10,(60 + (50*$j)) )
$MapBtn.add_Click($handler_MapBtn_Click)
$Form.Controls.Add($MapBtn)
#Save the initial state of the form
$InitialFormWindowState = $form.WindowState
#Init the OnLoad event to correct the initial state of the form
$form.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$Form.ShowDialog()
}
GenerateForm
No comments:
Post a Comment