Sign in to follow this  
Followers 0
darkleton

Button Help desperately needed

6 posts in this topic

#1 ·  Posted (edited)

I've got a GUI that will show the account status of a given user. It will show if the users account has been disabled.

What I wanted was for it to read the accountdisabled variable, then display a button to reverse the change, so if it was enabled, button would let you disable and vice versa.

that works ok, but if i click disable, and the area refreshes and shows its now disabled, the button changes to enable (as it should), but if i move over the button with my mouse, or tab to it, it automatically reverts back to disabled. this means if i click the box nothing happens as the action has already happened.

it seems to me that the script is ignoring IF commands and just running through the lot, as whichever command (enable/disable) is first in the script, thats the button it reverts to. if i swap them round in the script, the buttons revert on mouse over.

it's driving me absolutely insane, and i have a feeling its something stupidly simple that i'm overlooking.

all the actions are taken up to that point, the account is enabled or disabled properly, its just the buttons.

I have the DisplayUserInfo() call at the end of the other functions so it automatically updates the display after you hit the button. It's the only way I could work out to have the display auto update without the constant flickering.

anyway here's the code:

Func DisplayUserInfo()

$leftfqdn = ReadFQDNFromGUILeft()
$leftsam = _ADDNToSamAccountName ($leftfqdn)
$UserObj = ObjGet("LDAP://" & $leftfqdn)
$acctstatus = $UserObj.AccountDisabled

GUICtrlCreateLabel ("Username: ", 650, 90, 100, 20)   
GUICtrlCreateLabel ("First Name: ", 650, 145, 100, 20)   
GUICtrlCreateLabel ("Last Name: ", 650, 170, 100, 20)   
GUICtrlCreateLabel ("Display Name: ", 650, 200, 100, 20)   
GUICtrlCreateLabel ("Description: ", 650, 230, 100, 20)   
GUICtrlCreateLabel ("Logon Script: ", 650, 285, 100, 20)   
GUICtrlCreateLabel ("Password Last Changed: ", 650, 315, 100, 30)   
GUICtrlCreateLabel ("Last Logon: ", 650, 355, 100, 20) 
GUICtrlCreateLabel ("Account Disabled: ", 650, 385, 100, 20) 

GUICtrlCreateLabel ( ''& $leftsam, 750, 90, 100, 20)
GUICtrlCreateLabel (''& $UserObj.FirstName, 750, 145, 100, 20)   
GUICtrlCreateLabel (''& $UserObj.LastName, 750, 170, 100, 20)   
GUICtrlCreateLabel (''& $UserObj.FullName, 750, 200, 300, 20)   
GUICtrlCreateLabel (''& $UserObj.Description, 750, 230, 300, 20)  
GUICtrlCreateLabel (''& $UserObj.LoginScript, 750, 285, 200, 20)

$lastchange = $UserObj.PasswordLastChanged
$Date = StringMid($lastchange, 7, 2) & "/" & StringMid($lastchange, 5, 2) & "/" & StringMid($lastchange, 1, 4)
$Time = StringMid($lastchange, 9, 2) & ":" & StringMid($lastchange, 11, 2)
GUICtrlCreateLabel ($Time & " - "& $Date, 750, 315, 150, 20)

$lastlogin = $UserObj.LastLogin
$Date = StringMid($lastlogin, 7, 2) & "/" & StringMid($lastlogin, 5, 2) & "/" & StringMid($lastlogin, 1, 4)
$Time = StringMid($lastlogin, 9, 2) & ":" & StringMid($lastlogin, 11, 2) 
GUICtrlCreateLabel ($Time & " - "& $Date, 750, 355, 150, 20)

$btn_resetpass = GUICtrlCreateButton("Reset Users Password", 700, 500, 130, 20)
GUICtrlSetOnEvent($btn_resetpass, "ResetPass")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ALL OK UP TO HERE                      ;;
;; THIS IS WHERE PROBLEMS BEGIN ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

