none
Form.ShowDialog() opens a second copy of the form when Submit button is clicked. RRS feed

  • Question

  • Let me start off by saying I am pretty new to PowerShell.

    I am righting a script which includes multiple forms and when I click submit I am getting a second copy of the form. I am using Form.ShowDialog() in two places because if I just put it at the end It does not recognize the radio buttin selected and if I put it just before the radiobutton.checked it does not continue on to the next form. Here is my code:

    $form = New-Object Windows.Forms.Form

    #Set the dialog title
    $form.text = "On Boarding and Off Boarding"
    $form.Width = 600
    $form.Height = 400
    $form.AutoSize = $true
    $Form.StartPosition = "CenterScreen"

    $radiobutton1 = New-Object System.Windows.Forms.RadioButton
    $radiobutton2 = New-Object System.Windows.Forms.RadioButton
    $radiobutton3 = New-Object System.Windows.Forms.RadioButton

    # Create the label control and set text, size and location
    $label = New-Object Windows.Forms.Label
    $label.Location = New-Object Drawing.Point 175,35
    $label.Size = New-Object Drawing.Point 300,30
    $Label.Font = "Arial Black MS,12"
    $label.text = "Select an Action to be Performed"

        # Create a group that will contain your radio buttons
      $MyGroupBox = New-Object System.Windows.Forms.GroupBox
        $MyGroupBox.Location = '100,90'
        $MyGroupBox.size = '400,150'

     # radiobutton1
     #
     $radiobutton1.Location = New-Object System.Drawing.Point(100,30)
     $radiobutton1.Name = "radiobutton1"
     $radiobutton1.Size = New-Object System.Drawing.Size(104,24)
     $radiobutton1.TabIndex = 1
     $radiobutton1.TabStop = $True
     $radiobutton1.Text = "On-Boarding"
     $radiobutton1.UseVisualStyleBackColor = $false
     #
     # radiobutton2
     #
     $radiobutton2.Location = New-Object System.Drawing.Point(100,70)
     $radiobutton2.Name = "radiobutton2"
     $radiobutton2.Size = New-Object System.Drawing.Size(104,24)
     $radiobutton2.TabIndex = 2
     $radiobutton2.TabStop = $True
     $radiobutton2.Text = "Off-Boarding"
     $radiobutton2.UseVisualStyleBackColor = $True
     $RadioButton2.Checked = $true
     #
     # radiobutton3
     #
     $radiobutton3.Location = New-Object System.Drawing.Point(100,110)
     $radiobutton3.Name = "radiobutton3"
     $radiobutton3.Size = New-Object System.Drawing.Size(150,24)
     $radiobutton3.TabIndex = 3
     $radiobutton3.TabStop = $True
     $radiobutton3.Text = "Change Experation Date"
     $radiobutton3.UseVisualStyleBackColor = $True
          
    # Add an OK button
        $Button = new-object System.Windows.Forms.Button
        $button.Location = '100,300'
        $Button.Size = '150,40' 
        $button.text = "Submit"
        $Button.DialogResult=[System.Windows.Forms.DialogResult]::OK
     
        #Add a cancel button
        $CancelButton = new-object System.Windows.Forms.Button
        $CancelButton.Location = '305,300'
        $CancelButton.Size = '150,40'
        $CancelButton.Text = "Cancel"
        $CancelButton.DialogResult=[System.Windows.Forms.DialogResult]::Cancel

    $form.controls.add($button)
    $form.controls.add($Cancelbutton)
    $form.controls.add($label)
    $form.controls.add($radiobutton1)
    $form.controls.add($radiobutton2)
    $form.controls.add($radiobutton3)

     $Mygroupbox.Controls.Add($radiobutton3)
     $Mygroupbox.Controls.Add($radiobutton2)
     $Mygroupbox.Controls.Add($radiobutton1)

        # Add all the Form controls on one line 
        $form.Controls.AddRange(@($MyGroupBox,$Button,$CancelButton))
     
        # Add all the GroupBox controls on one line
        $MyGroupBox.Controls.AddRange(@($Radiobutton1,$RadioButton2,$RadioButton3))
        
        # Assign the Accept and Cancel options in the form to the corresponding buttons
        $form.AcceptButton = $Button
        $form.CancelButton = $CancelButton
     
        # Activate the form
        $form.Add_Shown({$form.Activate()})    
        $form.ShowDialog()

    Write-Host $radiobutton2;

    if ($radiobutton1.Checked){
                  $button.add_click({UserName2})}
            elseif ($RadioButton2.Checked){
                  $button.add_click({UserName})}
            elseif ($RadioButton3.Checked){
                  $button.add_click({UserName4})}
            else {
            [System.Windows.Forms.MessageBox]::Show("No Selection was made Please Select a boarding Option","Warning",
            [System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Warning)
                }
    # Display the dialog
    $form.ShowDialog()

    • Moved by Bill_Stewart Friday, March 15, 2019 12:27 AM This is not "fix/debug/rewrite my script for me" forum
    Monday, November 26, 2018 8:27 PM

All replies