by mac-duff at 2012-11-22 03:58:22
Hi there,by nohandle at 2012-11-23 14:13:40
I am quite new to Powershell and creating a form with textbox and a button. With the button I open the OpenFIleBox Dialog which result (the path) I would like to copy into the textbox.
For executing the Dialogbox I use this statement:
$SelectButton1.Add_Click({Select-FileDialog})
which opens this function:
function Select-FileDialog
{
param([string]$Title,[string]$Directory,[string]$Filter="MSI/MST Files (.msi,.mst)|.msi;.mst")
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
#[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
$objForm = New-Object System.Windows.Forms.OpenFileDialog
$objForm.InitialDirectory = $Directory
$objForm.Filter = $Filter
$objForm.Title = $Title
$Show = $objForm.ShowDialog()
If ($Show -eq "OK")
{
Return $objForm.FileName
}
Else
{
Write-Error "Operation cancelled by user."
}
}
[?Code]
When I just use this SelectFile Function I can save the result in a variable but by calling it with the button before I have no idea how to do it, because also I want to use this function for another case too.
Btw. I also I am facing the problem that I can only open the OpenFileDialog while running ISE, when I execute it with powershell I only get a black cmd window.
Thanks
Stephan
[quote="mac-duff"]When I just use this SelectFile Function I can save the result in a variable but by calling it with the button before I have no idea how to do it, because also I want to use this function for another case too.[/quote]by mac-duff at 2012-11-27 04:36:15
I am not sure what you are asking, can you rephrase it or show an example please?
Hi there,by mac-duff at 2012-11-28 00:52:24
this is my code and what I am trying to reach is select a MSI?MST which path/route than appears in the textbox
function Invoke-Method ($Object, $MethodName, $ArgumentList) {
return $Object.GetType().InvokeMember($MethodName, ‘Public, Instance, InvokeMethod’, $null, $Object, $ArgumentList)
}
$msiOpenDatabaseModeTransact = 1
$msiViewModifyUpdate = 2
$msiViewModifyReplace = 4
$msiViewModifyDelete = 6
Function main
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
out-null
$WinForm = new-object Windows.Forms.Form
$WinForm.text = "MSI Modifier"
$WinForm.Size = new-object Drawing.Size(400,200)
#$ListBox = new-object Windows.Forms.ListBox
#$winform.controls.add($listbox)
#ForEach($i in (Get-Content c:\fso\itemlist.txt))
#{ $ListBox.items.add($i) | Out-Null }
$objTextBox1 = New-Object System.Windows.Forms.TextBox
$objTextBox1.Location = New-Object System.Drawing.Size(10,20)
$objTextBox1.Size = New-Object System.Drawing.Size(260,20)
$winform.Controls.Add($objTextBox1)
$objTextBox1.Text = "MSI-File"
$objTextBox2 = New-Object System.Windows.Forms.TextBox
$objTextBox2.Location = New-Object System.Drawing.Size(10,60)
$objTextBox2.Size = New-Object System.Drawing.Size(260,20)
$winform.Controls.Add($objTextBox2)
$objTextBox2.Text = "MST-File"
$SelectButton1 = New-Object System.Windows.Forms.Button
$SelectButton1.Location = New-Object System.Drawing.Size(280,18)
$SelectButton1.Size = New-Object System.Drawing.Size(75,23)
$SelectButton1.Text = "Select"
$SelectButton1.Add_Click({Select-FileDialog})
$winform.Controls.Add($SelectButton1)
$SelectButton2 = New-Object System.Windows.Forms.Button
$SelectButton2.Location = New-Object System.Drawing.Size(280,58)
$SelectButton2.Size = New-Object System.Drawing.Size(75,23)
$SelectButton2.Text = "Select"
$SelectButton2.Add_Click({Select-FileDialog})
$winform.Controls.Add($SelectButton2)
$ModifyButton = New-Object System.Windows.Forms.Button
$ModifyButton.Location = New-Object System.Drawing.Size(80,110)
$ModifyButton.Size = New-Object System.Drawing.Size(75,23)
$ModifyButton.Text = "Modify"
$ModifyButton.Add_Click({Modify-MSI})
$winform.Controls.Add($ModifyButton)
$CloseButton = New-Object System.Windows.Forms.Button
$CloseButton.Location = New-Object System.Drawing.Size(280,110)
$CloseButton.Size = New-Object System.Drawing.Size(75,23)
$CloseButton.Text = "Close"
$CloseButton.Add_Click({$WinForm.Close()})
$winform.Controls.Add($CloseButton)
$WinForm.Add_Shown($WinForm.Activate())
$WinForm.showdialog() | out-null
#$ListBox.SelectedItem
}
function Select-FileDialog
{
param([string]$Title,[string]$Directory,[string]$Filter="MSI/MST Files (.msi,.mst)|.msi;.mst")
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
#[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
$objForm = New-Object System.Windows.Forms.OpenFileDialog
$objForm.InitialDirectory = $Directory
$objForm.Filter = $Filter
$objForm.Title = $Title
$Show = $objForm.ShowDialog()
If ($Show -eq "OK")
{
Return $objForm.FileName
}
Else
{
Write-Error "Operation cancelled by user."
}
}
function Modify-MSI
{
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[ValidateScript({$_ | Test-Path -PathType Leaf})]
[string]
$Path
)
$installer = New-Object -ComObject WindowsInstaller.Installer
$database = Invoke-Method $installer OpenDatabase @($Path, $msiViewModifyUpdate)
$view = Invoke-Method $database OpenView @("UPDATE Property SET Value=‘1’ WHERE Property=‘ALLUSERS’")
Invoke-Method $view Execute
#$view = $database.OpenView("INSERT INTO Property (Property, Value) VALUES (‘test’,'546)")
#$View = $Database.GetType().InvokeMember(“OpenViewâ€, “InvokeMethodâ€, $Null, $Database, ("INSERT INTO Property (Property, Value) VALUES (‘test’,'546)"))
#Invoke-Method $view Execute
Invoke-Method $view Close @()
$view = $null
$database = $null
}
main
#$str_filename = Select-FileDialog
#Modify-MSI($str_filename)
$SelectButton1.Add_Click({Select-FileDialog})
ok,
found it.
$SelectButton1.Add_Click({$objTextBox1.Text = Select-FileDialog})
Just had to it like that