locked
Two powershell functions running when only one being called? RRS feed

  • Question

  • Hi guys

    Wondering if anybody can help? I have several functions written, i have two buttons, one calling one function, one calling another function but one of the button makes 2 functions run at the same time and i can't figure out why? My code is below, the two functions running are TillButtonClick and HubButtonClick, but only HubButtonClick should be called, the line of code that is being run is $HubButton.Add_Click({HubButtonClick}) so i do not understand why 2 functions are running? Any help would be greatly appreciated.

    Thanks

    #Functions
    
    Function OKButtonHub{
    #$text="test "
    $HubName= $textBox.Text.ToString()
    
    $DeviceName= Get-Content -Path \\$HubName\D$\gkretail\basecomponents\sm.hybridinfoserver\config\stations\$HubName.properties | Where-Object {$_ -like 'info.version=*'}
    #[System.Windows.MessageBox]::Show('Is this a till or hub?','GK Tills')
    $textBox.Hide()
    $LabelHubName.Text=$DeviceName.ToString()
    $OKButton.Hide()
    $Form.Controls.Add($LogButton)
    $LogButton.Add_Click({Check_FileExistsHub})
    }
    
    Function LogButtonHub
    {
    
    
    [System.Windows.MessageBox]::Show('Is this a till or hub?','GK Tills')
    
    
    
    }
    
    
    Function TillButtonClick{
    [System.Windows.MessageBox]::Show('Is this a till or hub?','GK Tills')
    
    
    }
    
    #Hub Button
    Function HubButtonClick{
    
    $form.Controls.Add($textBox)
    $Form.Controls.Add($LabelHubName)
    $Label.Hide()
    $TillButton.Hide()
    $HubButton.Hide()
    $form.Controls.Add($OKButton)
    $OKButton.Add_Click({OKButtonHub})
    $LabelHubName.Text.ToString($HubName)
    #$Global:LabelHubName.Text.ToString($HubName)
    }
    
    
    
    
    
    Function Check_FileExistsHub
    {
    $DateAndTime = (Get-Date -Format g)
    $NewLine = "`r`n"
    $LogFileEntry = "$DateAndTime Info Client Version " +$LabelHubName -replace "System.Windows.Forms.Label, Text: info.version=v",""
    if(!(Test-Path $Global:LogPath))
    {
    
    New-Item -path "C:\GK\GK.txt" -ItemType File -Force -Value $LogFileEntry 
    [System.Windows.MessageBox]::Show('Log file created to '+ $Global:LogPath)
    $Form.Close()
    Invoke-Item $Global:LogFolder
    }
    else
    {
    
    Add-Content -Path "C:\GK\GK.txt" -Value  $logfileentry 
    [System.Windows.MessageBox]::Show('Log file modified at '+ $Global:LogPath)
    $Form.Close()
    Invoke-Item $Global:LogFolder
    }
    }
    
    
    
    
    #Show-Command 
    #$ComputerName = "";
    
    $Java = 'C:\Program Files (x86)\java\jre\bin\java.exe'
                
    #$TillHub=[System.Windows.MessageBox]::Show('Is this a till or hub?','GK Tills')
    
        
    
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    $Global:LogPath = "C:\GK\GK.txt"
    $Global:LogFolder ="C:\GK\"
    $Form = New-Object System.Windows.Forms.Form
    $Form.width = 250
    $Form.height = 178
    $Form.Text='GK Tills'
    $Form.Font=new-object System.Drawing.Font("Verdana",10)
    $form.MinimizeBox = $False
    $form.MaximizeBox = $False
    $form.SizeGripStyle = "Hide"
    $form.WindowState = "Normal"
    $form.StartPosition = "CenterScreen"
    $form.BackColor='darkcyan'
    $textBox = New-Object System.Windows.Forms.TextBox
    $textBox.Location = New-Object System.Drawing.Point(30,50)
    $textBox.Size = New-Object System.Drawing.Size(150,100)
    $LabelHubName= New-Object System.Windows.Forms.Label
    $LabelHubName.Location=New-Object System.Drawing.Point(40,30)
    $LabelHubName.Size=New-Object System.Drawing.Size(280,20)
    $LabelHubName.Text='Enter Hub name'
    $labelHubName.Font = New-Object System.Drawing.Font("Arial",11,[System.Drawing.FontStyle]::Regular)
    $LblTillName= New-Object System.Windows.Forms.Label
    $LblTillName.Location=New-Object System.Drawing.Point(40,30)
    $LblTillName.Size=New-Object System.Drawing.Size(280,20)
    $LblTillName.Text='Enter Hub name'
    $LblTillName.Font = New-Object System.Drawing.Font("Arial",11,[System.Drawing.FontStyle]::Regular)
    
    $Label = New-Object System.Windows.Forms.Label
    $Label.Location=New-Object System.Drawing.Point(40,30)
    $Label.Size=New-Object System.Drawing.Size(280,20)
    $Label.Text='Is this a till or hub?'
    $label.Font = New-Object System.Drawing.Font("Arial",11,[System.Drawing.FontStyle]::Regular)
    $Form.Controls.Add($Label)
    #Till button
    $TillButton = New-Object System.Windows.Forms.Button
    $TillButton.Location = New-Object System.Drawing.Point(125,75)
    $TillButton.Size = New-Object System.Drawing.Size(80,30)
    $TillButton.Text = "Till"
    $TillButton.BackColor='White'
    #Hub button
    $HubButton = New-Object System.Windows.Forms.Button
    $HubButton.Location = New-Object System.Drawing.Point(25,75)
    $HubButton.Size = New-Object System.Drawing.Size(80,30)
    $HubButton.Text = "Hub"
    $HubButton.BackColor='White'
    #OK Button
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point(30,75)
    $OKButton.Size = New-Object System.Drawing.Size(50,30)
    $OKButton.Text = "OK"
    $OKButton.BackColor='White'
    
    #Log Button
    $LogButton = New-Object System.Windows.Forms.Button
    $LogButton.Location = New-Object System.Drawing.Point(30,75)
    $LogButton.Size = New-Object System.Drawing.Size(50,30)
    $LogButton.Text = "Create Log"
    $LogButton.BackColor='White'
    
    $HubButton.Add_Click({HubButtonClick})
    $HubButton.Add_Click({TillButtonClick})
    $Form.Controls.Add($TillButton)
    $Form.Controls.Add($HubButton)
    $Form.ShowDialog()
    


    Tuesday, October 30, 2018 4:13 PM

All replies

  • Could you please edit your post and format your code as code? Use the code formating tool provided on the edit bar of post editor.

    Thanks


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Tuesday, October 30, 2018 4:36 PM
  • 4th line from the bottom.

    Shouldn't this:

    $HubButton.Add_Click({TillButtonClick})

    Really be this?

    $TillButton.Add_Click({TillButtonClick})


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Tuesday, October 30, 2018 6:25 PM
  • Apologies i've edited the post.
    Monday, November 5, 2018 11:49 AM
  • Hi

    Thank you very much so it should!! Totally missed that! Really appreciate that! School boy error on my part!

    Thanks

    Stephen

    Monday, November 5, 2018 11:51 AM