0

I create a GUI with Visual Studio and import the XAML code in Powershell and i have a problem to display in textbox a value from one of 5 combobox selected item. I learn by myself and i really love it but i dont have the good method for debbug my code alone, anybody can have a time and a knowledge for help me please ?

I want to display in textbox ($TxtBox) the selected item (or value) from CboPL or CboTG or CboSB.... with powershell.

This is my XAML code beetwen here string in powershell :

<Window x:Name="Main" x:Class="Printers_Ordi.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Printers_Ordi"
    mc:Ignorable="d"
    Title="Printers-Ordi" Height="450" Width="728.471">
<Grid x:Name="Wall">
    <Grid.Background>
        <ImageBrush ImageSource="C:/Users/admsb160365/Desktop/scripts Shell/Imprimantes GHT/Wall.png"/>
    </Grid.Background>
    <Label x:Name="LabelTitre" Content="Printers-Ordi" HorizontalAlignment="Left" Margin="74,25,0,0" VerticalAlignment="Top" FontFamily="Yu Gothic UI Semibold" FontSize="24" RenderTransformOrigin="0.503,0.76" Height="42" Width="164" Foreground="#FF1346CD"/>
    <ComboBox x:Name="CboComputer" HorizontalAlignment="Left" Margin="74,82,0,0" VerticalAlignment="Top" Width="164" FontSize="16" Height="31" IsEditable="True" IsReadOnly="True" Text="Ordinateur"/>
    <ComboBox x:Name="CboSites" HorizontalAlignment="Left" Margin="74,118,0,0" VerticalAlignment="Top" Width="164" Height="31" FontSize="16" IsEditable="True" IsReadOnly="True" Text="Sites">
        <ComboBox x:Name="CboPL" HorizontalAlignment="Left" VerticalAlignment="Top" Width="154" FontSize="16" IsEditable="True" IsReadOnly="True" Text="Paimpol"/>
        <ComboBox x:Name="CboGP" HorizontalAlignment="Left" VerticalAlignment="Top" Width="154" FontSize="16" IsEditable="True" IsReadOnly="True" Text="Guingamp"/>
        <ComboBox x:Name="CboSB" HorizontalAlignment="Left" VerticalAlignment="Top" Width="154" FontSize="16" IsEditable="True" IsReadOnly="True" Text="SaintBrieuc"/>
        <ComboBox x:Name="CboLN" HorizontalAlignment="Left" VerticalAlignment="Top" Width="154" FontSize="16" IsEditable="True" IsReadOnly="True" Text="Lannion"/>
        <ComboBox x:Name="CboTG" HorizontalAlignment="Left" VerticalAlignment="Top" Width="154" FontSize="16" IsEditable="True" IsReadOnly="True" Text="Treguier"/>
    </ComboBox>

Thanks by advance for your patience and support ;-)

  • You'd use conditional logic to check if there is text in those boxes, collect/capture the .text value from those combo boxes, and place where that text to the .text value of the form element where you need it. There are many examples all over the web and Youtube regarding WinForms/WPF and Combobox use case. – postanote Mar 23 '21 at 02:51

1 Answers1

0

Continuing from my comments. There are a few ways to deal with your use case. Here is a simple example using Winform. Ultimately, it's still collecting/assigning text from one element(s)/or code results to another.

Add-Type -AssemblyName System.Drawing

$ComboBox          = New-Object System.Windows.Forms.ComboBox
$ComboBox.Location = New-Object System.Drawing.Point(10,10)
$ComboBox.Items.AddRange(@('One','Two'))

$RichTextBox          = New-Object System.Windows.Forms.RichTextBox
$RichTextBox.Location = New-Object System.Drawing.Point(10,40)

$Form = New-Object System.Windows.Forms.Form

$Form.Controls.Add($ComboBox)
$Form.Controls.Add($RichTextBox)

$ComboBox.Add_TextChanged({
    switch($ComboBox.Text){
    'One' {$RichTextBox.Text = 'This is one'}
    'Two' {$RichTextBox.Text = 'This is two'}
    }
})

$Form.ShowDialog()

An example using WPF

