0

I need it to not close when user press enter or exit so that it mandate for user to make entry for all three box. I am new to powershell so I might be sounding akward but I can't find a good solution anywhere.   1st Textbox for username 2nd Dropbox for Country 3rd Dropbox for City

Also want heading of each box

function GenerateForm {

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
#endregion

#region Generated Form Objects
$form1 = New-Object System.Windows.Forms.Form
$button1 = New-Object System.Windows.Forms.Button
$button2 = New-Object System.Windows.Forms.Button
$textBox2 = New-Object System.Windows.Forms.TextBox
$textBox1 = New-Object System.Windows.Forms.TextBox
$comboBox1 = New-Object System.Windows.Forms.ComboBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

    #region $handler_comboBox1_SelectedIndexChanged
$handler_comboBox1_SelectedIndexChanged=
{
#TODO: Place custom script here
    try{
    if ($comboBox1.Text.Length -gt 0)
    {
        $form1.Text = $comboBox1.Text;
    }
    }catch{} #in case no items are available
}
    #endregion $handler_comboBox1_SelectedIndexChanged
    #region $handler_button1_Click
$handler_button1_Click=
{
#TODO: Place custom script here
    try{
    #How to add one item
    #$textBox1.Text
    if ($textBox1.Text.Length -gt 0 -and `
        $textBox2.Text.Length -eq 0)
    {
        $comboBox1.Items.Add($textBox1.Text);
        Write-Host "Added " $textBox1.Text "from `$textBox1";
    }
    #$textBox2.Text
    elseif ($textBox1.Text.Length -eq 0 -and `
        $textBox2.Text.Length -gt 0)
    {
        $comboBox1.Items.Add($textBox2.Text);
        Write-Host "Added " $textBox2.Text "from `$textBox2";
    }
    #$textBox1.Text and $textBox2.Text
    elseif ($textBox1.Text.Length -gt 0 -and `
        $textBox2.Text.Length -gt 0)
    {
        $comboBox1.Items.AddRange(($textBox1.Text, $textBox2.Text));
        Write-Host "Added " $textBox1.Text "from `$textBox1";
        Write-Host "Added " $textBox2.Text "from `$textBox2";
    }
    else
    {
        #Nothing to add
        #$textBox1.Text.Length -eq 0
        #$textBox2.Text.Length -eq 0
    }
    #At last, if the user typed something
    if ($comboBox1.Text.Length -gt 0)
    {
        $comboBox1.Items.Add($comboBox1.Text);
        Write-Host "Added " $comboBox1.Text "from `$comboBox1";
    }
    }catch{} #for example, no data in the both textboxes
}
    #endregion $handler_button1_Click
    #region $handler_button2_Click
$handler_button2_Click=
{
#TODO: Place custom script here
    try{
    #Clean up the combo box
    $comboBox1.Items.Clear();
    $comboBox1.Text = "";
    }catch{}
}
    #endregion $handler_button2_Click
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
    $form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
    #region $form1
$form1.Text = "Input Form"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 282
$System_Drawing_Size.Height = 220
$form1.ClientSize = $System_Drawing_Size
    #endregion $form1
    #region $button1
$button1.TabIndex = 3
$button1.Name = "Enter"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 70
$System_Drawing_Size.Height = 30
$button1.Size = $System_Drawing_Size
$button1.UseVisualStyleBackColor = $True
$button1.Text = "Enter"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 195
$System_Drawing_Point.Y = 160
$button1.Location = $System_Drawing_Point
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
$button1.add_Click($handler_button1_Click)
$form1.Controls.Add($button1)
    #endregion $button1
    #region $button2
$button2.TabIndex = 4
$button2.Name = "Exit"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 70
$System_Drawing_Size.Height = 30
$button2.Size = $System_Drawing_Size
$button2.UseVisualStyleBackColor = $True
$button2.Text = "Exit"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 15
$System_Drawing_Point.Y = 160
$button2.Location = $System_Drawing_Point
$button2.DataBindings.DefaultDataSourceUpdateMode = 0
$button2.add_Click($handler_button2_Click)
$form1.Controls.Add($button2)
    #endregion $button2
    #region $textBox1
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 250
$System_Drawing_Size.Height = 20
$textBox1.Size = $System_Drawing_Size
$textBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$textBox1.Name = "textBox1"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 15
$System_Drawing_Point.Y = 70
$textBox1.Location = $System_Drawing_Point
$textBox1.TabIndex = 1
$form1.Controls.Add($textBox1)
    #endregion $textBox1
    #region $textBox2
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 250
$System_Drawing_Size.Height = 20
$textBox2.Size = $System_Drawing_Size
$textBox2.DataBindings.DefaultDataSourceUpdateMode = 0
$textBox2.Name = "textBox2"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 15
$System_Drawing_Point.Y = 120
$textBox2.Location = $System_Drawing_Point
$textBox2.TabIndex = 2
$form1.Controls.Add($textBox2)
    #endregion $textBox2
    #region $comboBox1
$comboBox1.FormattingEnabled = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 250
$System_Drawing_Size.Height = 21
$comboBox1.Size = $System_Drawing_Size
$comboBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$comboBox1.Name = "comboBox1"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 15
$System_Drawing_Point.Y = 20
$comboBox1.Location = $System_Drawing_Point
$comboBox1.TabIndex = 0
$comboBox1.add_SelectedIndexChanged($handler_comboBox1_SelectedIndexChanged)

$comboBox1.DropDownStyle =
    [System.Windows.Forms.ComboBoxStyle]::DropDown;
$comboBox1.Sorted = $true;

$comboBox1.AutoCompleteCustomSource.Add("System.Windows.Forms");
$comboBox1.AutoCompleteCustomSource.AddRange(("System.Data", "Microsoft"));
$comboBox1.AutoCompleteMode =
    [System.Windows.Forms.AutoCompleteMode]::SuggestAppend;
$comboBox1.AutoCompleteSource =
    [System.Windows.Forms.AutoCompleteSource]::CustomSource;

$form1.Controls.Add($comboBox1)
    #endregion $comboBox1
#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null

} #End Function

