Button retains Visible and Enabled properties.

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.
This topic is 2 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
davsanko
Posts: 2
Last visit: Sun Jun 26, 2022 10:00 am

Button retains Visible and Enabled properties.

Post by davsanko »

#General Info:
Running PowerShell Studio 2021. V5.8.16

Hey Folks. I'm building a simple form-based script, or better call it a form-based tool for our HelpDesk reps to be able to quickly assist people in the organization with basic requests (Reset Password, Unlock Account, Add and Remove users from Active Directory Groups and etc.)
The Active Directory coding part I can do in my sleep as I've been writing PowerShell Automation codes for various Active Directory functions for a very long time now.
I'm however stumped by something which is probably trivial yet still escapes me.

The code below is meant to prevent anyone who is not in a predetermined Security group to be able to launch the internal scripts and forms, to that end, I need to allow the users to log in, I then check the credentials against the Active Directory LDAP service and show another form wth the actions the user can do based on his security group memberships.

I want the "Login" Button (see form design attached) the be invisible unless both the "UserName" and "Password" fields have any values OR the "Use Network credentials" checkbox is checked.
  1. $formLogin_Load={
  2.     $buttonLogin.Visible = $false
  3.     $buttonLogin.Enabled = $false
  4.    
  5. }
  6.  
  7. function Test-UserInput
  8. {
  9.     return $((($txt_pw.Text -eq "") -or ($txt_usr.Text -eq "")) -or (($txt_pw.Text -eq $null) -or ($txt_usr.Text -eq $null)))
  10. }
  11.  
  12. function Show-LoginButton
  13. {
  14.     {
  15.         $buttonLogin.Visible = $true
  16.         $buttonLogin.Enabled = $true
  17.     }
  18. }
  19.  
  20. function Hide-LoginButton
  21. {
  22.     {
  23.         $buttonLogin.Visible = $false
  24.         $buttonLogin.Enabled = $false
  25.     }
  26. }
  27.  
  28.  
  29. $checkboxUseNetworkCredential_CheckedChanged={
  30.     #TODO: Place custom script here
  31.     if ($checkboxUseNetworkCredential.Checked)
  32.     {
  33.         $txt_pw.Enabled = $false
  34.         $txt_pw.Text = $null
  35.         $txt_usr.Enabled = $false
  36.         $txt_usr.Text = $null
  37.         Show-LoginButton
  38.     }
  39.     else
  40.     {
  41.         $txt_pw.Enabled = $true
  42.         $txt_usr.Enabled = $true
  43.         Hide-LoginButton
  44.     }
  45. }
  46.  
  47. $buttonLogin_Click={
  48.     #TODO: Place custom script here
  49.     if ($checkboxUseNetworkCredential.Checked)
  50.     {
  51.         $Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
  52.     }
  53.     else
  54.     {
  55.         $Operator_uName = $txt_usr.Text
  56.         $Operator_PW = $txt_pw.Text
  57.     }
  58.    
  59. }
  60.  
  61. $txt_usr_TextChanged={
  62.     #TODO: Place custom script here
  63.     if (Test-UserInput)
  64.     {
  65.         Write-Host "At least one is empty"
  66.         Hide-LoginButton
  67.     }
  68.     else
  69.     {
  70.         Write-Host "Neither are empty"
  71.         Show-LoginButton
  72.     }
  73. }
  74.  
  75.  
  76. $txt_pw_TextChanged = {
  77.     #TODO: Place custom script here
  78.     if (Test-UserInput)
  79.     {
  80.         Write-Host "At least one is empty"
  81.         Hide-LoginButton
  82.     }
  83.     else
  84.     {
  85.         Write-Host "Neither are empty"
  86.         Show-LoginButton
  87.     }
  88. }
The problem is even when the conditions are met and the Show/Hide functions are triggered, the button remains the same as it is in the form load section - meaning, if I loaded the form with Enabled and Visible attributes of the login button set to $true, it will remain visible regardless of the values in the text boxes, the other is also true, meaning if I load the form with said attributes set to $false it will remain invisible. What am I missing here?
Any help you can provide on the matter will be greatly appreciated.
Sincerely,
Davsank.
Attachments
Form Design
Form Design
form error.JPG (25.3 KiB) Viewed 1797 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Button retains Visible and Enabled properties.

Post by jvierra »

