functions and parameters

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
newbi2009
Posts: 6
Last visit: Fri Mar 18, 2022 2:21 am

functions and parameters

Post by newbi2009 »

Hi there,
I am experimenting with functions, at the moment and do have the following issue:

I have a little GUI-based program, which has several windows.
I now want to add a little "copy right -logo" into every windows. Since I do not want to add the copy-right-code into every window, I am trying to arange this with a function.

My problem is, to tell the function to appear in the windows, I want it to appear:
Let's say, my window is named "$StartWindow", then the code to should be:
$StartWindow.Controls.Add($copyright)

Means: I somehow have to hand over the window-name "Startwindow" into the function
------------------------------------------------------
My function looks like this:

function copyright([string]$window)
{
in here is the code to insert the copy right logo
}
-----------------------------------------
I call the function like this:

copyright -window "Startwindow"
----------------------------------------------
When I do a "write-host $window" within the function, It shows "Startwindow" - so, this is okay!
------------------------------------------------------------------------
But I have no idea, how can I get the command

$StartWindow.Controls.Add($copyright)

done within the function, now??

Thanks
for any help
newbi
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: functions and parameters

Post by jvierra »

You have to use objects. Just pass teh Window object and add to that controls collection.

WHat you are trying to do is totally unnecessary. Just add a copyright to each window. In the end it doesn't accomplish much to try to change a windo that doesn't yet exist. You cannot add to an object that hasn't been created.
newbi2009
Posts: 6
Last visit: Fri Mar 18, 2022 2:21 am

Re: functions and parameters

Post by newbi2009 »

Thanks for answering on this. But could you give me another hint, how to pass an object to a function?

Thanks again
newbi2009
Posts: 6
Last visit: Fri Mar 18, 2022 2:21 am

Re: functions and parameters

Post by newbi2009 »

Maybe it helps to post my code:
Can somebody tell me, how to get the copyright-button from the function into the mainwindow and the second window?
Thank you very much

function copyright([object]$window){

$copyRight = New-Object System.Windows.Forms.Button
$copyRightFont = New-Object System.Drawing.Font("Calibri",9,[System.Drawing.FontStyle]::Italic)
$copyRight.Font = $copyRightFont
$copyRight.Location = New-Object System.Drawing.Size(1,150)
$copyRight.Size = New-Object System.Drawing.Size(105,20)
$copyRight.Forecolor = "gray"
$copyRight.Text = "@CopyRight-owner"

####### $Mainwindow.Controls.Add($copyRight) or $second_window.Controls.Add($copyRight)
}

#---------------------------Mainwindow--------------------------------------------------

$Mainwindow = New-Object System.Windows.Forms.Form
$Mainwindow.Backcolor="white"
$Mainwindow.StartPosition = "CenterScreen"
$Mainwindow.Size = New-Object System.Drawing.Size(400,200)
$Mainwindow.Text = "this is the Mainwindow"

copyright -window Mainwindow

#----------------------button for 2. window----------------------------------------

$button = New-Object System.Windows.Forms.Button
$button.Backcolor="lightblue"
$button.Location = New-Object System.Drawing.Size(50,50)
$button.Size = New-Object System.Drawing.Size(100,33)
$button.Text = "open second window"
$button.Add_Click({
#----------------------second window-------------------------------------------------

$second_window = New-Object System.Windows.Forms.Form
$second_window.Backcolor="white"
$second_window.StartPosition = "CenterScreen"
$second_window.Size = New-Object System.Drawing.Size(400,200)
$second_window.Text = "this is the second window"

copyright -window second_window

$second_window.ShowDialog()

})

$Mainwindow.Controls.Add($button)

$Mainwindow.ShowDialog()
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: functions and parameters

Post by jvierra »

You have only one form so a function is not useful.

If you want to generalize eavh form with a function you will have to exefdcute the function from inside the form and call it at the end of the form code before the ShowDialog call.

The function will work on any form if you write it like this:
  1. function copyright{
  2.     Param(
  3.            $Window
  4.     )
  5.     # in here is the code to insert the copy right logo
  6.    
  7. }
Call the function like this from any form:

copyright $MainWindow

The call must be at the root of the form function before the "ShowDialog()".
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: functions and parameters

Post by jvierra »

Sorry but I screwed up the instructions. I will fix it.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: functions and parameters

Post by jvierra »

Original post fixed.
newbi2009
Posts: 6
Last visit: Fri Mar 18, 2022 2:21 am

Re: functions and parameters

Post by newbi2009 »

I am sorry - I just might be too stupid, to get this.... ?!

When I call the function (from the main window) like you said:

Code: Select all

copyright $this
....and do a

Code: Select all

write-host $window
in the function, I do not get anything....

I think, that the issue is, that I have to have

either

Code: Select all

$Mainwindow.Controls.Add($copyRight)
or

Code: Select all

$second_window.Controls.Add($copyRight)
to get the copyright button shown in one or the other window. But this line is missing. And I do not know, how to "produce" it


$Mainwindow.Controls.Add($copyRight) or $second_window.Controls.Add($copyRight)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: functions and parameters

Post by jvierra »

You cannot use Write-Host to display anything in a GUI. You have to use a control and assign the text to it.
This topic is 2 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked