Function causes form to freeze and label not to update.

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
User avatar
Swatto86
Posts: 21
Joined: Mon May 18, 2015 7:21 am

Function causes form to freeze and label not to update.

Post by Swatto86 » Thu Jun 04, 2015 2:28 pm

Good Evening,

I have got an issue with a click event that runs a function - I would like the click event to update a label on my form before running the function as the function takes a while to run and causes my form to freeze. Is there a way I can force a form refresh before the function runs?:
PowerShell Code
Double-click the code block to select all.
$btnGetMailboxes_Click = {
	$btnGetMailboxes.Enabled = $false
	$labelInfo.ForeColor = 'Magenta'
	$labelInfo.Text = 'Retrieving mailboxes...please wait'
	$labelInfo.Visible = $true
	$cboSourceMailbox.Items.Clear()
	$cboUserMailbox.Items.Clear()
	$Mailboxes = GetMailboxList #this function causes form to freeze and label not to get updated with the above text until the function has finished.
	Load-ComboBox $cboSourceMailbox $Mailboxes.UserPrincipalName -Append
	Load-ComboBox $cboUserMailbox $Mailboxes.UserPrincipalName -Append
	$btnGetMailboxes.Enabled = $true
}

User avatar
SAPIEN Support Forums
Posts: 945
Joined: Wed Dec 03, 2014 2:26 pm

Force form repaint?

Post by SAPIEN Support Forums » Thu Jun 04, 2015 2:28 pm

This is an automated post. A real person will respond soon.

Thank you for posting, Swatto86.

Here are some hints to help you get an accurate and complete answer to your question.

Ask in the best forum: If you asked in the wrong forum, just copy your question to the right forum.

Anticipate follow-up questions!

Did you remember to include the following?
  • 1. Product, version and build
    2. 32 or 64 bit product
    3. Operating system, e.g. Windows 7 64 bit.
    4. Attach a screenshot, if applicable
    5. Attach logs, crash reports, etc., in a ZIP file
If not, please take a moment to edit your original post or reply to this one.

*** Make sure you do not post any licensing information ***

User avatar
jvierra
Posts: 13463
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Function causes form to freeze and label not to update.

Post by jvierra » Thu Jun 04, 2015 2:56 pm

Have you tried:
$label1.Refresh()

User avatar
Swatto86
Posts: 21
Joined: Mon May 18, 2015 7:21 am

Re: Function causes form to freeze and label not to update.

Post by Swatto86 » Thu Jun 04, 2015 3:38 pm

jvierra wrote:Have you tried:
$label1.Refresh()
Excellent, that worked. Thanks very much.

Do you know why sometimes I have to specify a method like this:

$label1.Refresh()

but other times I can just use

$cboSourceMailbox.Text.Length?

User avatar
jvierra
Posts: 13463
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Function causes form to freeze and label not to update.

Post by jvierra » Thu Jun 04, 2015 3:51 pm

"Length" is not a method. It is a property.

See: https://msdn.microsoft.com/en-us/librar ... .110).aspx

This lists all Properties, Methods and Events.

User avatar
Swatto86
Posts: 21
Joined: Mon May 18, 2015 7:21 am

Re: Function causes form to freeze and label not to update.

Post by Swatto86 » Thu Jun 04, 2015 4:14 pm

I thought .Text was the property though? Is it like a sub property

User avatar
Swatto86
Posts: 21
Joined: Mon May 18, 2015 7:21 am

Re: Function causes form to freeze and label not to update.

Post by Swatto86 » Thu Jun 04, 2015 4:39 pm

Swatto86 wrote:I thought .Text was the property though? Is it like a sub property
Ahh I think I understand, the length property is inherited from another control so it can be used in the combo box class.

User avatar
jvierra
Posts: 13463
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Function causes form to freeze and label not to update.

Post by jvierra » Thu Jun 04, 2015 4:51 pm

Swatto86 wrote:I thought .Text was the property though? Is it like a sub property
Propeties are "Types" and "Types" have properties.

Try this:
PowerShell Code
Double-click the code block to select all.
$combobox.Text.GetType()
$combobox.Text.Length.GetType()
$c=Get-Culture
$combobox.Text.length.ToByte($c)
$combobox.Text.length.ToByte($c).ToString().Length

User avatar
jvierra
Posts: 13463
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Function causes form to freeze and label not to update.

Post by jvierra » Thu Jun 04, 2015 4:56 pm

Swatto86 wrote:
Swatto86 wrote:I thought .Text was the property though? Is it like a sub property
Ahh I think I understand, the length property is inherited from another control so it can be used in the combo box class.
No. Everything that is a Property is a "Type" and types each have specific properties. It is not about inheritance.

User avatar
jvierra
Posts: 13463
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Function causes form to freeze and label not to update.

Post by jvierra » Thu Jun 04, 2015 5:04 pm

Swatto86 wrote:
Swatto86 wrote:I thought .Text was the property though? Is it like a sub property
Ahh I think I understand, the length property is inherited from another control so it can be used in the combo box class.
To be explicit. THe "Text" property is of "Type" "string" and string has a length property

'hello world'.Length

Locked