[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"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="PowerShell Form" Height="310" Width="350" ResizeMode="NoResize">
    <Grid Margin="0,0,0,0">
        <GroupBox Header="General" HorizontalAlignment="Left" Height="225" Margin="10,10,0,0" VerticalAlignment="Top" Width="320">
            <Grid HorizontalAlignment="Left" Height="206" Margin="0,0,0,0" VerticalAlignment="Top" Width="320">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="60*"/>
                    <ColumnDefinition Width="250*"/>
                </Grid.ColumnDefinitions>
                <Label Content="Field #1:" HorizontalAlignment="Left" Margin="0,7,0,0" VerticalAlignment="Top" Height="26" Width="93"/>
                <Label Content="Field #2:" HorizontalAlignment="Left" Margin="0,35,0,0" VerticalAlignment="Top" Height="26" Width="93"/>
                <Label Content="Field #3:" HorizontalAlignment="Left" Margin="0,65,0,0" VerticalAlignment="Top" Height="26" Width="93"/>
                <TextBox Name="f1" HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="229" Grid.Column="1"/>
                <ComboBox Name="f2" HorizontalAlignment="Left" Height="23" Margin="10,38,0,0" VerticalAlignment="Top" Width="229" Grid.Column="1">
                    <ListBoxItem>Test 1</ListBoxItem>
                    <ListBoxItem>Test 2</ListBoxItem>
                    <ListBoxItem>Test 3</ListBoxItem>
                </ComboBox>
                <TextBox Name="f3" HorizontalAlignment="Left" Height="23" Margin="10,66,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="229" Grid.Column="1"/>
            </Grid>
        </GroupBox>
        <Button Name="Clear" Content="Clear Form" HorizontalAlignment="Left" Margin="10,240,0,0" VerticalAlignment="Top" Width="90" Height="30" RenderTransformOrigin="0.522,0.733"/>
        <Button Name="Submit" Content="Submit" Margin="234,240,12,0" VerticalAlignment="Top" Height="30"/>
    </Grid>
</Window>
'@

$reader   = (New-Object System.Xml.XmlNodeReader $xaml) 
try{$Form = [Windows.Markup.XamlReader]::Load( $reader )}
catch
{
    Write-Host 'Unable to load Windows.Markup.XamlReader.'
    break
}

$xaml.SelectNodes('//*[@Name]') | 
ForEach-Object {Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}

$Form.Add_Loaded({    
    if (Test-Path -Path '.\ComboBox.selection') 
    {
        $cbSelection = Get-Content '.\ComboBox.selection'
        [int]$cbSelectionInt = -1
        [void][int]::TryParse($cbSelection,[ref]$cbSelectionInt)

        if ($cbSelectionInt -ge 0) 
        {$f2.SelectedIndex = $cbSelectionInt}
    }
})


$Clear.Add_Click({
    $fields = @('f1','f2','f3')

    foreach ($field in $fields) 
    {
        $control = $Form.FindName($field)
        switch ($control.GetType()) {
            'System.Windows.Controls.TextBox' {$control.Clear()}
            'System.Windows.Controls.ComboBox' {$control.SelectedIndex = -1}
            DEFAULT {'unknown control'}
        }
    }
})

$f2.add_SelectionChanged({
    param($sender,$args)
    $f3.Text = ($($sender.SelectedValue) -replace '.*:\s+')
})

$Form.ShowDialog()
postanote
  • 15,138
  • 2
  • 14
  • 25
  • Hello and thanks for explains but i dont know how to adapt this part of code with my variables : $ComboBox.Add_TextChanged({ switch($ComboBox.Text){ 'One' {$RichTextBox.Text = 'This is one'} 'Two' {$RichTextBox.Text = 'This is two'} } }) – Vincent Le Gall Mar 23 '21 at 10:48
  • In this case for exemple i want to pick the selected value from $WPFCboPL and display this value in $WPFTxtBox. – Vincent Le Gall Mar 23 '21 at 11:04
  • Understood, but that does not change the approach provided. You simply, supply the proper form element to share text data. You are not showing enough of your code to see what you are or are not doing to reach your goal. You are only showing XAML for form elements. – postanote Mar 23 '21 at 23:14