If $acctstatus == 0 Then 
    GUICtrlCreateLabel ('No', 750, 385, 20, 15)
    GUICtrlSetBkColor(-1, 0x00ff00);Green
    $btn_disableacct = GUICtrlCreateButton("Disable Users Account", 700, 520, 130, 20,$WS_DISABLED)
    GUICtrlSetState($btn_disableacct, $GUI_ENABLE)
    GUICtrlSetOnEvent($btn_disableacct, "DisableAcct")
EndIf

If $acctstatus < 0 Then 
    GUICtrlCreateLabel ('Yes', 750, 385, 20, 15)
    GUICtrlSetBkColor(-1, 0xEE0000);Red
    $btn_enableacct = GUICtrlCreateButton("Enable Users Account", 700, 520, 130, 20,$WS_DISABLED)
    GUICtrlSetState($btn_enableacct, $GUI_ENABLE)
    GUICtrlSetOnEvent($btn_enableacct, "EnableAcct")
EndIf

EndFunc 

Func DisableAcct()
    $leftfqdn = ReadFQDNFromGUILeft()
    $leftsam = _ADDNToSamAccountName ($leftfqdn)
    $UserObj = ObjGet("LDAP://" & $leftfqdn)
    $UserObj.AccountDisabled = -1
    $UserObj.SetInfo
    MsgBox(0, "", $leftsam & "'s account has been disabled", 2)
    DisplayUserInfo()
EndFunc 


Func EnableAcct()
    $leftfqdn = ReadFQDNFromGUILeft()
    $leftsam = _ADDNToSamAccountName ($leftfqdn)
    $UserObj = ObjGet("LDAP://" & $leftfqdn)
    $UserObj.AccountDisabled = 0
    $UserObj.SetInfo
    MsgBox(0, "", $leftsam & "'s account has been enabled", 2)
    DisplayUserInfo()
EndFunc

If my description sounded crap, this is a screencapture of what is happening:

Button Problem Example

Many thanks in advance!

Edited by darkleton

Share this post


Link to post
Share on other sites



Maybe you could delete the "old" button before creating a new one.

Let's say you've created a "Disable Users Account" button and you've clicked it this way disabling the user account.

you can change the following ...

If $acctstatus < 0 Then 
    GUICtrlCreateLabel ('Yes', 750, 385, 20, 15)
    GUICtrlSetBkColor(-1, 0xEE0000);Red

    GUICtrlDelete ($btn_disableacct)

    $btn_enableacct = GUICtrlCreateButton("Enable Users Account", 700, 520, 130, 20,$WS_DISABLED)
    GUICtrlSetState($btn_enableacct, $GUI_ENABLE)
    GUICtrlSetOnEvent($btn_enableacct, "EnableAcct")
EndIf

and

If $acctstatus == 0 Then 
    GUICtrlCreateLabel ('No', 750, 385, 20, 15)
    GUICtrlSetBkColor(-1, 0x00ff00);Green
    
    GUICtrlDelete ($btn_enableacct)

$btn_disableacct = GUICtrlCreateButton("Disable Users Account", 700, 520, 130, 20,$WS_DISABLED)
    GUICtrlSetState($btn_disableacct, $GUI_ENABLE)
    GUICtrlSetOnEvent($btn_disableacct, "DisableAcct")
EndIf

Maybe you can detect if the button exists before trying to delete it ... it is up to you.

Also I've noticed you used "==" operator ... it is needed? a simple "=" can't do the trick?

and you can always use a "Select" statement

Select
    Case $acctstatus = 0 
     ;your part here ....
    Case Else
     ;your other part here ...
EndSelect

Hope any of these ideas could help.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

i tried the delete line like you said, after i click on enable or disable once, it wipes them both off for good.

i did try using select and case, and ended up with the same result. it just seems like its not paying any attention to CASE or IF or any sort of arguments, it is just working through that part of the script regardless.

how would i make it detect if a button exists? i might try that if i know what arguments to use

