How do I create an input box with a cancel button that will end a loop in my script?

I have been working on a script for my client and I am having trouble getting it to work properly. I stole most of the code from the PowerShell Tip of the Week "Creating a Custom Input Box". I also took someone's code for the Show-MessageBox function.
I have slightly modified the original code with two parameters and an additional text box. The first field is for an e-mail address and the second is for an employee number.
I need to know how I can get a Do Until loop to recognize when the cancel button is pushed and break the loop and effectively end the script. The work happens at the end but perhaps I need something added/modified in the InputBox function.
I want the script to check to see if anything has been entered in the second text box. If empty it displays a message and calls the InputBox function again. Then if there is something I use elseif to check to see if it matches my RegEx (digits only). If
it doesn't match it will loop and call the InputBox function again.
This all works fine. The problem I am having is that I cannot cancel out of the form. I'd like the loop to continue until the second box matches my RegEx or Cancel is clicked. Clicking cancel doesn't break the loop. I need to know how I can stop the loop
when cancel is pressed. I've seen Stack "Overflow: PowerShell Cancel Button Stop Script" but I don't think this will work in a loop.
Any help would be awesome. As a note, I DO NOT want to use the VB Interaction stuff.
function InputBox {
param ($Name,$EN)
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Data Entry Form"
$objForm.Size = New-Object System.Drawing.Size(300,200)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Employee Email Address:"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,40)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
if ($Name) {
$objTextBox.Text = $Name
else {
$objTextBox.Text = "@domain.com"
$objLabel2 = New-Object System.Windows.Forms.Label
$objLabel2.Location = New-Object System.Drawing.Size(10,70)
$objLabel2.Size = New-Object System.Drawing.Size(280,20)
$objLabel2.Text = "Employee Number:"
$objForm.Controls.Add($objLabel2)
$objTextBox2 = New-Object System.Windows.Forms.TextBox
$objTextBox2.Location = New-Object System.Drawing.Size(10,90)
$objTextBox2.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)
$objForm.Controls.Add($objTextBox2)
$objForm.Topmost = $True
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$Script:ButtonName = $objTextBox.Text
$script:ButtonEN =$objTextBox2.Text
$ButtonName; $ButtonEN
Function Show-MessageBox{
Param(
[Parameter(Mandatory=$True)][Alias('M')][String]$Msg,
[Parameter(Mandatory=$False)][Alias('T')][String]$Title = "",
[Parameter(Mandatory=$False)][Alias('OC')][Switch]$OkCancel,
[Parameter(Mandatory=$False)][Alias('OCI')][Switch]$AbortRetryIgnore,
[Parameter(Mandatory=$False)][Alias('YNC')][Switch]$YesNoCancel,
[Parameter(Mandatory=$False)][Alias('YN')][Switch]$YesNo,
[Parameter(Mandatory=$False)][Alias('RC')][Switch]$RetryCancel,
[Parameter(Mandatory=$False)][Alias('C')][Switch]$Critical,
[Parameter(Mandatory=$False)][Alias('Q')][Switch]$Question,
[Parameter(Mandatory=$False)][Alias('W')][Switch]$Warning,
[Parameter(Mandatory=$False)][Alias('I')][Switch]$Informational)
#Set Message Box Style
IF($OkCancel){$Type = 1}
Elseif($AbortRetryIgnore){$Type = 2}
Elseif($YesNoCancel){$Type = 3}
Elseif($YesNo){$Type = 4}
Elseif($RetryCancel){$Type = 5}
Else{$Type = 0}
#Set Message box Icon
If($Critical){$Icon = 16}
ElseIf($Question){$Icon = 32}
Elseif($Warning){$Icon = 48}
Elseif($Informational){$Icon = 64}
Else{$Icon = 0}
#Loads the WinForm Assembly, Out-Null hides the message while loading.
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
#Display the message with input
$Answer = [System.Windows.Forms.MessageBox]::Show($MSG , $TITLE, $Type, $Icon)
#Return Answer
Return $Answer
$num = "^\d+$"
do {
if (!($ButtonEN)) {
Show-MessageBox -Msg "You must enter a numeric value for the employee number." -Title "Employee Number Missing" -Critical
InputBox -Name $ButtonName
elseif ($ButtonEN -notmatch $num) {
Show-MessageBox -Msg "The employee number must contain numbers only!" -Title "Non-numerical characters found" -Critical
InputBox -Name $ButtonName
until ( ($ButtonEN -match $num) -or (<this is where I want to be able to use the cancel button>)

Here is a simple validation method.
function New-InputBox{
param(
$EmailAddress='',
$EmployeeNumber=''
Add-Type -AssemblyName System.Windows.Forms
$Form=New-Object System.Windows.Forms.Form
$Form.Text='Data Entry Form'
$Form.Size='300,200'
$Form.StartPosition='CenterScreen'
$OKButton=New-Object System.Windows.Forms.Button
$OKButton.Location='75,120'
$OKButton.Size='75,23'
$OKButton.Text='OK'
$OKButton.DialogResult='Ok'
$OKButton.CausesValidation=$true
$Form.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text ='Cancel'
$CancelButton.DialogResult='Cancel'
$CancelButton.CausesValidation=$false
$Form.Controls.Add($CancelButton)
$Label1=New-Object System.Windows.Forms.Label
$Label1.Location='10,20'
$Label1.Size='280,20'
$Label1.Text='Employee Email Address:'
$Form.Controls.Add($Label1)
$TextBox1=New-Object System.Windows.Forms.TextBox
$TextBox1.Location='10,40'
$TextBox1.Size='260,20'
$textbox1.Name='EmailAddress'
$textbox1.Text=$EmailAddress
$Form.Controls.Add($textbox1)
$Label2=New-Object System.Windows.Forms.Label
$Label2.Location='10,70'
$Label2.Size='280,20'
$Label2.Text='Employee Number:'
$Form.Controls.Add($Label2)
$TextBox2=New-Object System.Windows.Forms.TextBox
$TextBox2.Location='10,90'
$TextBox2.Size='260,20'
$TextBox2.Name='EmployeeNumber'
$TextBox2.Text=$EmployeeNumber
$Form.Controls.Add($TextBox2)
$Form.AcceptButton=$OKButton
$Form.CancelButton=$CancelButton
$Form.Topmost = $True
$form1_FormClosing=[System.Windows.Forms.FormClosingEventHandler]{
if($Form.DialogResult -eq 'OK'){
if($textbox1.Text -eq ''){
[void][System.Windows.Forms.MessageBox]::Show('please enter an email address','Validation Error')
$_.Cancel=$true
}else{
# Check empno is all digits
if("$($TextBox2.Text)" -notmatch '^\d+$'){
[void][System.Windows.Forms.MessageBox]::Show('please enter a number "999999"','Validation Error')
$_.Cancel=$true
$form.add_FormClosing($form1_FormClosing)
$Form.Add_Shown({$Form.Activate()})
if($Form.ShowDialog() -eq 'Ok'){
# return the form contents
$Form
if($f=New-InputBox -EmailAddress [email protected]){
'Email is:{0} for Employee:{1}' -f $f.Controls['EmailAddress'].Text,$f.Controls['EmployeeNumber'].Text
}else{
Write-Host 'From cancelled!' -ForegroundColor red
¯\_(ツ)_/¯

Similar Messages

  • Does anyone know how I can create a text field in a pdf document that will multiply a total in another box?

    does anyone know how I can create a text field in a pdf document that will multiply a total in another box? I’m making an interactive pdf for an order form (attached), and I need to find a way for the “total quantity” number to multiply by 9 and total in the “Amount Due” box.

    Hey Gary,
    Have a look at this post: Re: horizontal scrolling similar to excel
    Andy's reply will show you how to make a table scroll horizontally, but it will be tough to accomplish it in some sort of easily replicable way. I am working on a JQuery extension that will help accomplish this, but I have had my time invested in another project at the moment.
    Good Luck,
    Tyson

  • How can I create a horizontal scrollbar with a centered thumb that scrolls content from both sides?

    I am having trouble figuring out how to create a horizontal scrollbar component either by itself or nested inside a data list component that will have it's thumb centered in the track when running and reveal content from either the right or left side when the thumb is moved. The furthest I have managed to get is to create a data list component with a scrollbar component inside that has a centered thumb that reveals content from the right side of the list (0 through 10 of the items) but only reveals blank area when tracking the other way. Is there a way to create say… negative items in the data list… 0 through -10? Or am I approaching this the wrong way. Please help. Thanks.

    Mykola,
    Thanks. I guess I was hoping for an answer that addressed my question from a designer's point of view rather than a developers'. As a designer with over ten years experience using Adobe products for print work it is difficult to understand why "centered" is such a complicated concept when designing art for use on the web. It is so frustrating to realize that most containers for images and text can only be resized by pushing and pulling handles located on the right and bottom; Catalyst seems like a great start but if Adobe really wanted to impress the "design" community it might consider putting all that supercharged code underneath the hood of some more familiar "design" tools. Maybe a "catalytic converter" would allow the introduction of a "centered" tool for every element.
    After all, why is it literally twenty-five times more difficult, requiring the use of three additional programs to create a component that functions exactly like a typical data list and scroll bar with the exception that the thumb is "centered" on the track and reveals images from both the right and left. For that matter why not also have as an option a thumb that snaps to the bottom of the track and reveals images from the top… and one that snaps to the right and reveals images from the left when you run the project? It just seems so logical to expand the scroll bar component to include these options.
    I am very glad to have Catalyst and will redesign my project to fit within the constraints of the tools available in the program but it seems that if Adobe is really serious about Catalyst being a window into the world of web design for designers/AD's that perhaps it might benefit by focusing on what might improve the program from the designer's point of view. I hate to say it but the Catalyst forum is already rife with answers to questions that are riddled with code… Literally. And to be honest most designers don't have the time to decipher that code. As a designer I work regularly and have a deep understanding of Photoshop, InDesign, Illustrator, Acrobat Pro, Final Cut Pro, Final Draft and modo. I do hi-res assembly, retouching, design and layout, identity, production, 3D modeling and rendering, video editing… and before I switch to using Catalyst for web mock-ups I am going to need a more "designer" friendly set of tools and definitely a "centered" control.
    I really think Adobe is fantastic. But I also think it could take a lesson from a great little company called Luxology. I tried learning 3D modeling and rendering for years with programs like Lightwave, Maya and others, always with mixed results. Then Luxology came along and actually delivered on their promise to create a 3D program for artists. What was the factor that made all the difference? Well, besides the Apple award winning interface and sets of tools it was the training available on their sight. The program itself ships with thirty-six hours of quicktime movies. And hundreds more hours available for download. I have never yet not been able to quickly and easily find an answer to a question I had about how to accomplish something in modo. You know how long I have already spent on Adobe TV searching through videos and on the Catalyst forum searching through topics trying to get an answer to what I thought was a very simple question? Way too many. If I have a question about a Luxology product that I can't find the answer to do you know what I do? I call Brad Peebler, the President of Luxology. I'm not special nor do I work for some special development house with special privileges that is simply their policy. And that policy has paid big dividends. Both ILM and Pixar has licensed their technology.
    Well… I apologize for this long response but I really think that if Catalyst is going to attract the market it wants that it will have to consider putting some designers on the development team. After all… Isn't that what the promo videos tout… Finally a web design program for designers. Well, I guess we'll see.
    Karl

  • How do I make a text box in a Master Slide that will have a Bold headline and Medium bullets?

    I wan't one Master box that will accomodate the above. Start bold, hit return for bullets and the font will be medium/light.
    Please/Thanks-
    Rich

    But doing it your way above, I can't edit the text in a slide. When I double clicke I get the You Have To Edit The Master message. When I check the box...all is lost...see the above. I started with a whole new presentation and went to the blank slide and copied/renamed it...Thanks for your help so far Gary...I've got Keynote '09, version 5.1.1 (1034) if that makes the difference...

  • How do I create a transparent box with only the colored border visible

    I'm trying to highlight something on a data sheet, and just need to make a simple box that outlines the content area I'm highlighting, but obviously the viewer needs to see through the box to the content.
    Can't find anything on Elements for how this is done.
    thanks
    doug

    If you want more control you could use the Rectangle Tool and either add a stroke with layer styles or turn down the opacity
    and then you can move, resize or change the color at any time.
    Highlight:
    1. Draw the retangle, double click on the shape layer in the layers panel to choose the color and then lower the layer opacity to taste.
        You can use the move tool to reposition the box or resize by clicking on one of the corner handles.
    Stroke:
    1. Draw out the rectangle.
    2. In the effects panel under Layer Styles choose Visibility and Hide
        (the rectangle color will diappear)
    3. Go to Layer>Layer Style>Style Settings and choose Stroke in the dialog.
       (you can choose the color and size of the stroke in the dialog)
    MTSTUNER

  • How do I create a PDF document with a URL so that I can link it to my webpage?

    I am in the middle of creating a business website. Some of the words on a page I've highligted and set them as a link to other information. I've typed up a Word doc and saved it as a PDF and on my webpage I have: Click here to see a sample report. I want to create a link so that when someone clicks on it the PDF document will open up. How do I do that?

    Hi,
    This is not the right forum for this, please ask it in Acrobat forum.
    Regards,
    Perry

  • How to create a check box with an X

    Hi,
    How do you create a check box with an X as apposed to a tick?
    Thanks

    Go to the check box's properties and in the Options tab choose "Cross" as the check box style.

  • Creating a dialog box with OK and Cancel buttons without OADialogPage

    Hi Experts,
    i want to create a dialog box for Delete Confirmation means after clicking on delete it should pop up a small dialog box with OK/Cancel buttons
    and need code for the same to handle the buttons.
    i have created with OADialogPage but its showing in a complete page but i want to show those buttons in a small box.
    Plz help.
    THANKS IN ADVANCE.
    Thanks
    Raja.

    Hi,
    I have not tried using javascript in destination URI as suggested by Mukul.
    I have tried the below code for opening a page as dialog box.
    You can try it for your requirement by creating a dialog page of your own, say XXDialogPage (with OK and Cancel button)
    StringBuffer l_buffer = new StringBuffer();
    l_buffer.append("javascript:mywin = openWindow(top, '");
    String url = "/OA_HTML/OA.jsp?page=/xx/oracle/apps/fnd/dialog/webui/OADialogPG";
    OAUrl popupUrl = new OAUrl(url, OAWebBeanConstants.ADD_BREAD_CRUMB_SAVE );
    String strUrl = popupUrl.createURL(pageContext);
    l_buffer.append(strUrl.toString());
    l_buffer.append("', 'lovWindow', {width:500, height:500},false,'dialog',null);");
    pageContext.putJavaScriptFunction("SomeName",l_buffer.toString());
    In dialog page's controller, you can forward it to the page onc ethe user selects OK or Cancel.
    Pass the selected value back to the main page and further you can use it for deleting.
    Regards.

  • Clear an input box with failed validation status

    My jdev version is 11.1.1.5.0
    I have an input box with certain validation.If validation fails it doesnt clear its contents on button click.
    My button click code is :
    propButton.setValue(null);
    where propButton is binding name to button.

    Hi Lovin,
    How are you actually validating? Is it by <f:Validator> Tag, or Validation in the Button click code?
    If its in button click code: Refreshing programmatically in the button code, will ensure that the value is cleared in the inputTextBox and then refreshed.
    Refreshing programmatically:
    First, create a binding to the InputText Box in a managed bean, using the Binding attribute.
    binding="#{managedBeanName.inputTextBox}"
    public void clickButton(ActionEvent ae){
    this.getInputTextBox().setValue(null);
    AdfFacesContext.getCurrentInstance().addPartialTarget(this.getInputTextBox());
    Thanks,
    Pandu

  • How can I create a sales order with reference to a purchase order?

    How can I create a sales order with reference to a purchase order?
    Thanks in advance...

    Hello,
    you can't create a sales order with reference to a purchase order. You can input customer PO nuber in the sales order Purchase Order number filed.
    Prase.

  • How does one create a "check box" in a cell?

    How does one create a "check box" in a cell?

    select the cell then open the cell formatter:
    process should be similar in the iOS version.  I do not have any iOS devices with Numbers so I cannot check.  I did find this link that may help:
    http://support.apple.com/kb/PH3374?viewlocale=en_US

  • How does the search functon/input box work?

    How does the search functon/input box work?
    Hi
    my level is beginner
    there is a search box on the upper right hand on this page
    frames in gey ?
    how does this work.
    does this search in the database?
    if i would want to create this in an application
    what can i do ?
    are there CF examples??

    Thanks
    search against a database : Are there a function of Cf ta
    tags/code to do this? is this CFquery / and Cfoutput?
    or
    do a Verity search of a collection. How is this dont by? CF
    submit a search to an external search engine like Google.
    oh, is this when a page has a search and when the user types in
    there text in the input box it automatically goes to the google
    search. any articles on how to do this in CF
    Thanks

  • How do you create a seach box in MUSE?

    how do you create a seach box in MUSE? - viewer enters a keyword and will be lead to correct product

    Hi there - For questions about individual programs, you're better off posting in their specific forums. Here's the Muse forum: http://forums.adobe.com/community/muse/general_questions_about_adobe_muse
    That being said, there is currently not a search bar widget that can be added to a site directly from within Muse. However, you can add custom HTML code to your page, with a method such as this: http://www.ehow.com/how_6772398_embed-google-search-bar.html. Just add the custom HTML to your Muse page by going to Object > Insert HTML.
    This page might also help you out: http://www.adobe.com/products/muse/embedded-html.edu.html
    Good luck!

  • How do i create a little network with my i-mac and macbook

    how do i create a little network with my i-mac and macbook

    Hello:
    To give a sensible answer, a little more information is needed.
    I am guessing that you want to set up a wireless network as you have both a desktop and laptop.
    There are some pretty good tutorials/articles in the knowledge base articles.
    Barry

  • How I can stop the combo box with list of values from fireing validations

    Hi I'm using Jdeveloper 11.1.2.3.0
    Using Hr Schema employees table
    I Display employees data in af:table
    and I make List Of values on Department_id filed to easy change the employee department
    and another one on Job_id filed
    and Imake them UI Hints as ( combo box with list of values ) in the employeesVO
    the problem is when I Select a value from department or jobs ( combo box with list of values )
    fires the entire filed validations for mandatory atributes
    Note : the af:table Property ( contedelivery) is set to (immediate )
    How I can stop the combo box with list of values from fireing validations

    check it out.,
    http://andrejusb.blogspot.in/2012/09/what-to-do-when-adf-editable-table.html

Maybe you are looking for