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
functions and parameters
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.
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.
Re: functions and parameters
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.
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.
Re: functions and parameters
Thanks for answering on this. But could you give me another hint, how to pass an object to a function?
Thanks again
Thanks again
Re: functions and parameters
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()
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()
Re: functions and parameters
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:
Call the function like this from any form:
copyright $MainWindow
The call must be at the root of the form function before the "ShowDialog()".
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:
- function copyright{
- Param(
- $Window
- )
- # in here is the code to insert the copy right logo
- }
copyright $MainWindow
The call must be at the root of the form function before the "ShowDialog()".
Re: functions and parameters
I am sorry - I just might be too stupid, to get this.... ?!
When I call the function (from the main window) like you said:
....and do a in the function, I do not get anything....
I think, that the issue is, that I have to have
either
or
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)
When I call the function (from the main window) like you said:
Code: Select all
copyright $this
Code: Select all
write-host $window
I think, that the issue is, that I have to have
either
Code: Select all
$Mainwindow.Controls.Add($copyRight)
Code: Select all
$second_window.Controls.Add($copyRight)
$Mainwindow.Controls.Add($copyRight) or $second_window.Controls.Add($copyRight)