thank you for this, it's been driving me crazy all day

Share this post


Link to post
Share on other sites

i tried the delete line like you said, after i click on enable or disable once, it wipes them both off for good.

i did try using select and case, and ended up with the same result. it just seems like its not paying any attention to CASE or IF or any sort of arguments, it is just working through that part of the script regardless.

how would i make it detect if a button exists? i might try that if i know what arguments to use

thank you for this, it's been driving me crazy all day

Sorry I couldn't give you better ideas ...

About how to detect if a button exists ... The only thing I can think of is using a variable (like a flag) for each button (enable or disable button) let's say you created the "Disable" button - then use a variable $disable_buton_flag = 1 (only an example) if the button exists or 0 if the button doesn't exist.

Every time you create the button just set the value to 1 and when you delete it , set it to 0 (it behaves like a flag)

You can do the following:

If $acctstatus < 0 Then 
    GUICtrlCreateLabel ('Yes', 750, 385, 20, 15)
    GUICtrlSetBkColor(-1, 0xEE0000);Red
    
    If $disable_buton_flag = 1 Then 
        GUICtrlDelete ($btn_disableacct)
        $disable_buton_flag = 0
    EndIf
    $btn_enableacct = GUICtrlCreateButton("Enable Users Account", 700, 520, 130, 20,$WS_DISABLED)
    $enable_buton_flag = 1
    
    GUICtrlSetState($btn_enableacct, $GUI_ENABLE)
    GUICtrlSetOnEvent($btn_enableacct, "EnableAcct")
EndIf

I'm sure there is a better (faster and shorter) way to do this but unfortunately this is all I can think now of.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

enaiman: Thank you very much for all the assistance, it's given me things to consider for future projects too.

A friend gave me a hand with this one, and I don't know if this is faster or shorter than any other method, but it works so I'll go with it :whistle:

Global $btn_acctstatus = GUICtrlCreateButton("", 700, 520, 130, 20)
GUICtrlSetOnEvent($btn_acctstatus, "ChangeAcctStatus")
Global $lab_acctstatus = GUICtrlCreateLabel ("", 750, 385, 20, 15)

    If $UserObj.AccountDisabled == 0 Then 
        GUICtrlSetData($lab_acctstatus, "No")
        GUICtrlSetBkColor($lab_acctstatus, 0x00ff00);Green
        GUICtrlSetData($btn_acctstatus, "Disable Users Account")
    Else
        GUICtrlSetData($lab_acctstatus, "Yes")
        GUICtrlSetBkColor($lab_acctstatus, 0xEE0000);Red
        GUICtrlSetData($btn_acctstatus, "Enable Users Account")
    EndIf

EndFunc 

Func ChangeAcctStatus()
    $leftfqdn = ReadFQDNFromGUILeft()
    $leftsam = _ADDNToSamAccountName ($leftfqdn)
    $UserObj = ObjGet("LDAP://" & $leftfqdn)

    If $UserObj.AccountDisabled == 0 Then 
        $UserObj.AccountDisabled = -1
        GUICtrlSetData($lab_acctstatus, "Yes")
        GUICtrlSetBkColor($lab_acctstatus, 0xEE0000);Red
        GUICtrlSetData($btn_acctstatus, "Enable Users Account")
        MsgBox(0, "", $leftsam & "'s account has been disabled", 2)
    Else
        $UserObj.AccountDisabled = 0
        GUICtrlSetData($lab_acctstatus, "No")
        GUICtrlSetBkColor($lab_acctstatus, 0x00ff00);Green
        GUICtrlSetData($btn_acctstatus, "Disable Users Account")
        MsgBox(0, "", $leftsam & "'s account has been Enabled", 2)
    EndIf

    $UserObj.SetInfo
EndFunc

Share this post


Link to post
Share on other sites

Glad you made it :whistle:

I'm sorry because I couldn't be of so much help to you.

Anyway ... good luck :P


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0