My first suggestion is to remove all of teh utility functions and just write simple code. Th3re is never a need to code anything in th9is way and it almost always leads to hard to detect logic errors.

Your question clearly indicates and error in your logic.

The issue would be better handles using the built-in validation methods/events.

You can tst for an empty variable without using a comparison. "if('") and if($null)" both return false so we don't test for both conditions. An empty string is identical to a null string logically in PowerShell and most languages.

When stuck in issues like this the method of recovery is to simplify everything or use the debugger.

The aqctual design and usage of your code is a bit vague which indicates that your logical approach is also vague to you, and it shows in the code.

Start with one thing and solve that one issue first.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Button retains Visible and Enabled properties.

Post by jvierra »

You should also define a full USE case. Does the user normal use alternate credentials or current credentials. This is how to start a form definition. It tells how to set up the form. It also helps to remove most of your excess code.

Here is all that is needed as well as validation.
  1. $buttonLogin_Click={
  2.     if ($checkboxUseNetworkCredential.Checked){
  3.         $Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
  4.     }else{
  5.         $txt_usr = $txt_usr.Text
  6.         $Operator_PW = $txt_pw.Text
  7.     }
  8. }
  9.  
  10. $txt_usr_Validating=[System.ComponentModel.CancelEventHandler]{
  11. #Event Argument: $_ = [System.ComponentModel.CancelEventArgs]
  12.     if($this.Text){
  13.         #do nothing
  14.     }else{
  15.         # cancel if blank
  16.         [System.Windows.Forms.MessageBox]::Show('Must enter a value')
  17.         $_.Cancel = $true
  18.     }
  19. }
  20.  
  21. $txt_pw_Validating=[System.ComponentModel.CancelEventHandler]{
  22. #Event Argument: $_ = [System.ComponentModel.CancelEventArgs]
  23.     if($this.Text){
  24.         $buttonLogin.Visible = $true
  25.     }else{
  26.         # cancel if blank
  27.         [System.Windows.Forms.MessageBox]::Show('Must enter a value')
  28.         $_.Cancel = $true
  29.     }
  30. }
  31.  
  32. $checkboxUseNetworkCredential_Click_CheckedChanged={
  33.     $txt_pw.Visible = $this.Checked
  34.     $txt_usr.Visible = $this.Checked
  35.     $buttonLogin.Visible = $this.Checked
  36. }
davsanko
Posts: 2
Last visit: Sun Jun 26, 2022 10:00 am

Re: Button retains Visible and Enabled properties.

Post by davsanko »

jvierra wrote: Fri Dec 17, 2021 4:23 pm You should also define a full USE case. Does the user normal use alternate credentials or current credentials. This is how to start a form definition. It tells how to set up the form. It also helps to remove most of your excess code.

Here is all that is needed as well as validation.
  1. $buttonLogin_Click={
  2.     if ($checkboxUseNetworkCredential.Checked){
  3.         $Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
  4.     }else{
  5.         $txt_usr = $txt_usr.Text
  6.         $Operator_PW = $txt_pw.Text
  7.     }
  8. }
  9.  
  10. $txt_usr_Validating=[System.ComponentModel.CancelEventHandler]{
  11. #Event Argument: $_ = [System.ComponentModel.CancelEventArgs]
  12.     if($this.Text){
  13.         #do nothing
  14.     }else{
  15.         # cancel if blank
  16.         [System.Windows.Forms.MessageBox]::Show('Must enter a value')
  17.         $_.Cancel = $true
  18.     }
  19. }
  20.  
  21. $txt_pw_Validating=[System.ComponentModel.CancelEventHandler]{
  22. #Event Argument: $_ = [System.ComponentModel.CancelEventArgs]
  23.     if($this.Text){
  24.         $buttonLogin.Visible = $true
  25.     }else{
  26.         # cancel if blank
  27.         [System.Windows.Forms.MessageBox]::Show('Must enter a value')
  28.         $_.Cancel = $true
  29.     }
  30. }
  31.  
  32. $checkboxUseNetworkCredential_Click_CheckedChanged={
  33.     $txt_pw.Visible = $this.Checked
  34.     $txt_usr.Visible = $this.Checked
  35.     $buttonLogin.Visible = $this.Checked
  36. }
I Just tried your piece of code, Doesn't work either, I get exactly the same behaviour
This topic is 2 years and 3 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked