2014-02-25 76 views
0
cls 

function GenerateForm2 { 


#region Import the Assemblies 
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null 
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null 
#endregion 

#region Generated Form Objects 
$form1 = New-Object System.Windows.Forms.Form 
$button2 = New-Object System.Windows.Forms.Button 
$button1 = New-Object System.Windows.Forms.Button 
$button3 = New-Object System.Windows.Forms.Button 
$button4 = New-Object System.Windows.Forms.Button 
$listView1 = New-Object System.Windows.Forms.ListView 
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState 
#endregion Generated Form Objects 

#region ImageList for nodes 
$global:imageList = new-Object System.Windows.Forms.ImageList 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Width = 32 
$System_Drawing_Size.Height = 32 
$global:imageList.ImageSize = $System_Drawing_Size 
$image1 = [System.Drawing.Image]::FromFile("M:\Powershell\ico files\folder.ico") 
$image2 = [System.Drawing.Image]::FromFile("M:\Powershell\ico files\file.ico") 

$global:imageList.Images.Add("folder",$image1) 
$global:imageList.Images.Add("file",$image2) 
#endregion 

#---------------------------------------------- 
#Generated Event Script Blocks 
#---------------------------------------------- 
#Provide Custom Code for events specified in PrimalForms. 
$button1_OnClick= 
{ 
    if ($script:previousItem) { 
     Refresh-ListView $script:previousItem 
    } else { 
     [System.Windows.Forms.MessageBox]::Show("Nothing to go back") 
    } 
} 

$doubleClick= 
{ 
    if ($listView1.SelectedItems[0].tag -eq "File") { 
     [System.Windows.Forms.MessageBox]::Show("You double clicked a file") 

    } else { 
     Refresh-ListView $listView1.SelectedItems[0].Name 
    } 
} 

$button2_OnClick= 
{ 
    $form1.Close() 
} 

$OnLoadForm_StateCorrection= 
{ 
    $form1.WindowState = $InitialFormWindowState 
    $listView1.LargeImageList = $imageList 
    Refresh-ListView "c:\" 
} 

$button3_OnClick= 
{ 
    if($listView1.SelectedItems[0].tag -eq "Folder"){ 
     $path2 = $listView1.SelectedItems[0] 
     $form1.Close() 
    } 
} 
$button4_OnClick= 
{ 

    $pathNew = CustomInputBox 


      if(Test-Path $pathNew){ 
       echo("Folder Already Exists") 
      } 
      else 
      { 
       New-Item -ItemType Directory -path $pathNew -Force 
       echo("Folder created") 
      } 
      $path2 = $pathNew 
      $form1.Close() 


} 

#---------------------------------------------- 
#region Generated Form Code 
$form1.Text = "Windows Browser" 
$form1.Name = "form1" 
$form1.DataBindings.DefaultDataSourceUpdateMode = 0 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Width = 689 
$System_Drawing_Size.Height = 499 
$form1.ClientSize = $System_Drawing_Size 

$button2.TabIndex = 2 
$button2.Name = "button2" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Width = 75 
$System_Drawing_Size.Height = 23 
$button2.Size = $System_Drawing_Size 
$button2.UseVisualStyleBackColor = $True 

$button2.Text = "Close" 

$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 602 
$System_Drawing_Point.Y = 459 
$button2.Location = $System_Drawing_Point 
$button2.DataBindings.DefaultDataSourceUpdateMode = 0 
$button2.add_Click($button2_OnClick) 

$form1.Controls.Add($button2) 

$button1.TabIndex = 1 
$button1.Name = "button1" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Width = 75 
$System_Drawing_Size.Height = 23 
$button1.Size = $System_Drawing_Size 
$button1.UseVisualStyleBackColor = $True 

$button1.Text = "Back" 

$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 13 
$System_Drawing_Point.Y = 459 
$button1.Location = $System_Drawing_Point 
$button1.DataBindings.DefaultDataSourceUpdateMode = 0 
$button1.add_Click($button1_OnClick) 

$form1.Controls.Add($button1) 

$button3.TabIndex = 3 
$button3.Name = "button3" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Width = 75 
$System_Drawing_Size.Height = 23 
$button3.Size = $System_Drawing_Size 
$button3.UseVisualStyleBackColor = $true 

$button3.Text = "Choose" 

$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 200 
$System_Drawing_Point.Y = 459 
$button3.Location = $System_Drawing_Point 
$button3.DataBindings.DefaultDataSourceUpdateMode =0 
$button3.add_Click($button3_OnClick) 

$form1.Controls.Add($button3) 

$button4.TabIndex = 4 
$button4.Name = "button4" 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Width = 75 
$System_Drawing_Size.Height = 23 
$button4.Size = $System_Drawing_Size 
$button4.UseVisualStyleBackColor = $true 

$button4.Text = "Create" 

$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 400 
$System_Drawing_Point.Y = 459 
$button4.Location = $System_Drawing_Point 
$button4.DataBindings.DefaultDataSourceUpdateMode = 0 
$button4.add_Click($button4_OnClick) 

$form1.Controls.Add($button4) 

$listView1.UseCompatibleStateImageBehavior = $False 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Width = 664 
$System_Drawing_Size.Height = 440 
$listView1.Size = $System_Drawing_Size 
$listView1.DataBindings.DefaultDataSourceUpdateMode = 0 
$listView1.Name = "listView1" 
$listView1.View = 0 
$listView1.TabIndex = 0 
$System_Drawing_Point = New-Object System.Drawing.Point 
$System_Drawing_Point.X = 13 
$System_Drawing_Point.Y = 13 
$listView1.Location = $System_Drawing_Point 
$System_Drawing_Size = New-Object System.Drawing.Size 
$System_Drawing_Size.Width = 32 
$System_Drawing_Size.Height = 32 
$listView1.TileSize = $System_Drawing_Size 

$listView1.add_DoubleClick($doubleClick) 

$form1.Controls.Add($listView1) 

#endregion Generated Form Code 

#Save the initial state of the form 
$InitialFormWindowState = $form1.WindowState 
#Init the OnLoad event to correct the initial state of the form 
$form1.add_Load($OnLoadForm_StateCorrection) 
#Show the Form 
$form1.ShowDialog()| Out-Null 

} #End Function2 


GenerateForm2 

Write-Host $path2 

這將寫出來的ListViewItem:{}測試Powershell的GUI返回文件路徑

我需要路徑2是我選擇的文件夾中的文件路徑。

我已經寫了一個gui的Windows資源管理器撕掉,所以我可以搜索文件和文件夾。 button3是選擇。這個按鈕應該取所選內容的值並將其分配給path2,然後關閉gui。不過,我有麻煩意識到這一點。任何幫助將不勝感激

回答

0

你可以得到,如果你改變你的腳本行

$path2 = $listView1.SelectedItems[0] 

$Script:path2 = $listView1.SelectedItems[0].Name.ToString() 
+0

尼斯書面所選文件夾名!非常感謝你的訣竅。你介意解釋什麼是$ script:path2之前呢? – Peter3

+0

它在腳本級別設置變量。如果你不使用它,只要你退出這個函數,$ path2就會消失。 –