Struggling with Add_Click RRS feed

  • Question

  • Hello everyone,

    I am working on a GUI with Forms and have built a textbox, where user data is inserted.

    I have used Add_Click to do some checks regarding possible user input errors. Therefore I have within Add_Click if/Elseif/Else

    to do some routine checks and it works perfectly.

    I end it with the following:

    Else {$form.hide()}

    As mentioned above, it works well and as soon as all data has been entered correctly, I press the OK button, the form closes as required.

    Now, I wanted to add at the end another IF/Elseif/Else code. The function behind it is, to check, how long the entered name is and if it is less than 5 characters, to use as many characters as there are available and if the name is more then 5 characters, to use 5. This is actually used, to generate a user-id and prepared for Active Directory.

    {if($CreateNewUserFormTextboxLastname.TextLength -lt 5) {$useridfirstpart = $CreateNewUserFormTextboxLastname.TextLength} else {$useridfirstpart =5}

    I actually have the code and it also works but how can i entere it before the else or within the else function? I tried several ways but was not able to solve it, because it usually did end on a way, that it stopped and usually did not even create the variable $useridfirstpart. If tried to use elseif, then I lacked at the end, that I was not able to use else.

    I assume, it is very easy, I unfortunately was not able to figure it out.

    The target at the end is, that as soon as I click the OK button, that the name entered in the textbox is anaylised by the length of the name and if longer then 5 characters will use the first 5 characters and if shorter, will use the available characters, so I can generate a user-id for Active Directory.

    Can someone please enlighten me?

    Thank you very much,


    Thursday, January 31, 2019 5:48 PM


All replies

  • "hiding" a dialog is the same as closing the dialog.

    You can set a maximum input length on the textbos.

    $tb = [System.Windows.Forms.TextBox]::New()
    $tb.MaxLength = 5

    You should also use the "validating" event to validate a controls input.


    • Edited by jrv Thursday, January 31, 2019 6:09 PM
    Thursday, January 31, 2019 6:09 PM
  • Dear jrv,

    Thank you very much for your answer and support as always. That is very good to know but I need to be able to have any input length, because it will be an input field for a Last-name, that is going to be used for Active Directory.  Therefore if the Last-name is going to be 10 letters long, I need the full 10 letters and from there, I need to be able to prepare and generate the User-ID (SamAccountName), I need the first 5 letters from the Last-name and if there is less, then only what I will get.

    There will be a similar inquiry regarding the first-name, the only difference there will be, that form the last name, I only need the two first letters, and with that, I will generate the User-ID, which is (if available)

    5 first letters last-name + 2 first letters first-name and ad a number starting with 1.

    As an example: John Doe would become DoeJo1 and a Arnold Schwarzzenegger would become SchwaAr1.

    Is there any way, I can check for the input and prepare it, before I close the dialog? I would like to prepare the User-ID before I close the dialog, if there is any possibility, but I am sure, there is some way, where I could do, that.

    Thank you very much,


    Friday, February 1, 2019 3:23 AM
  • There is no limit for the "surname" field in AD.  The "Name" is also not limited.  It is only SamAccountName and that allows 20 characters.  You have been reading very old documentation that limits the computer name to 10 characters dues to WINS/NetBIOS restrictions in pre-Windows NT systems.   We haven't used WINS for more than a decade.


    Friday, February 1, 2019 3:33 AM
  • Sorry, that is my bad.

    It is the way, how the user-id is generated and therefore I need to take the surname apart as explained. I just tried to explain, how the SamAccountName in my case is put together and why I would like to have within the "Add_Click" the possibility, to prepare the SamAccountName, so it can be used to be added to Active Directory.

    It was no intention to confuse, therefore sorry.

    Any way, I can build that into the code, before I close the dialog?

    Thank you,


    PS: If preferred, I can show a simple example of the code but would have to generate it, because my is far too large to show, so I would need a little time to generate an example, if required.

    Friday, February 1, 2019 3:47 AM
    • Marked as answer by mstgier Monday, February 18, 2019 11:23 PM
    Friday, February 1, 2019 3:50 AM
  • OK, I will look into this,

    Thank you jrv

    Friday, February 1, 2019 4:09 AM