locked
Form with Input Window, where a mechanism shall check if data has been entered without closing the main window RRS feed

  • Question

  • Dear Professionals,

    I was so far lucky, to cheat and copy myself through simple Powershell scripts and now came to a point, where without understanding, I assume, I won't get any further.

    I started to read and play around with Forms in Powershell and built so far the following short script.

    (Please forgive me, if the code looks like spaghetti code, I just do not know, how to code correctly.)

    What I would like to achieve, to check, as soon as the "OK" button is pressed, to have a check, if the Input field contains any symbol and if not, a new window shall appear with a message, that there is input missing.

    I am fighting with two things. First of all, as soon as a new Window/Message opens, the "Main" Window disappears. Goal should of course be, that the window maintains and data can be entered. I assume the problem is, that a new window opens and gets active and the other one is being closed, so how can I prevent that?

    At the end of the script, there is a commented part, I copy pasted over the Write-Host "There is input missing", where I hoped, that on such way, I can call a new window, if no data has been entered into the input field. Unfortunately, the Window also appears, when I have entered data. So I am doing something terribly wrong.

    There is surely a way to achieve this kind of request, but how to do it? Hopefully it is not rocket science?

    thank you very much for any help,

    Mike

    CLS
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing

                ########################
                #Window & Windows Title#
                ########################

    $mainform = New-Object System.Windows.Forms.Form
    $mainform.Text = "Test with Forms'"
    $mainform.AutoScroll = $True
    $mainform.AutoSize = $False
    $mainform.AutoSizeMode = "GrowAndShrink"
    $mainform.MinimizeBox = $True
    $mainform.MaximizeBox = $True
    $mainform.WindowState = "Normal"
    $mainform.Size = New-Object System.Drawing.Size(1000,1000)
    $mainform.StartPosition = 'CenterScreen'

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point(40,800)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = 'OK'
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $mainform.AcceptButton = $OKButton
    $mainform.Controls.Add($OKButton)

    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Point(150,800)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = 'Cancel'
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $mainform.CancelButton = $CancelButton
    $mainform.Controls.Add($CancelButton)

                ############
                #Form-Title#
                ############

    $labelTitle = New-Object System.Windows.Forms.Label
    $labelTitle.Location = New-Object System.Drawing.Point(10,20)
    $labelTitle.Size = New-Object System.Drawing.Size(450,50)
    $mainformTitle = New-Object System.Drawing.Font("Times New Roman",20,[System.Drawing.FontStyle]::Bold)
    $labelTitle.Font = $mainformTitle
    $labelTitle.Text = "Asking for input"
    $mainform.Controls.Add($labelTitle)

                #################
                #Label Firstname#
                #################

    $labelFirstname = New-Object System.Windows.Forms.Label
    $labelFirstname.Location = New-Object System.Drawing.Point(10,100)
    $labelFirstname.Size = New-Object System.Drawing.Size(860,40)
    $labelFirstnameFont = New-Object System.Drawing.Font("Arial",16,[System.Drawing.FontStyle]::Italic)
    $labelFirstname.ForeColor = "DarkBlue"
    $labelFirstname.Font = $labelFirstnameFont
    $labelFirstname.Text = "Please enter Firstname in the below box:"
    $mainform.Controls.Add($labelFirstname)

                ###################
                #Textbox Firstname#
                ###################

    $textBoxFirstname = New-Object System.Windows.Forms.TextBox
    $textBoxFirstname.Location = New-Object System.Drawing.Point(10,140)
    $textBoxFirstname.Size = New-Object System.Drawing.Size(860,100)
    $textBoxFirstnameFont = New-Object System.Drawing.Font("Arial",20,[System.Drawing.FontStyle]::Italic)
    $textBoxFirstname.Font = $textBoxFirstnameFont
    $mainform.Controls.Add($textBoxFirstname)


    $mainform.Topmost = $true

    $mainform.Add_Shown({$textBoxFirstname.Select()})

    $result = $mainform.ShowDialog()

    if ($result -eq [System.Windows.Forms.DialogResult]::OK)

    {if($textBoxFirstname.TextLength -eq 0)

       {Write-Host "There is Input missing"
    }
        else
            {Write-Host "There has been text entered"}
    }


    <#      $WarningForm = New-Object system.Windows.Forms.Form
            $WarningForm.Text = "Warning"
            $WarningForm.AutoScroll = $False
            $WarningForm.AutoSize = $False
            $WarningForm.ShowInTaskbar = $True
            $WarningForm.StartPosition = "CenterScreen"
            $WarningLabel = New-Object System.Windows.Forms.Label
            $WarningLabel.Text = "This form is very simple."
            $WarningLabel.AutoSize = $True
            $WarningForm.Controls.Add($WarningLabel)
            
            $OKWarningButton = New-Object System.Windows.Forms.Button
            $OKWarningButton.Location = New-Object System.Drawing.Point(50,50)
            $OKWarningButton.Size = New-Object System.Drawing.Size(75,23)
            $OKWarningButton.Text = 'OK'
            $OKWarningButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
            $Warningform.AcceptButton = $OKWarningButton
            $Warningform.Controls.Add($OKWarningButton)
            
            $Warningform.Topmost = $true
            $WarningForm.ShowDialog()}
            $Warningresult = $Warningform.ShowDialog()
            #>

    Friday, November 2, 2018 7:54 AM

