powershellxamlinputoutputsearch-box

Powershell + XAML + Search Box + capture and display


I am just about finished with program and need some help getting over the finish line. What is supposed to do is search through log files in a predefined directory. Nothing happens with the text I enter in the search box nor when I use the button that I created to execute the search. I have searched all over the Internet which has me as far as I am. I see lot's of examples in Winforms but I don't know the "translation" between Winforms and WPF.

I think that if someone can get me passed this hurdle I can implement the other features I want to add.

PS: I really have no programming skills, just tenacity.

Thanks in advance to all who help.

'''

    # Import needed Assemblies
    Add-Type -AssemblyName PresentationCore, PresentationFramework, WindowsBase, 
    System.Drawing, System.Windows.Forms, WindowsFormsIntegration
    [void][System.Reflection.Assembly]::LoadWithPartialName('PresentationFramework')
    [Windows.Forms.Application]::EnableVisualStyles()

    #XAML form designed using Visual Studio
    # Build the GUI
    [xml]$Form = @"
    <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                
                    Title="Log Crawler"
                    HorizontalAlignment="Center"
                    FontSize="11" Height="700" Width="1300" FontFamily="Calibri"
                    WindowStartupLocation="CenterScreen"
                    ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                    ScrollViewer.VerticalScrollBarVisibility="Disabled" WindowStyle="ThreeDBorderWindow">

                <Grid Margin="0,0,0,0">
                    <Grid.Background>
                            <LinearGradientBrush EndPoint="0.8,1" StartPoint="0.8,0">
                                    <GradientStop Color="Black"/>
                                    <GradientStop Color="White" Offset="1"/>
                            </LinearGradientBrush>
                    </Grid.Background>
                    
                    <TextBox x:Name="IntegrationsLogCrawler" Text="Integration Log Crawler" 
                                     HorizontalAlignment="Center" VerticalAlignment="Top" TextAlignment="Center" TextWrapping="NoWrap" 
                                     Margin="0,20,0,0 " Height="37" Width="260" 
                                     FontSize="24" FontFamily="Calibri" FontWeight="Bold" FontStyle="Normal" FlowDirection="LeftToRight"/>

                    <GroupBox x:Name="SearchBox" Header="SearchBox"
                                     HorizontalAlignment="Center" VerticalAlignment="Top"
                                     FontSize="12" FontFamily="Calibri" FontWeight="Bold" Foreground="#FFE4E41A"
                                     Margin="0,100,0,0" Height="40" Width="400">

                    <TextBox x:Name="InputBox" TextWrapping="NoWrap"/>
                    </GroupBox>

                    <Button x:Name="LogCrawl" Content="Log Crawl" 
                                    HorizontalAlignment="Center" VerticalAlignment="Top"
                                    Margin="0,150,0,0" Height="40" Width="190"
                                    FontSize="18" FontFamily="Calibri" FontWeight="Bold" Foreground="#FFEE0C3F"/>
                    
                    <Button x:Name="Exit" Content="Exit" 
                                    HorizontalAlignment="Center" VerticalAlignment="Top" 
                                    Margin="0,200,0,0" Height="39" Width="110" 
                                    FontSize="16" FontFamily="Calibri" FontWeight="Bold" Foreground="#FFE4E41A" Background="#FF040404"/>
                    
                    <GroupBox x:Name="Results" Header="Results"
                                     HorizontalAlignment="Center" VerticalAlignment="Top" 
                                     FontSize="12" FontFamily="Calibri" FontWeight="Bold" Foreground="#FFE4E41A"
                                     Margin="0,250,0,0 " Height="250" Width="1000">

                    <TextBox x:Name="OutputBox" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"/>
                    </GroupBox>
            </Grid>
    </Window>
    "@

    #Create a form
    $XMLReader = (New-Object System.Xml.XmlNodeReader  $Form)
    $XMLForm = [Windows.Markup.XamlReader]::Load($XMLReader)


    #Connect to Controls 
    $xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]")  | ForEach {
    New-Variable  -Name $_.Name -Value $XMLForm.FindName($_.Name) -Force
    }

    #   Load Log Crawl Menu Button Controls
    $InputBox = $XMLForm.FindName('InputBox')
    $OutputBox = $XMLForm.FindName('OutputBox')
    $LogCrawl = $XMLForm.FindName('LogCrawl')
    $Exit = $XMLForm.FindName('Exit')

    # Logfile Location
    $LogFiles = "$($ENV:USERPROFILE)\Documents\_data\logfiles"


    #   Log Crawl Button Action
    $LogCrawl1.Add_Click({ 
    Select-String  -Path "$LogFiles\*.*"  -Pattern  $InputBox  -Context 10, 20   |  Write-Host
    })


    # EXIT
    $Exit.Add_Click({
            $XMLForm.Close()
    })

    #Show XMLform
    $null = $XMLForm.ShowDialog()

'''


Solution

  • I'm not sure about the workings of this part (gave me exception "You cannot call a method on a null-valued expression.") :

    #Connect to Controls 
    $xaml.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]")  | ForEach {
    New-Variable  -Name $_.Name -Value $XMLForm.FindName($_.Name) -Force
    }
    

    and I think you should remove it.

    Then you made a typo in the button variable name $LogCrawl1 --> $LogCrawl, but also change the Add_Click() method to:

    #  Log Crawl Button Action
    $LogCrawl.Add_Click({ 
        # Logfile Location
        $LogFiles = "$($ENV:USERPROFILE)\Documents\_data\logfiles"
        $OutputBox.Text = Select-String -Path "$LogFiles\*.*" -Pattern $InputBox.Text -SimpleMatch -Context 10, 20
    })
    

    P.S. It is unclear if this is also in your real code, but the final closing "@ for the XAML here-string must not have whitespace to the left.