Home > Software engineering >  Need to declare variables based on textbox input after a button click
Need to declare variables based on textbox input after a button click

Time:11-08

I am attempting to create a GUI for a script I wrote a while back. I have had a little trouble with it; I would like to have the "Submit Entries" button log the data entered by the user and assign it to variables($fromcompname, $tocompname, $userid) I am not sure if I need to have a function that pulls the input in the textboxes and assigns them to those variables or what would make this work.

The form itself is in xaml which I made in VS Pro and then I moved it to vs code as I am using powershell to run this script. The Window appears with all the elements I am just not certain how to get the "submit entries" button to log the data input by the user and pass it to the rest of the script to include the "transfer data" button. If there is a question you need to ask me to better understand the intent please do, I am hoping to find an answer. Thank you.

Code below:

Add-Type -AssemblyName PresentationFramework
#Builds the window/form for the GUI
[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="Profile Transfer" WindowStartupLocation = "CenterScreen"
        Width = "460" Height = "220" ShowInTaskbar = "True">
    <Grid Margin="0,0,0,0" Background="#FF3D0198">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="17*"/>
            <ColumnDefinition Width="443*"/>
        </Grid.ColumnDefinitions>
        <Button x:Name="subbttn" Content="Submit Entries" HorizontalAlignment="Left" Margin="290,84,0,0" VerticalAlignment="Top" Width="120" FontFamily="Times New Roman" Grid.Column="1" Height="18"/>
        <Button x:Name="transbttn" Content="TRANSFER Data" HorizontalAlignment="Left" Margin="145,124,0,0" VerticalAlignment="Top" Width="120" FontFamily="Times New Roman" Grid.Column="1" Height="18"/>
        <TextBox HorizontalAlignment="Left" Margin="290,15,0,0" TextWrapping="Wrap" Text="Old PC" VerticalAlignment="Top" Width="120" FontFamily="Times New Roman" Grid.Column="1" Height="16"/>
        <TextBox HorizontalAlignment="Left" Margin="290,38,0,0" TextWrapping="Wrap" Text="New PC" VerticalAlignment="Top" Width="120" FontFamily="Times New Roman" Grid.Column="1" Height="16"/>
        <TextBox HorizontalAlignment="Left" Margin="290,61,0,0" TextWrapping="Wrap" Text="User ID" VerticalAlignment="Top" Width="120" FontFamily="Times New Roman" Grid.Column="1" Height="16"/>
        <Label x:Name="fromlbl" Content="What is the name of the Old PC?" HorizontalAlignment="Left" Margin="1,11,0,0" VerticalAlignment="Top" Width="202" Foreground="White" FontFamily="Times New Roman" Grid.Column="1" Height="24"/>
        <Label x:Name="tolbl"Content="What is the name of the New PC?" HorizontalAlignment="Left" Margin="1,34,0,0" VerticalAlignment="Top" Foreground="White" FontFamily="Times New Roman" Grid.Column="1" Height="24" Width="173"/>
        <Label x:Name="uidlbl"Content="What is the USER's ID?" HorizontalAlignment="Left" Margin="1,57,0,0" VerticalAlignment="Top" Foreground="White" FontFamily="Times New Roman" Grid.Column="1" Height="24" Width="126"/>
        <StatusBar Grid.Column="1" Margin="0,147,33,10"/>

    </Grid>
</Window>
"@
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)

#Declares the variables based on the Name input in the XAML elements
$subbttn = $window.FindName("subbttn")
$transbttn = $window.FindName("transbttn")
$fromlabel = $window.FindName("fromlbl")
$tolabel = $window.FindName("tolbl")
$useridlabel = $window.FindName("uidlbl")

#Declares the needed data based on the user's input in the Textboxes
$subbttn.Add_Click({
    $fromcompname = $fromlabel.Text
    $tocompname = $tolabel.Text
    $userid = $useridlabel.Text

    Write-Host "$fromcompname" "$tocompname" "$userid"
})

#Opens the path to the old PC and the new PC for Robocopy
Set-Item -path env:computername -Value "$fromcompname"
Set-Item -path env:computername -Value "$tocompname"

#Sets the clikc of the transfer data button to initiate the robocopy
$transbttn.Add_Click({
    #robocopy is more intelligent
    C:\windows\system32\Robocopy.exe "\\$fromcompname\c$\Users\$userid" "\\$tocompname\c$\Users\$userid" `
    "/XJ" "/MIR" "/PURGE"  `
    "/R:2" "/W:5" "/XA:SH" "/S" "/256" "/XF" "*.MP3" "desktop.ini" `
    "/XD" "AppData" "Contacts" "Downloads" "Links" "Music" "SapWorkDir" "Searches" "Tracing" "Remote Assistance Logs" "Saved Games"

    #ROBOCOPY NOTES

    #/XJ excludes Junction Directories, breaks the "Application Data" loop Robocopy has a bug with
    #/MIR Mirrors a directory tree
    #/PURGE Deletes destination files and directories that no longer exist in the source.
    #/S Copies subdirectories and excludes empty directories.
    #/R:0 Specifies the number of retries on failed copies
    #/W:0 Specifies the wait time between retries in seconds
    #/XF Excludes Files
    #/XD Excludes Directories
    #/COPY:DATSOU copies security permissions of folders
})

$window.ShowDialog()

CodePudding user response:

As TheMadTechnician commented, you need to set up variables for the TextBoxes in your gui, rather than the Labels.

To do that, you will have to give those text boxes names too like:

<TextBox x:Name="fromcompany" HorizontalAlignment="Left" Margin="290,15,0,0" TextWrapping="Wrap" Text="Old PC" VerticalAlignment="Top" Width="120" FontFamily="Times New Roman" Grid.Column="1" Height="16"/>
<TextBox x:Name="tocompany" HorizontalAlignment="Left" Margin="290,38,0,0" TextWrapping="Wrap" Text="New PC" VerticalAlignment="Top" Width="120" FontFamily="Times New Roman" Grid.Column="1" Height="16"/>
<TextBox x:Name="userid" HorizontalAlignment="Left" Margin="290,61,0,0" TextWrapping="Wrap" Text="User ID" VerticalAlignment="Top" Width="120" FontFamily="Times New Roman" Grid.Column="1" Height="16"/>

and then create variables to be able to reference them

$fromcompanyTB = $window.FindName("fromcompany")
$tocompanyTB   = $window.FindName("tocompany")
$useridTB      = $window.FindName("userid")

Inside the Click() event handlers you need script scoping to be able to use these variables, otherwise they will simply be variables, local to the scriptblock:

$subbttn.Add_Click({
    $script:fromcompname = $script:fromcompanyTB.Text
    $script:tocompname   = $script:tocompanyTB.Text
    $script:userid       = $script:useridlabelTB.Text

    Write-Host "$script:fromcompname  $script:tocompname  $script:userid"
})

The same goes for $transbttn.Add_Click({})

Finally, you need to put a space in front of Content= in the two last <Label lines, otherwise the Xaml is invalid.

  • Related