clearing a textbox which is in a function - powershell

i have a function that create a textbox (info box) and can be called from other functions or from buttons.
function InfoBox ($x,$y,$text){
$TextboxInfoBox = New-Object System.Windows.Forms.TextBox
$TextboxInfoBox.Location = New-Object System.Drawing.Size($x,$y)
$TextboxInfoBox.Size = New-Object System.Drawing.Size(200,800)
# Readonly Textbox.
$TextboxInfoBox.Enabled = $true
$TextboxInfoBox.Text = $text
$TabPanelButtons.Controls.Add($TextboxInfoBox)
}
After i called the textbox once, i am not able to overwrite it with another info message.
the first time i call with all the parameters ($x, $y & $text).
Infobox -x '100' -y '150' -text 'This is the first message'
The second time i only want to give another text. How can i clear the textbox and give only a new text like:
Infobox -text "This is the second message"
Any Ideas?

As commenter wrote, the function has to output the TextBox object so you can reference it later to change the text:
function InfoBox ($x,$y,$text){
$TextboxInfoBox = New-Object System.Windows.Forms.TextBox
$TextboxInfoBox.Location = New-Object System.Drawing.Size($x,$y)
$TextboxInfoBox.Size = New-Object System.Drawing.Size(200,800)
# Readonly Textbox.
$TextboxInfoBox.Enabled = $true
$TextboxInfoBox.Text = $text
$TabPanelButtons.Controls.Add($TextboxInfoBox)
$TextboxInfoBox # Output
}
Contrary to most other programming languages, in PowerShell you normally don't need to use the return statement, except for early return from a function. Simply referring to a variable by name on its own line will output its value from the function.
Now you can store the output in a variable and change the text:
$box = Infobox -x 100 -y 150 -text 'This is the first message'
$box.Text = 'This is the second message'

Related

Using HEX character on forms button, but one character isn`t displaying - others work fine

I'm trying to use a 'forward' arrow character on my form button ([char]0x84 from the WingDings 3 font), to match the 'back' arrow ([char]0x83), but for some reason the 'forward' arrow doesn't show on the button. I've tried other HEX values and they work fine. HEX values were sourced from MS Word. Below is my MRE showing correct "back" button, blank "forward" button, plus a spare.
I could just use different symbols, but curious to know if there is a reason for this one character not working.
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$Form.Size = '800,600'
$buttFACE1 = [char]0x83 # Back arrow from WingDings3
$buttFACE2 = [char]0x84 # Forward arrow from WingDings3 <- NOT WORKING
$buttFACE3 = [char]0x86 # Spare button for testing
$navFONT = New-Object System.Drawing.Font("WingDings 3","14",[System.Drawing.FontStyle]::Bold)
#################################################
# NAV BACK BUTTON
$navBACK = New-Object System.Windows.Forms.Button
$navBACK.Location = '10,10'
$navBACK.Size = '30,30'
$navBack.Font = $navFONT
$navBACK.Text = $buttFACE1
$navBACK.Enabled = $True
$Form.Controls.Add($navBACK)
####################################################
# NAV FORWARD BUTTON
$navFORWARD = New-Object System.Windows.Forms.Button
$navFORWARD.Location = '50,10'
$navFORWARD.Size = '30,30'
$navFORWARD.Font = $navFONT
$navFORWARD.Text = $buttFACE2
$navFORWARD.Enabled = $True
$Form.Controls.Add($navFORWARD)
##################################################
# SPARE BUTTON
$navSPARE = New-Object System.Windows.Forms.Button
$navSPARE.Location = '90,10'
$navSPARE.Size = '30,30'
$navSPARE.Font = $navFONT
$navSPARE.Text = $buttFACE3
$navSPARE.Enabled = $True
$Form.Controls.Add($navSPARE)
$Form.ShowDialog()
Thanks for taking the time to read this. This forum has been a massive help since my organisation decided HTA's were too risky (I know, right!).

How to Clear a Chart form

i'm currently working on a project (WOL) where i have a program that launch as a window form and have few components which one of them is a chart form.
Basically, the program can re-launch multiple times as the user wants but i'm trying to clear/reset the chart form (that is in Pie style) at each launch = delete the previous datas of the chart.
My code is way too long so i will post only the concerned parts
$WOL_W = New-Object System.Windows.Forms.Form
.... (skipping useless code part)
function Chart {
param(
[Parameter(Mandatory)]
[PSCustomObject[]]
$Results
)
$G_Graphique = New-object System.Windows.Forms.DataVisualization.Charting.Chart
$G_Graphique.Location = New-Object System.Drawing.Point(300,350)
$WOL_W.Controls.Add($G_Graphique)
$Wol_W.Add_Shown({$Graphique.Activate()})
$G_Graphique.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right -bor [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left
# Objet Zone Graphique (ZG_)
$ZG_GraphArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
$G_Graphique.ChartAreas.Add($ZG_GraphArea)
[void]$G_Graphique.Titles.Add("Réussite du WOL")
$G_Graphique.BackColor = [System.Drawing.Color]::White
$G_Graphique.BorderColor = 'Black'
$G_Graphique.BorderDashStyle = 'Solid'
[void]$G_Graphique.Series.Add("Data")
$G_Graphique.Series["Data"].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Pie
$G_Graphique.Series["Data"].Points.DataBindXY($Results.Keys,$Results.Values)
}
Note : i've tried to use the followings command in another function (function of the creation for the main window) :
$G_Graphique.ChartAreas.Clear()
$Wol_W.Controls.Remove($G_Graphique)
Which failed as i want.

Powershell dot source GUI script and return value when button clicked

I am dot sourcing a script which builds a GUI form.
The user puts a value in a text box, some input validation happens and then if it passes the user can click a button: The Apply button. (e.g to save the value)
So I want that value returned to my script1 as below:
Any recommendations for a neat way to do this?
#Script1.ps1
. ($PSScriptRoot + "\GUIForm.ps1"
InitialiseForm
#When the Ok button is pressed in the GuiForm.ps1 script.
#Store value to here to parse elsewhere
#GUIForm.ps1
$TheTextIWantFromTextBox = New-Object system.Windows.Forms.TextBox
$OkButton.Add_Click({SubmitTextBox})
Function SubmitTextBox()
{
$Form.close()
return $TheTextIWantFromTextBox.Text
}
Function InitialiseForm()
{
[void]$Form.ShowDialog()
}
Note: I have omitted all the code that creates the form etc...If this is relevant I will update!
Just capture what is in the text box on the button click event.
You don't have to show your whole form, just part of it that is relevant.
You could have just created a simple dialog for the same use case.
What you are asking for is a very common task. There are literally tons of examples and videos on YouTube that show how to do this. As well as on the very site.
What you are asking for is a very common task. See the example here:
Powershell Custom GUI input box for passing values to Variables
I shortened the code for this reply.
function button ($mailbx)
{
###################Load Assembly for creating form & button######
[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
#####Define the form size & placement
$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 500;
$form.Height = 150;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
##############Define text label1
$textLabel1 = New-Object “System.Windows.Forms.Label”;
$textLabel1.Left = 25;
$textLabel1.Top = 15;
$textLabel1.Text = $mailbx;
############Define text box1 for input
$textBox1 = New-Object “System.Windows.Forms.TextBox”;
$textBox1.Left = 150;
$textBox1.Top = 10;
$textBox1.width = 200;
#############Define default values for the input boxes
$defaultValue = “”
$textBox1.Text = $defaultValue;
#############define OK button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 85;
$button.Width = 100;
$button.Text = “Ok”;
############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler] {
$textBox1.Text;
$textBox2.Text;
$textBox3.Text;
$form.Close();
};
$button.Add_Click($eventHandler) ;
#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($textLabel1);
$form.Controls.Add($textBox1);
$ret = $form.ShowDialog();
#################return values
return $textBox1.Text
}
$return = button 'Enter Folders' 'Enter mailbox'
# Below variables will get the values that had been entered by the user
$return
This is just one approach, there are other ways to capture and return text, for example, not showing any form code, just the return, from another simple dialog WinForm…
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
($FName = $FName.Text )
($LName = $Lname.Text)
}
The same thing can be done for WPF (Windows Presentation Foundation) style forms as well.

PowerShell GUI observer several socket-connections

I have found lots of examples that are all very long - but I think it can be done in a lot shorter way.
I nee a GUI that displays and enables several options of the socket connections I need to adimn.
In the beginning I think I have:
# Load external assemblies
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$OnLoadForm_StateCorrection = {
$form1.WindowState = $InitialFormWindowState
}
$myGUI = New-Object System.Windows.Forms.Form
$myGUI.Text = "Socket-Traffic"
$myGUI.Name = "myGUI"
$myGUI.DataBindings.DefaultDataSourceUpdateMode = 0
$myGUI.ForeColor = [System.Drawing.Color]::FromArgb(255,0,0,255)
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 332
$System_Drawing_Size.Height = 264
$myGUI.ClientSize = $System_Drawing_Size
Now I have the list of Hashes containing all relevant information of the variables and the connections. The groupe just has to be able to access their hash:
$Sockets = $HASH1,$HASH2,$HASH3,$HASH4 # all hasches have a [string] ID: $HASH1.ID = $port
$n = 0 # do I need that?
foreach ($h in $Sockets) {
makeTab $myGUI $h $n
$n++
}
the function that I have in mind should start like that:
function makeTab {
param (
[parameter(Mandatory=$true)]
[PSObject] $gui,
[parameter(Mandatory=$true)]
[PSObject] $hashTable, # with all info to connect and vars.
[parameter(Mandatory=$true)]
[int] $noTab
)
... ??
}
Each Socket-Tab has to have these internal groupes:
all function calls behind a buttonclick like:
$x_OnClick = { Write-host "Button clicked, do..." }
1) Send-a-Line Groupe
a) Line to enter test meant to sent.
b) Button to send # no 'cancle'
2) Login-Groupe:
a) status: Login=Green.ico, Connected=Orange.ico, nothing=red.ico
b) Button: Login (connect & login)
c) Button: Logout (Logout & disconnect)
3) Logging-Groupe:
a) last 2 Lines been sent
b) last 2 line received
4) Status Groupe
a) text lines with var. info. about the connection
b) text lines with var. info. about the connection
...
Global - Finally
a) Button to Exit the script with logout & disconnect all socket connections...
May s.o. can draft an example? After that I can determine the size and the place of the various and the buttons within a group.
Thank you very much in advance!
Gooly
If you are able to use PowerSHell 3.0 or higher, you can use WPF, where forms are just XML-like text. You can create an XML form in visual designer, like Visual Studio Express.
Like this: TreeView Example

Powershell pass a function as a parameter

I am a newbie to the world of programming and I am trying to create a form using functions to create the buttons and labels etc. The form is created with the exception that the functions passed to on button click events are not being passed correctly. For example I have a function to create a button....
function new_btn ($name, $parent, $x, $y, $l, $h, $text, $onClick){
$object = New-Object System.Windows.Forms.Button
$object.Location = New-Object System.Drawing.Point($x, $y)
$Object.Size = New-Object System.Drawing.Size($l, $h)
$Object.Text = $text
$object.add_Click({$onClick})
New-Variable $name -Value $object -Scope global
(Get-Variable $parent).Value.Controls.Add((Get-Variable $name).value)
}
I then have the function that I want to run on the button click.....
function msg {
[System.Windows.Forms.MessageBox]::Show("We are proceeding with next step.")
}
I then call the function and feed it the parameters.......
new_btn getdbslist tab1 20 50 69 23 "Get DB's" msg
This produces the button as expected and adds it to tab1, but the on click event will not work, nothing happens at all. Any help would be very appreciated!
You are just passing a string. Instead pass a script block:
new_btn getdbslist tab1 20 50 69 23 'Get DBs' {
[System.Windows.Forms.MessageBox]::Show("We are proceeding with next step.")
}
And in your new_btn function you probably just need to use
$object.add_Click($onClick)
If you really want to pass a string, then you probably need to use the following:
$object.add_Click({ & $onClick })