All replies

  • First there are syntax errors in your code.

    Second it is not possible to understand what you are asking. 

    In PowerShell it is not possible to have two active modal dialogs at the same time.


    \_(ツ)_/

    Friday, November 2, 2018 8:02 AM
  • I think he just want a message popup...

    For those read about Wscript.Shell

    Also I recommend you to create a function for your condition and place it at top of your script, just call your function when button OK is clicked.

    Regards,

    Friday, November 2, 2018 8:37 AM
  • I think he just want a message popup...

    For those read about Wscript.Shell

    Also I recommend you to create a function for your condition and place it at top of your script, just call your function when button OK is clicked.

    Regards,

    Not the issue.  Even a messageBox mjst be executed modally withing the form and the executing form will freeze until the modal form is closed.

    We don't need VBScript to execute a message box in a form.

    [System.Windows.Forms.MessageBox]::Show('Your message')

    See documentation for all options.


    \_(ツ)_/

    Friday, November 2, 2018 9:07 AM
  • Dear Jebisata and jrv,

    Thank you for coming back to me and sorry if I haven't stated my problem clearly. Let me try to explain again.

    Yes, it is correct, I would like to have a popup message, as soon as there is no entry. The idea is, to have within the window several input textboxes, to fill in data and if something goes missing, the person filling out the form, shall get a message, that he/she forgotten something. What kind of message appears, doesn't matter, as long as the main window does not disappear and it is OK, that the main window will become freeze, which is actually even better.

    I will have a deeper look into [System.Windows.Forms.MessageBox]::Show('Your message'). Should you have any more ideas or suggestions, please let me know.

    Thank you very much,

    Mike

    Friday, November 2, 2018 2:05 PM
  • This would be done in your validation code.   When the validation fails use the "MessageBox function above to display the message.

    To learn how to use forms the following resources are excellent:

    https://info.sapien.com/index.php/guis/gui-scripting

    and

    https://info.sapien.com/index.php/guis/gui-advanced-tips/validating-the-form


    \_(ツ)_/

    Friday, November 2, 2018 2:16 PM
  • Thanx a lot for the help, much appreciated and I will stick to the links you have sent me.

    Hopefull, I will figure it out and get it to work.

    Thank you and have a great weekend,

    Mike

    Friday, November 2, 2018 3:13 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    Best Regards,

    Lee


    Just do it.

    Friday, November 9, 2018 7:17 AM