Hello Everyone,
I am struggling with a form, in combination with a Textbox and data entered into it. I started to build a test script that works as shown in example 1 and then extended it and need to do some minor changes to make it work.
In example 1: I generate a Form, ask for a Name and after a data input and pressing OK, a second form opens to present the data in a label. That works fine and I would like to have it on such way.
CLS
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
#New User Form
$TestForm = New-Object System.Windows.Forms.Form
$TestForm.Text = "Test Name Input"
$TestForm.AutoScroll = $True
$TestForm.AutoSizeMode = "GrowAndShrink"
$TestForm.MinimizeBox = $True
$TestForm.MaximizeBox = $True
$TestForm.WindowState = "Normal"
$TestForm.Size = New-Object System.Drawing.Size(1000,1000)
$TestForm.StartPosition = 'CenterScreen'
#New User Form Title
$TestFormTitle = New-Object System.Windows.Forms.Label
$TestFormTitle.Location = New-Object System.Drawing.Point(10,30)
$TestFormTitle.Size = New-Object System.Drawing.Size(450,50)
$TestFormTitleFont = New-Object System.Drawing.Font("Times New Roman",26,[System.Drawing.FontStyle]::Bold)
$TestFormTitle.Font = $TestFormTitleFont
$TestFormTitle.Text = "This is a Test"
$TestForm.Controls.Add($TestFormTitle)
#New User Form Label Firstname
$TestFormFormLabelFirstname = New-Object System.Windows.Forms.Label
$TestFormFormLabelFirstname.Location = New-Object System.Drawing.Point(10,100)
$TestFormFormLabelFirstname.Size = New-Object System.Drawing.Size(885,40)
$TestFormFormLabelFirstnameFont = New-Object System.Drawing.Font("Arial",16,[System.Drawing.FontStyle]::Italic)
$TestFormFormLabelFirstname.ForeColor = "DarkBlue"
$TestFormFormLabelFirstname.Font = $TestFormFormLabelFirstnameFont
$TestFormFormLabelFirstname.Text = "Please Enter Firstname"
$TestForm.Controls.Add($TestFormFormLabelFirstname)
#New User Form Textbox Firstname
$TestFormTextBoxFirstname = New-Object System.Windows.Forms.TextBox
$TestFormTextBoxFirstname.Location = New-Object System.Drawing.Point(10,140)
$TestFormTextBoxFirstname.Size = New-Object System.Drawing.Size(885,100)
$TestFormTextBoxFirstnameFont = New-Object System.Drawing.Font("Arial",20,[System.Drawing.FontStyle]::Italic)
$TestFormTextBoxFirstname.Font = $TestFormTextBoxFirstnameFont
$TestForm.Controls.Add($TestFormTextBoxFirstname)
#Create OK Button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(190,860)
$OKButton.Size = New-Object System.Drawing.Size(200,75)
$OKButton.Font = New-Object System.Drawing.Font("Arial",16,[System.Drawing.FontStyle]::Regular)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$TestForm.AcceptButton = $OKButton
$TestForm.Controls.Add($OKButton)
$result = $TestForm.ShowDialog()
#Confirmation Name Form
$ConfirmatonNameform = New-Object System.Windows.Forms.Form
$ConfirmatonNameform.Text = "Confirmation Name"
$ConfirmatonNameform.Size = "1300, 780"
$ConfirmatonNameform.StartPosition = "CenterScreen"
$ConfirmatonNameform.Icon = $ConfirmatonNewUserformIcon
$ConfirmatonNameform.AutoSize = $False
$ConfirmatonNameform.BringToFront()
#Confirmation New User Firstname Label
$ConfirmatonNameLabel = New-Object System.Windows.Forms.Label
$ConfirmatonNameLabel.Location = New-Object System.Drawing.Point(10,80)
$ConfirmatonNameLabel.Size = New-Object System.Drawing.Size(200,50)
$ConfirmatonNameLabelFont = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Regular)
$ConfirmatonNameLabel.ForeColor = "DarkBlue"
$ConfirmatonNameLabel.Font = $ConfirmatonNameLabelFont
$ConfirmatonNameLabel.Text = "Firstname:"
$ConfirmatonNameform.Controls.Add($ConfirmatonNameLabel)
#Confirmation New User Firstname Data
$ConfirmatonNamevar = New-Object System.Windows.Forms.Label
$ConfirmatonNamevar.Location = New-Object System.Drawing.Point(400,80)
$ConfirmatonNamevar.Size = New-Object System.Drawing.Size(700,50)
$ConfirmatonNamevarFont = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Regular)
$ConfirmatonNamevar.ForeColor = "Black"
$ConfirmatonNamevar.Font = $ConfirmatonNamevarFont
$ConfirmatonNamevar.text = $TestFormTextBoxFirstname.text
$ConfirmatonNameform.Controls.Add($ConfirmatonNamevar)
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{$ConfirmatonNameform.ShowDialog()}
I have extended this form and because I have several Textboxes, I wanted to have a mechanism, to prevent a textbox left-out without any data entered.
In example 2 you can see how the code looks slightly different.
CLS
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
#New User Form
$TestForm = New-Object System.Windows.Forms.Form
$TestForm.Text = "Test Name Input"
$TestForm.AutoScroll = $True
$TestForm.AutoSizeMode = "GrowAndShrink"
$TestForm.MinimizeBox = $True
$TestForm.MaximizeBox = $True
$TestForm.WindowState = "Normal"
$TestForm.Size = New-Object System.Drawing.Size(1000,1000)
$TestForm.StartPosition = 'CenterScreen'
#New User Form Title
$TestFormTitle = New-Object System.Windows.Forms.Label
$TestFormTitle.Location = New-Object System.Drawing.Point(10,30)
$TestFormTitle.Size = New-Object System.Drawing.Size(450,50)
$TestFormTitleFont = New-Object System.Drawing.Font("Times New Roman",26,[System.Drawing.FontStyle]::Bold)
$TestFormTitle.Font = $TestFormTitleFont
$TestFormTitle.Text = "This is a Test"
$TestForm.Controls.Add($TestFormTitle)
#New User Form Label Firstname
$TestFormFormLabelFirstname = New-Object System.Windows.Forms.Label
$TestFormFormLabelFirstname.Location = New-Object System.Drawing.Point(10,100)
$TestFormFormLabelFirstname.Size = New-Object System.Drawing.Size(885,40)
$TestFormFormLabelFirstnameFont = New-Object System.Drawing.Font("Arial",16,[System.Drawing.FontStyle]::Italic)
$TestFormFormLabelFirstname.ForeColor = "DarkBlue"
$TestFormFormLabelFirstname.Font = $TestFormFormLabelFirstnameFont
$TestFormFormLabelFirstname.Text = "Please Enter Firstname"
$TestForm.Controls.Add($TestFormFormLabelFirstname)
#New User Form Textbox Firstname
$TestFormTextBoxFirstname = New-Object System.Windows.Forms.TextBox
$TestFormTextBoxFirstname.Location = New-Object System.Drawing.Point(10,140)
$TestFormTextBoxFirstname.Size = New-Object System.Drawing.Size(885,100)
$TestFormTextBoxFirstnameFont = New-Object System.Drawing.Font("Arial",20,[System.Drawing.FontStyle]::Italic)
$TestFormTextBoxFirstname.Font = $TestFormTextBoxFirstnameFont
$TestForm.Controls.Add($TestFormTextBoxFirstname)
#Create OK Button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(190,860)
$OKButton.Size = New-Object System.Drawing.Size(200,75)
$OKButton.Font = New-Object System.Drawing.Font("Arial",16,[System.Drawing.FontStyle]::Regular)
$OKButton.Text = 'OK'
$TestForm.AcceptButton = $OKButton
$TestForm.Controls.Add($OKButton)
#Check if Name has been entered
$OKButtonClickEvent = (
{if (!$TestFormTextBoxFirstname.text -and !$TestFormTextBoxFirstname.text -eq 'True') {[System.Windows.MessageBox]::Show('No data entered in Firstname','Ooops','OK','Warning')}
Else {$TestForm.Hide(),$ConfirmationNameform.ShowDialog()}
}
)
$OKButton.Add_Click($OKButtonClickEvent)
$TestForm.ShowDialog()
#Confirmation Name Form
$ConfirmationNameform = New-Object System.Windows.Forms.Form
$ConfirmationNameform.Text = "Confirmation Name"
$ConfirmationNameform.Size = "1300, 780"
$ConfirmationNameform.StartPosition = "CenterScreen"
$ConfirmationNameform.Icon = $ConfirmationNewUserformIcon
$ConfirmationNameform.AutoSize = $False
$ConfirmationNameform.BringToFront()
#Confirmation New User Firstname Label
$ConfirmationNameLabel = New-Object System.Windows.Forms.Label
$ConfirmationNameLabel.Location = New-Object System.Drawing.Point(10,80)
$ConfirmationNameLabel.Size = New-Object System.Drawing.Size(200,50)
$ConfirmationNameLabelFont = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Regular)
$ConfirmationNameLabel.ForeColor = "DarkBlue"
$ConfirmationNameLabel.Font = $ConfirmationNameLabelFont
$ConfirmationNameLabel.Text = "Firstname:"
$ConfirmationNameform.Controls.Add($ConfirmationNameLabel)
#Confirmation New User Firstname Data
$ConfirmationNamevar = New-Object System.Windows.Forms.Label
$ConfirmationNamevar.Location = New-Object System.Drawing.Point(400,80)
$ConfirmationNamevar.Size = New-Object System.Drawing.Size(700,50)
$ConfirmationNamevarFont = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Regular)
$ConfirmationNamevar.ForeColor = "Black"
$ConfirmationNamevar.Font = $ConfirmationNamevarFont
$ConfirmationNamevar.text = $TestFormTextBoxFirstname.text
$ConfirmationNameform.Controls.Add($ConfirmationNamevar)
The ClickEvent (thanx jrv) checks if data has been entered and if not, will give a warning. This also works perfectly and I was happy but then, I faced a new problem:
As soon as the second form opens, for some reason, the content of the textbox in the example called "$TestFormTextBoxFirstname.text" is empty.
What I was able to find out is, that if I run the Script a second time, I get the UserInput from the first try and if I understand correctly, that means, that the name entered in example 2 and stored as a variable, runs through without being forwarding to
the second form to "$ConfirmationNamevar.text". I was playing around, trying different approaches but was not able to fix it.
I tried this approach, because I had no clue, how to show "$TestFormTextBoxFirstname.text" directly in the Label. Here, I also tried several ways but failed also.
What am I doing wrong? Can someone help with a solution, so I can keep the ClickEvent and have the content of the Textbox transferred into the Second Form, so the name is shown, as it works correctly in example 1?
Thank you very much for your help,
Mike