#Call the Function
GenerateForm
1
  • Firstly, for this to be close, you need to be using modal dialogs and other control efforts. Your form is still showing the min/max/exit for elements. You can't really stop this, this way; since, 'X', 'ALT+F4', or going into TaskMgr and exiting the process will also close this. Normal users may not do the previous, but again, is doable. What you could do is check for state and relaunch if it has no data, by checking on the form using a do loop or form close event, and or simply don't display close and or exit, if all fields are not complete.
    – postanote
    Commented Nov 12, 2022 at 6:52

1 Answer 1

1

Here is a q&d (quick and dirty) approach to what I am trying to recommend in my comment.

If you try and close the form via the WinForm 'X', the Windows 'Alt+F4', or even TaskMgrs' end task, it will warn you and simply restart for the user to try again.

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()


$form               = New-Object System.Windows.Forms.Form 
$form.Text          = 'New user GUI'
$form.Size          = New-Object System.Drawing.Size(300,200) 
$form.StartPosition = 'CenterScreen'

$OKButton              = New-Object System.Windows.Forms.Button
$OKButton.Location     = New-Object System.Drawing.Point(75,120)
$OKButton.Size         = New-Object System.Drawing.Size(75,23)
$OKButton.Text         = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$OKButton.Enabled      = $false

$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$label          = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20) 
$label.Size     = New-Object System.Drawing.Size(280,20) 
$label.Text     = 'Enter some stuff'

$form.Controls.Add($label) 

$textBox          = New-Object System.Windows.Forms.TextBox 
$textBox.Location = New-Object System.Drawing.Point(10,40) 
$textBox.Size     = New-Object System.Drawing.Size(260,20) 

$form.Controls.Add($textBox) 

$form.Topmost = $True
$form.Add_Shown({$textBox.Select()})

$textbox.Add_KeyDown(
    {$OKButton.Enabled = $True}
)

do
{
    $result = $form.ShowDialog()

    if ([string]::IsNullOrEmpty($textbox.text))
    {{[System.Windows.Forms.MessageBox]::Show('Please enter stuff, because I cannot proceed until you do.', 'Warning Message',1)}}
}
until(![string]::IsNullOrEmpty($textbox.text))

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{($UserInput = $textBox.Text)}

***Note:

Though I can see your goal here. This is not really a good user experience. Users should always be able to exit any app they are using to move on to other tasks.***

I can guarantee you, you are going to anger many with this use case.

2
  • :- What if i want to apply this with multiple textbox? what will be the code Commented Nov 12, 2022 at 16:16
  • Q&D, you can just repeat the code block I show for each textbox, or create a collection that you iterate through for such checks. Again, I can only want that this is not a good GUI methodology. Users must be able to get out of anything they are doing, whenever they choose/need to.
    – postanote
    Commented Nov 12, 2022 at 20:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .