locked
How to get values from a WPF GUI RRS feed

  • Question

  • Hello all!

    I am relatively new to PowerShell and I'm trying to create a User Interface, where I can retrieve the data values from that GUI.

    Basically, I want to be able to create a user with this GUI.

    I created the UI with Visual Studio 2019 in a WPF.

    I have defined:

    • Button_Create_User
    • Textbox_Vorname
    • Textbox_Nachname

    Then I'm trying to get these Textbox and Button for powershell:

    $createuserbutton = $window.FindName("Button_Create_User")
    $textblockvorname = $window.FindName("TextBox_Vorname")
    $textblocknachname = $window.FindName("TextBox_Nachname")

    Then once the button is pressed, I want to echo the textbox value. Echo was justed used as test, to see if it would even work.
    But it just wont work. I've tried multiple different methods, but somehow I can't manage to get this working properly.

    Any help would be gladly appretiated!

    Thanks in Advance

    Below you can find the full code:

    [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
    [xml]$XAML = @"
     
     <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            x:Name="Window" Title="Initial Window" WindowStartupLocation = "CenterScreen" 
             Height="450" Width="800">
            
            
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="64*"/>
                <ColumnDefinition Width="51*"/>
                <ColumnDefinition Width="377*"/>
                <ColumnDefinition Width="302*"/>
            </Grid.ColumnDefinitions>
            <Button Name="Button_Create_User" Content="Create User" Grid.Column="3" HorizontalAlignment="Left" Margin="146,358,0,0" VerticalAlignment="Top" Width="97" Height="29"/>
            <TextBox Name="TextBox_Vorname" Grid.Column="2" HorizontalAlignment="Left" Height="23" Margin="169,150,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
            <TextBlock Name="Text_Block_Copyright" Grid.Column="1" HorizontalAlignment="Left" Margin="22,25,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="35" Width="167" Grid.ColumnSpan="2"><Run Text="User Erstellungs Applikation"/><LineBreak/><Run FontSize="8" Text="copyright "/><Run FontSize="8" Text="winzips 2020"/></TextBlock>
            <TextBlock Name="TextBlock_Vorname" Grid.Column="1" HorizontalAlignment="Left" Margin="22,150,0,0" TextWrapping="Wrap" Text="Vorname:" VerticalAlignment="Top" Height="23" Width="167" Grid.ColumnSpan="2"/>
            <TextBox Name="TextBox_Nachname" Grid.Column="2" HorizontalAlignment="Left" Height="23" Margin="169,191,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
            <TextBlock Name="TextBlock_Nachname" Grid.Column="1" HorizontalAlignment="Left" Margin="22,191,0,0" TextWrapping="Wrap" Text="Nachname:" VerticalAlignment="Top" Height="23" Width="167" Grid.ColumnSpan="2"/>
            <TextBlock Name="TextBlock_OU" Grid.Column="1" HorizontalAlignment="Left" Margin="22,233,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="23" Width="167" Grid.ColumnSpan="2"><Run Text="Abteilung / Klasse"/><Run Text=" / Schuljahr"/><Run Text=":"/></TextBlock>
            <TextBox Name="TextBox_OU" Grid.Column="2" HorizontalAlignment="Left" Height="23" Margin="169,232,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
            <Image Name="CompanyLogo" Grid.Column="2" HorizontalAlignment="Left" Height="71" Margin="221,10,0,0" VerticalAlignment="Top" Width="77" Source="C:\temp\unnamed.png"/>
    
        </Grid>
    </Window>
    
    "@
    
    
    $createuserbutton = $window.FindName("Button_Create_User")
    $textblockvorname = $window.FindName("TextBox_Vorname")
    $textblocknachname = $window.FindName("TextBox_Nachname")
    
    
    $createuserbutton.Add_Click({
        $textblockvorname.Text
    
    echo $textblockvorname
        })
    
    
    
    
    
    
    
    #Read XAML
    $reader=(New-Object System.Xml.XmlNodeReader $xaml) 
    
    
    
    $Window=[Windows.Markup.XamlReader]::Load( $reader )
    $Window.ShowDialog()
    
    
    
    #Read XAML
    $reader = (New-Object System.Xml.XmlNodeReader $xaml)
    try {
        $window = [Windows.Markup.XamlReader]::Load( $reader )
    } catch {
        Write-Warning $_.Exception
        throw
    }
    
    
    
    
    
    try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
    catch{Write-Host "Unable to load Windows.Markup.XamlReader"; exit}
     
    # Store Form Objects In PowerShell
    $xaml.SelectNodes("//*[@Name]") | ForEach-Object {Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}
     
    #Show Form
    $Form.ShowDialog() | out-null
    
    
    
    
    

    Monday, February 17, 2020 1:11 PM

All replies

  • Hi,

    Not sure exactly what you are trying to do on click but i don't think "echo" is a powershell commandlet? Try this:

    [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
    [xml]$XAML = @"
     
     <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            x:Name="Window" Title="Initial Window" WindowStartupLocation = "CenterScreen" 
             Height="450" Width="800">
            
            
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="64*"/>
                <ColumnDefinition Width="51*"/>
                <ColumnDefinition Width="377*"/>
                <ColumnDefinition Width="302*"/>
            </Grid.ColumnDefinitions>
            <Button Name="Button_Create_User" Content="Create User" Grid.Column="3" HorizontalAlignment="Left" Margin="146,358,0,0" VerticalAlignment="Top" Width="97" Height="29"/>
            <TextBox Name="TextBox_Vorname" Grid.Column="2" HorizontalAlignment="Left" Height="23" Margin="169,150,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
            <TextBlock Name="Text_Block_Copyright" Grid.Column="1" HorizontalAlignment="Left" Margin="22,25,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="35" Width="167" Grid.ColumnSpan="2"><Run Text="User Erstellungs Applikation"/><LineBreak/><Run FontSize="8" Text="copyright "/><Run FontSize="8" Text="winzips 2020"/></TextBlock>
            <TextBlock Name="TextBlock_Vorname" Grid.Column="1" HorizontalAlignment="Left" Margin="22,150,0,0" TextWrapping="Wrap" Text="Vorname:" VerticalAlignment="Top" Height="23" Width="167" Grid.ColumnSpan="2"/>
            <TextBox Name="TextBox_Nachname" Grid.Column="2" HorizontalAlignment="Left" Height="23" Margin="169,191,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
            <TextBlock Name="TextBlock_Nachname" Grid.Column="1" HorizontalAlignment="Left" Margin="22,191,0,0" TextWrapping="Wrap" Text="Nachname:" VerticalAlignment="Top" Height="23" Width="167" Grid.ColumnSpan="2"/>
            <TextBlock Name="TextBlock_OU" Grid.Column="1" HorizontalAlignment="Left" Margin="22,233,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="23" Width="167" Grid.ColumnSpan="2"><Run Text="Abteilung / Klasse"/><Run Text=" / Schuljahr"/><Run Text=":"/></TextBlock>
            <TextBox Name="TextBox_OU" Grid.Column="2" HorizontalAlignment="Left" Height="23" Margin="169,232,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
            <Image Name="CompanyLogo" Grid.Column="2" HorizontalAlignment="Left" Height="71" Margin="221,10,0,0" VerticalAlignment="Top" Width="77" Source="C:\temp\unnamed.png"/>
    
        </Grid>
    </Window>
    
    "@
    
    
    #Read XAML
    $reader=(New-Object System.Xml.XmlNodeReader $xaml) 
    
    
    
    $Window=[Windows.Markup.XamlReader]::Load( $reader )
    
    $createuserbutton = $window.FindName("Button_Create_User")
    $textblockvorname = $window.FindName("TextBox_Vorname")
    $textblocknachname = $window.FindName("TextBox_Nachname")
    
    
    $createuserbutton.Add_Click{
    
    Write-Host $textblockvorname.text
        }
    
    
    
    
    
    
    
    
    $Window.ShowDialog()
    
    
    
    #Read XAML
    $reader = (New-Object System.Xml.XmlNodeReader $xaml)
    try {
        $window = [Windows.Markup.XamlReader]::Load( $reader )
    } catch {
        Write-Warning $_.Exception
        throw
    }
    
    
    
    
    
    try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
    catch{Write-Host "Unable to load Windows.Markup.XamlReader";}
     
    # Store Form Objects In PowerShell
    $xaml.SelectNodes("//*[@Name]") | ForEach-Object {Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}
     
    #Show Form
    $Form.ShowDialog() | out-null

     

    Monday, February 17, 2020 3:49 PM
  • If you are new to PowerShell and programming please use WinForms.  WPF will be a huge challenge.  WinForms are simple and are fairly easy to design and code.

    You can quickly design a simple interface on the PoshGUI web site.  It is perfect for simple forms.

    https://poshgui.com/

    Here is a quickie that i did in about two minutes.

    # This form was created using POSHGUI.com  a free online gui designer for PowerShell
    #   .NAME
    #    Untitled
    #
    
    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.Application]::EnableVisualStyles()
    
    $Form                            = New-Object system.Windows.Forms.Form
    $Form.ClientSize                 = '400,163'
    $Form.text                       = "Form"
    $Form.TopMost                    = $false
    
    $tbFirstName                     = New-Object system.Windows.Forms.TextBox
    $tbFirstName.multiline           = $false
    $tbFirstName.width               = 194
    $tbFirstName.height              = 20
    $tbFirstName.location            = New-Object System.Drawing.Point(160,40)
    $tbFirstName.Font                = 'Microsoft Sans Serif,10'
    
    $tbLastNAme                      = New-Object system.Windows.Forms.TextBox
    $tbLastNAme.multiline            = $false
    $tbLastNAme.width                = 193
    $tbLastNAme.height               = 20
    $tbLastNAme.location             = New-Object System.Drawing.Point(159,69)
    $tbLastNAme.Font                 = 'Microsoft Sans Serif,10'
    
    $buttonOk                        = New-Object system.Windows.Forms.Button
    $buttonOk.text                   = "Ok"
    $buttonOk.width                  = 60
    $buttonOk.height                 = 19
    $buttonOk.location               = New-Object System.Drawing.Point(287,105)
    $buttonOk.Font                   = 'Microsoft Sans Serif,10'
    
    $Label1                          = New-Object system.Windows.Forms.Label
    $Label1.text                     = "Last name:"
    $Label1.AutoSize                 = $true
    $Label1.width                    = 25
    $Label1.height                   = 10
    $Label1.location                 = New-Object System.Drawing.Point(87,70)
    $Label1.Font                     = 'Microsoft Sans Serif,10'
    
    $Label2                          = New-Object system.Windows.Forms.Label
    $Label2.text                     = "First name:"
    $Label2.AutoSize                 = $true
    $Label2.width                    = 25
    $Label2.height                   = 10
    $Label2.location                 = New-Object System.Drawing.Point(85,43)
    $Label2.Font                     = 'Microsoft Sans Serif,10'
    
    $Form.controls.AddRange(@($tbFirstName,$tbLastNAme,$buttonOk,$Label1,$Label2))
    
    $buttonOk.Add_Click({ 
        $Form.Close()
    })
    
    $Form.ShowDialog()

    All of this code was generated by PoshGUI web site.  YO can thenedit the code as needed or add more controls and style it as needed on teh web site.



    \_(ツ)_/


    • Edited by jrv Monday, February 17, 2020 6:17 PM
    Monday, February 17, 2020 6:15 PM