Hi,
I've created a phonebook application with PowerShell Studio 2016 (single form with a datagridview, labels, text boxes & buttons).
The form behaves as expected until it has been maximised, then restored.
After a restore down, clicking on the title bar and trying to drag the window (even a tiny amount) causes it to maximise.
Then it can be moved around, but it won't restore to normal size.
Any suggestions?
Badly behaving window
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Re: Badly behaving window
And this is after you have changed the fonts to use pixels instead of points?
This is not behavior caused by Windows Forms but is due to a coding error or issues with your graphics hardware. We cannot guess at what is causing this.
Start by creating a simple version of your form with no code them add your code a logical step at a time to try and determine which code may be responsible.
Is this while trying to run it from PowerShell Studio? Do you have the latest version of PSS installed?
This is not behavior caused by Windows Forms but is due to a coding error or issues with your graphics hardware. We cannot guess at what is causing this.
Start by creating a simple version of your form with no code them add your code a logical step at a time to try and determine which code may be responsible.
Is this while trying to run it from PowerShell Studio? Do you have the latest version of PSS installed?
Re: Badly behaving window
This is while running from PowerShell Studio and when users run the .exe on their PCs (various makes & models) or on a Server 2008 R2 terminal server.
It's going to be tricky to figure out at what point this problem was introduced:
1. It's kind of grown organically as I worked out ways to add new features.
2. Adding the ability to maximize the windows was a recent addition.
3. Even after making the window maximizable I wasn't checking for this problem.
Until I get the time to rewrite from scratch (which is probably needed) I guess I'll just make the form fixed size.
Anyway - thanks for your advice, whatever the outcome it's been fun learning how to put my scripts into GUIs.
It's going to be tricky to figure out at what point this problem was introduced:
1. It's kind of grown organically as I worked out ways to add new features.
2. Adding the ability to maximize the windows was a recent addition.
3. Even after making the window maximizable I wasn't checking for this problem.
Until I get the time to rewrite from scratch (which is probably needed) I guess I'll just make the form fixed size.
Anyway - thanks for your advice, whatever the outcome it's been fun learning how to put my scripts into GUIs.
Re: Badly behaving window
I really wouldn't know how to.
I just set the form's MaximizeBox property to True and corrected the anchor points for the datagridview etc. (all within the form designer window).
I just set the form's MaximizeBox property to True and corrected the anchor points for the datagridview etc. (all within the form designer window).
Re: Badly behaving window
Then what is the issue? Forms don't change by themselves. Something you are doing is causing an issue or your graphics subsystem has issues. This can happen with hi-res adapters in gaming mode when the driver is not compatible with the OS.
Re: Badly behaving window
The I.T. dept doesn't qualify for expensive graphics cards!
I'd paste the script here - but I haven't figured out how to properly do that yet
...OK try this:
I'd paste the script here - but I haven't figured out how to properly do that yet
...OK try this:
- $formPhoneBookSearchDial_Load={
- }
- #region Control Helper Functions
- function Load-DataGridView
- {
- <#
- .SYNOPSIS
- This functions helps you load items into a DataGridView.
- .DESCRIPTION
- Use this function to dynamically load items into the DataGridView control.
- .PARAMETER DataGridView
- The DataGridView control you want to add items to.
- .PARAMETER Item
- The object or objects you wish to load into the DataGridView's items collection.
- .PARAMETER DataMember
- Sets the name of the list or table in the data source for which the DataGridView is displaying data.
- .PARAMETER AutoSizeColumns
- Resizes DataGridView control's columns after loading the items.
- #>
- Param (
- [ValidateNotNull()]
- [Parameter(Mandatory = $true)]
- [System.Windows.Forms.DataGridView]$DataGridView,
- [ValidateNotNull()]
- [Parameter(Mandatory = $true)]
- $Item,
- [Parameter(Mandatory = $false)]
- [string]$DataMember,
- [System.Windows.Forms.DataGridViewAutoSizeColumnMode]$AutoSizeColumns = 'None'
- )
- $DataGridView.SuspendLayout()
- $DataGridView.DataMember = $DataMember
- if ($Item -is [System.Data.DataSet] -and $Item.Tables.Count -gt 0)
- {
- $DataGridView.DataSource = $Item.Tables[0]
- }
- elseif ($Item -is [System.ComponentModel.IListSource]`
- -or $Item -is [System.ComponentModel.IBindingList] -or $Item -is [System.ComponentModel.IBindingListView])
- {
- $DataGridView.DataSource = $Item
- }
- else
- {
- $array = New-Object System.Collections.ArrayList
- if ($Item -is [System.Collections.IList])
- {
- $array.AddRange($Item)
- }
- else
- {
- $array.Add($Item)
- }
- $DataGridView.DataSource = $array
- }
- if ($AutoSizeColumns -ne 'None')
- {
- $DataGridView.AutoResizeColumns($AutoSizeColumns)
- }
- $DataGridView.ResumeLayout()
- }
- function ConvertTo-DataTable
- {
- <#
- .SYNOPSIS
- Converts objects into a DataTable.
- .DESCRIPTION
- Converts objects into a DataTable, which are used for DataBinding.
- .PARAMETER InputObject
- The input to convert into a DataTable.
- .PARAMETER Table
- The DataTable you wish to load the input into.
- .PARAMETER RetainColumns
- This switch tells the function to keep the DataTable's existing columns.
- .PARAMETER FilterWMIProperties
- This switch removes WMI properties that start with an underline.
- .EXAMPLE
- $DataTable = ConvertTo-DataTable -InputObject (Get-Process)
- #>
- [OutputType([System.Data.DataTable])]
- param (
- [ValidateNotNull()]
- $InputObject,
- [ValidateNotNull()]
- [System.Data.DataTable]$Table,
- [switch]$RetainColumns,
- [switch]$FilterWMIProperties)
- if ($null -eq $Table)
- {
- $Table = New-Object System.Data.DataTable
- }
- if ($InputObject -is [System.Data.DataTable])
- {
- $Table = $InputObject
- }
- elseif ($InputObject -is [System.Data.DataSet] -and $InputObject.Tables.Count -gt 0)
- {
- $Table = $InputObject.Tables[0]
- }
- else
- {
- if (-not $RetainColumns -or $Table.Columns.Count -eq 0)
- {
- #Clear out the Table Contents
- $Table.Clear()
- if ($null -eq $InputObject) { return } #Empty Data
- $object = $null
- #find the first non null value
- foreach ($item in $InputObject)
- {
- if ($null -ne $item)
- {
- $object = $item
- break
- }
- }
- if ($null -eq $object) { return } #All null then empty
- #Get all the properties in order to create the columns
- foreach ($prop in $object.PSObject.Get_Properties())
- {
- if (-not $FilterWMIProperties -or -not $prop.Name.StartsWith('__')) #filter out WMI properties
- {
- #Get the type from the Definition string
- $type = $null
- if ($null -ne $prop.Value)
- {
- try { $type = $prop.Value.GetType() }
- catch { }
- }
- if ($null -ne $type) # -and [System.Type]::GetTypeCode($type) -ne 'Object')
- {
- [void]$table.Columns.Add($prop.Name, $type)
- }
- else #Type info not found
- {
- [void]$table.Columns.Add($prop.Name)
- }
- }
- }
- if ($object -is [System.Data.DataRow])
- {
- foreach ($item in $InputObject)
- {
- $Table.Rows.Add($item)
- }
- return @( ,$Table)
- }
- }
- else
- {
- $Table.Rows.Clear()
- }
- foreach ($item in $InputObject)
- {
- $row = $table.NewRow()
- if ($item)
- {
- foreach ($prop in $item.PSObject.Get_Properties())
- {
- if ($table.Columns.Contains($prop.Name))
- {
- $row.Item($prop.Name) = $prop.Value
- }
- }
- }
- [void]$table.Rows.Add($row)
- }
- }
- return @( ,$Table)
- }
- #endregion
- $crlf = [char]13 + [char]10
- try
- { # Download phonebook from Asterisk PBX server
- $pb = [xml](New-Object System.Net.Webclient).Downloadstring("http://10.0.3.254:83/phonebook.xml")
- }
- catch
- {
- $textboxError.Text = "Cannot download the phone book. Please check your proxy settings."
- }
- $grps = $pb.AddressBook.pbgroup
- $contacts = $pb.AddressBook.Contact
- $script:numbers = $dNumbers = @($contacts |
- Sort-Object -Property Group, LastName, FirstName |
- Select-Object @{
- Label = "Group"; Expression = { $grps.name[$_.Group] }
- }, FirstName, LastName, Department, @{
- Label = "Number"; Expression = { if ($null -eq $_.Phone[0].phonenumber) { $_.Phone.phonenumber }
- else { $_.Phone[0].phonenumber } }
- }
- )
- Load-DataGridView -DataGridView $datagridview1 -Item $script:numbers
- $dialCapable = $false
- # For terminal server users, the file with the IP address of their phone is stored in LocalAppData
- # PC users get this file from ProgData
- $ipFiles = @(($env:LocalAppData + '\Norbar\IP.txt'), ($env:ProgramData + '\Norbar\IP.txt'))
- foreach ($ipFile in $ipFiles)
- {
- if ((Test-Path -Path $ipFile) -eq $true)
- {
- $ipAddr = @(Get-Content -Path $ipFile)[0]
- $textboxDebugMsg.Text = "IP address: $ipAddr" + $crlf
- if ([ipaddress]::TryParse($ipAddr, [ref]0) -eq $true)
- {
- $dialCapable = $true
- break
- }
- }
- }
- $password = "secret"
- $fLineStatus = "http://" + $ipAddr + "/cgi-bin/api-get_line_status?passcode=" + $password
- $fSendKeys = "http://" + $ipAddr + "/cgi-bin/api-send_key?passcode=" + $password
- $buttonClose_Click={
- }
- $buttonReset_Click = {
- $textbox2.Visible = $false
- $labelNumber.Visible = $false
- $buttonDial.Visible = $false
- $script:numbers = $dNumbers
- $textboxDebugMsg.Text = "$($script:numbers.Count) contacts found." + $crlf
- $textbox1.Clear()
- Load-DataGridView -DataGridView $datagridview1 -Item $script:numbers
- $textbox1.Focus()
- }
- $buttonSearch_Click={
- $textbox2.Visible = $false
- $labelNumber.Visible = $false
- $buttonDial.Visible = $false
- if ($null -ne $textbox1.Text)
- {
- $search = "*" + $textbox1.Text + "*"
- $script:numbers = $script:numbers | Where-Object {
- $_.Group -like $search -or
- $_.FirstName -like $search -or
- $_.LastName -like $search -or
- $_.Department -like $search -or
- $_.Number -like $search
- }
- $textboxDebugMsg.Text = "Search clicked. $($script:numbers.Count) contacts found" + $crlf
- if ($script:numbers.Count -eq 0)
- {
- $script:numbers = New-Object PSobject
- $script:numbers | Add-Member Group "Search returns no matches. Please reset"
- }
- Load-DataGridView -DataGridView $datagridview1 -Item $script:numbers
- }
- $textbox1.Focus()
- $textbox1.SelectAll()
- }
- $datagridview1_CellClick=[System.Windows.Forms.DataGridViewCellEventHandler]{
- #Event Argument: $_ = [System.Windows.Forms.DataGridViewCellEventArgs]
- $name = $script:numbers[$_.RowIndex].FirstName + " " + $script:numbers[$_.RowIndex].LastName
- $textboxDebugMsg.AppendText("$name selected." + $crlf)
- $script:dialNumber = $script:numbers[$_.RowIndex].Number
- if ($dialCapable -and $null -ne $script:dialNumber)
- {
- $textbox2.Text = $script:dialNumber
- $labelNumber.Visible = $true
- $textbox2.Visible = $true
- $buttonDial.Visible = $true
- }
- $textbox1.Focus()
- }
- $datagridview1_KeyDown = [System.Windows.Forms.KeyEventHandler]{
- #Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
- if ($_.KeyCode -eq 'Escape')
- {
- $buttonReset_Click.Invoke()
- }
- }
- $buttonDial_Click = {
- $textboxDebugMsg.AppendText("Dial clicked." + $crlf)
- $resultLine = Invoke-WebRequest -Uri $fLineStatus
- if ($resultLine.Content.Length -eq 0)
- {
- $textboxDebugMsg.AppendText("Line status not returned: Your phone needs a firmware update." + $crlf)
- }
- $textboxDebugMsg.AppendText($resultLine.Content.Replace("{", $crlf) + $crlf)
- # This condition abandoned for the moment. A bug in some Grandstream firmware
- # versions prevents the phone from returning a line check
- # if ($resultLine.Content -match '"line": 1, "state": "idle"')
- # {
- # $textboxDebugMsg.AppendText("Line OK." + $crlf)
- $dialString = $fSendKeys + "&keys=" + ($script:dialNumber.ToCharArray() -Join (":")) + ":SEND"
- $textboxDebugMsg.AppendText($dialString.Replace($password,"******") + $crlf)
- $result = Invoke-WebRequest -Uri $dialString
- $textboxDebugMsg.AppendText($result.Content)
- # }
- }
- $logo_DoubleClick = {
- if ($textboxDebugMsg.Visible -eq $true)
- {
- $textboxDebugMsg.Visible = $false
- }
- else
- {
- $textboxDebugMsg.Visible = $true
- }
- }
- $textbox1_KeyDown=[System.Windows.Forms.KeyEventHandler]{
- #Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
- if ($_.KeyCode -eq 'Enter')
- {
- $buttonSearch_Click.Invoke()
- }
- if ($_.KeyCode -eq 'Escape')
- {
- $buttonReset_Click.Invoke()
- }
- }
- $textboxDebugMsg_KeyDown=[System.Windows.Forms.KeyEventHandler]{
- #Event Argument: $_ = [System.Windows.Forms.KeyEventArgs]
- if ($_.KeyCode -eq 'Enter')
- {
- $buttonSearch_Click.Invoke()
- }
- if ($_.KeyCode -eq 'Escape')
- {
- $buttonReset_Click.Invoke()
- }
- }
Re: Badly behaving window
you have a great amount of code that only exists outside of any event. This will likely cause very unpredictable behavior. All code in a form should be in an event.
If you are going to post code it should be attached as a PSF as there is no way to know how your form is set up with just the embedded custom script.
If you are going to post code it should be attached as a PSF as there is no way to know how your form is set up with just the embedded custom script.
Re: Badly behaving window
Oops!
Am I right to guess that the non-event stuff goes into the form_Load section?
I had wondered what belongs in there.
Anyway - I've done that (attached), but it still doesn't like maximize, restore, followed by drag window.
I'm sure it must be something I've done - just can't see what.
Many thanks.
Am I right to guess that the non-event stuff goes into the form_Load section?
I had wondered what belongs in there.
Anyway - I've done that (attached), but it still doesn't like maximize, restore, followed by drag window.
I'm sure it must be something I've done - just can't see what.
Many thanks.
- Attachments
-
- Search-PhoneBook6.psf
- Search the company phone book, pick a number & dial it
- (137.46 KiB) Downloaded 126 times
Re: Badly behaving window
Your form has some fundamental mistakes or misunderstandings. As long as code is executing in an event the form will be frozen. To get a form to display we have the "load" vent which is mostly used to initialize values or do simple assignments to control. The "Activated" event fires after the form is displayed and allows a further opportunity to initialize the form. Any long running code will freeze the form until the event exits.
To understand this and learn how to design a form that is responsive read the following blog posts.
https://www.sapien.com/blog/?s=responsive+forms
https://www.sapien.com/blog/topics/user ... istrators/
To understand this and learn how to design a form that is responsive read the following blog posts.
https://www.sapien.com/blog/?s=responsive+forms
https://www.sapien.com/blog/topics/user ... istrators/