Add to Technorati Favorites
Welcome to ThePowerShellGuy.com Sign in | Join | Help

Hey PowerShell Guy!, How Can I Create an Windows Form For Displaying Log Files?

In the Hey Scripting guy ! article : How Can I Create an HTA For Displaying Log Files? ,

I made this example in PowerShell Using Windows Forms using the PSEventing CodePlex project PowerShell Eventing Snapin from Oisin the handle the ItemActivated event from the ListView.

I used a SplitControl Panel to make the Listview sizeable and the Form easy to construct in code.

The result is a form that looks like this,

Almost the same as the HTA version in the original article, the left  will list all files in the directory and on doubleclicking the name of a file it will display the contents in the right pane.

The Script looks like this :

# TextViewer.ps1
# http://thePowerShellGuy.com

[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
Add-PSSnapin PSEventing

Function invoke-TextViewer ($dir = '.'){

  $form = New-Object System.Windows.Forms.Form
  $form.text = "PowerShell Text viewer " + (resolve-path $dir)

  $sc = New-Object System.Windows.Forms.SplitContainer
  $sc.Dock = 'Fill'

  $lv = New-Object System.Windows.Forms.ListView
  $lv.View  = 'list'
  $lv.Dock = 'Fill'
  $sc.Panel1.Controls.add($lv)

  $rb = New-Object System.Windows.Forms.RichTextBox
  $rb.Dock = 'Fill'
  $sc.Panel2.Controls.add($rb)

  $form.Controls.Add($sc)

  $Form.Add_Shown({
    ls $dir |? {$_.PSIsContainer -eq $false} |% {$lv.Items.Add($_.name)}
    $form.Activate()
  }) 


  $form.show()

  Connect-Event -VariableName lv -EventName ItemActivate
  Connect-Event -VariableName form -EventName Closed

  while($true) {
    $event = read-Event -Wait
    if ($event.Name -eq "Closed") {return}
    $path = join-path $dir $event.source.value.selecteditems[0].text
    $rb.Text = [io.file]::ReadAllText($path) 
  }

}

invoke-TextViewer c:\PowerShell\Draft

As indicated in the HTA example this is very handy for viewing a directory with logfiles.

Enjoy,

Greetings /\/\o\/\/

Published Monday, February 04, 2008 6:16 PM by MoW
Filed under: , , ,

Attachment(s): textviewer.PNG

Comments

# Interesting Finds: February 5, 2008

Tuesday, February 05, 2008 1:15 PM by Jason Haley
Anonymous comments are disabled