none
How to prevent the main form closing RRS feed

  • Question

  • Dear Friends,

    below is a small piece of code which is part of a big form , i am trying to create a search box to quickly search for a specific object , the below code fetch data from a csv file , and matches with my search creteria and gives out put , so it is working fine

    issue is : the main form is getting closed after i press the search button, how to prevent this ?  , i believe it is closing because of the OK button , but i am not able to resolve this issue

    screenshot added below

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point(610,80)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "Search"
    
    $OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $objform.AcceptButton = $OKButton
    $objform.Controls.Add($OKButton)
    
    $textBox = New-Object System.Windows.Forms.TextBox 
    $textBox.Location = New-Object System.Drawing.Point(350,82) 
    $textBox.Size = New-Object System.Drawing.Size(260,23) 
    $objform.Controls.Add($textBox) 
    
    $objform.Topmost = $True
    
    $objform.Add_Shown({$textBox.Select()})
    $result = $objform.ShowDialog()
    #$result | Out-File -FilePath "C:\Development_Zone\Staging\test.txt" -Append
    
    if ($result -eq [System.Windows.Forms.DialogResult]::OK)
    {
        $x = $textBox.Text
        $a = Import-Csv "$currentdir\collections.csv" | Where-Object {$_ -match $x}
        $output = $a.CollectionName
        $output | Out-File -FilePath "C:\Development_Zone\Staging\test.txt" -Append
    
    }


    OSLM ENGINEER - SCCM 2007 & 2012


    • Edited by Tanoj Mahishkar Monday, November 6, 2017 6:39 PM added screenshot
    • Moved by Bill_Stewart Tuesday, December 5, 2017 3:44 PM Help vampire
    Monday, November 6, 2017 6:37 PM

All replies

  • There is no way to answer you question without the main form code.

    Here is what you posted simplified so it uses forms more correctly for PowerShell.  As you can see it tells us nothing about the main form.

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Size = '75, 23'
    $OKButton.Location = '610,80'
    $OKButton.Text = 'Search'
    $OKButton.DialogResult = 'OK'
    $textBox = New-Object System.Windows.Forms.TextBox $textBox.Location = '350, 82' $textBox.Size = '260, 23' $searchDialog.Controls.Add($textBox) $searchDialog.AcceptButton = $OKButton $searchDialog.Controls.Add($OKButton) $searchDialog.Topmost = $True $searchDialog.Add_Shown({ $textBox.Select() })
    if('Ok' -eq $searchDialog.ShowDialog()){ Import-Csv "$currentdir\collections.csv" | Where-Object { $_ -match $textBox.Text } | Out-File C:\Development_Zone\Staging\test.txt -Append }


    \_(ツ)_/




    • Edited by jrv Monday, November 6, 2017 7:12 PM
    Monday, November 6, 2017 7:10 PM
  • The first thing I see is that you are adding your sub-dialogs to the main form controls collection.  You should not do this as all events will be consumed by the main form.

    There is no way for me to test this because there are too many pieces that exist only on your system

    I also notice that you are not making simple use of form events and seem to want to run long functions to make choices that a properly evented form would simplify.

    From the variable naming I suspect that you copied and pasted various bits from the web.  Most of the structures and approach show inclusion of many elements that come from coders unfamiliar with writing Windows code.  You whole project could be greatly simplified by using standard forms design methods.

    You should place all dialogs into their own functions.

    Here are some links that will help you understand how to approach complex projects. 

    https://info.sapien.com/index.php/guis


    \_(ツ)_/

    Tuesday, November 7, 2017 1:28 PM
  • Note also that you are using the same object for all forms "$objForm".   Each form must be either isolated in a function or use different variable names for all controls including the form.  That is why I changed the name of your original form.

    \_(ツ)_/

    Tuesday, November 7, 2017 1:32 PM
  • Dear Jrv,

    i managed to fix the script and successfully integrated a search function ,   

    i need help to include the "Enter key on textbox "  , currently i have to press the Search button , insted it should also work if i press enter key

    Tried 1 : 

    Set the Form's AcceptButton property to the Go button e.g.:

    $form1.AcceptButton = $goButton


    Tried 2  :

    $textbox.Add_KeyDown({
        if ($_.KeyCode -eq "Enter") {
            #logic
            $textbox.Text | Out-Host
        }
    })

    no luck ... may be i am not doing it right way

    Please find my piece of code attached 

    Thank you !!! 

    $handler_Find_Click= 
    {
        $output = $null
        $a = $null
        $x = $null
        $Collectioninfo = $null
        Remove-Item -Path "$currentdir\buffer.csv" -Force -ErrorAction SilentlyContinue;
        New-Item -Path "$currentdir\buffer.csv" -ItemType file -Force | out-null;
        add-content -path "$currentdir\buffer.csv" -Value "CollectionName" -Force;
        $x = $textBox.Text
        $a = Import-Csv "$currentdir\collections.csv" | Where-Object {$_ -match $x}
        $output = $a.CollectionName;
        $output | Out-File -FilePath "C:\Development_Zone\Staging\buffer.csv" -Encoding ascii -Append
        if($output)
        {
        $Collectioninfo = '1'
        Updatelistbox -Collectioninfo $Collectioninfo
        }
        else
        {
        $Collectioninfo = '2'
        Updatelistbox -Collectioninfo $Collectioninfo
        }
    
    }
    
    # Search Button
    
    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Point(695,80)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "Search"
    $OKButton.add_Click($handler_Find_Click)
    $objform.Controls.Add($OKButton)
    
    # Search text box
    
    $textBox = New-Object System.Windows.Forms.TextBox
    $textBox.Location = New-Object System.Drawing.Point(250,82) 
    $textBox.Size = New-Object System.Drawing.Size(445,23) 
    $objform.Controls.Add($textBox) 
    $objform.Add_Shown({$textBox.Select()})

    Tanoj


    OSLM ENGINEER - SCCM 2007 & 2012

    Thursday, November 9, 2017 5:58 PM
  • You have to enable "KeyPreview" on the control.

    \_(ツ)_/

    Thursday, November 9, 2017 6:53 PM