Automatically Populating Label/text boxs

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.
Locked
User avatar
Reddrgn2
Posts: 17
Joined: Wed May 23, 2018 6:13 am

Automatically Populating Label/text boxs

Post by Reddrgn2 » Wed Aug 14, 2019 2:14 pm

Product – Powershell studio 2019, version 5.6.164
OS – win 10
Os version – 64b
Basic idea, AD user creations, goal to see if there is a way to automatically populate 3 variables.
1. To populate/display the given name and last name automatically – Display name
2. Count the given name, count “.” and count the last name.
a. Apples(given) then add “.”, then counts Grapes(last)
b. Total of 13
c. Display total as typing
3. If able to get 2 to work correctly, then if the character count is over 20. To show an error or warning – something to get attention.

This is just really a rough outline of the script below, included is a screen shot of the GUI. On the GUI there is a button to press that will display the first and last, but that is a manual trigger. (Screen shot, out is right below “display name”
I think that if I am able to get these few things working I will be able to finish and put the “Create User” button script in. *took out because I forgot I put in place and was creating users.
Any guidance would be helpful.

Image
  1. $form1_Load={
  2.  
  3.     #TODO: Initialize Form Controls here
  4.  
  5.    
  6.  
  7. }
  8.  
  9.  
  10.  
  11. $EmployeeNumber_Click={
  12.  
  13.     #TODO: Place custom script here
  14.  
  15.    
  16.  
  17. }
  18.  
  19.  
  20.  
  21. $employeeID_TextChanged={
  22.  
  23.     #TODO: Place custom script here
  24.  
  25.     #$employeeID_TextChanged = Read-Host 'Please enter Emp ID'
  26.  
  27.    
  28.  
  29.    
  30.  
  31.    
  32.  
  33. }
  34.  
  35.  
  36.  
  37. $labelDepartment_Click={
  38.  
  39.     #TODO: Place custom script here
  40.  
  41.    
  42.  
  43. }
  44.  
  45.  
  46.  
  47. $department_TextChanged={
  48.  
  49.     #TODO: Place custom script here
  50.  
  51.    
  52.  
  53. }
  54.  
  55.  
  56.  
  57. #region Control Helper Functions
  58.  
  59. function Update-ComboBox
  60.  
  61. {
  62.  
  63. <#
  64.  
  65.     .SYNOPSIS
  66.  
  67.         This functions helps you load items into a ComboBox.
  68.  
  69.    
  70.  
  71.     .DESCRIPTION
  72.  
  73.         Use this function to dynamically load items into the ComboBox control.
  74.  
  75.    
  76.  
  77.     .PARAMETER ComboBox
  78.  
  79.         The ComboBox control you want to add items to.
  80.  
  81.    
  82.  
  83.     .PARAMETER Items
  84.  
  85.         The object or objects you wish to load into the ComboBox's Items collection.
  86.  
  87.    
  88.  
  89.     .PARAMETER DisplayMember
  90.  
  91.         Indicates the property to display for the items in this control.
  92.  
  93.        
  94.  
  95.     .PARAMETER ValueMember
  96.  
  97.         Indicates the property to use for the value of the control.
  98.  
  99.    
  100.  
  101.     .PARAMETER Append
  102.  
  103.         Adds the item(s) to the ComboBox without clearing the Items collection.
  104.  
  105.    
  106.  
  107.     .EXAMPLE
  108.  
  109.         Update-ComboBox $combobox1 "Red", "White", "Blue"
  110.  
  111.    
  112.  
  113.     .EXAMPLE
  114.  
  115.         Update-ComboBox $combobox1 "Red" -Append
  116.  
  117.         Update-ComboBox $combobox1 "White" -Append
  118.  
  119.         Update-ComboBox $combobox1 "Blue" -Append
  120.  
  121.    
  122.  
  123.     .EXAMPLE
  124.  
  125.         Update-ComboBox $combobox1 (Get-Process) "ProcessName"
  126.  
  127.    
  128.  
  129.     .NOTES
  130.  
  131.         Additional information about the function.
  132.  
  133. #>
  134.  
  135.    
  136.  
  137.     param
  138.  
  139.     (
  140.  
  141.         [Parameter(Mandatory = $true)]
  142.  
  143.         [ValidateNotNull()]
  144.  
  145.         [System.Windows.Forms.ComboBox]
  146.  
  147.         $ComboBox,
  148.  
  149.         [Parameter(Mandatory = $true)]
  150.  
  151.         [ValidateNotNull()]
  152.  
  153.         $Items,
  154.  
  155.         [Parameter(Mandatory = $false)]
  156.  
  157.         [string]$DisplayMember,
  158.  
  159.         [Parameter(Mandatory = $false)]
  160.  
  161.         [string]$ValueMember,
  162.  
  163.         [switch]
  164.  
  165.         $Append
  166.  
  167.     )
  168.  
  169.    
  170.  
  171.     if (-not $Append)
  172.  
  173.     {
  174.  
  175.         $ComboBox.Items.Clear()
  176.  
  177.     }
  178.  
  179.    
  180.  
  181.     if ($Items -is [Object[]])
  182.  
  183.     {
  184.  
  185.         $ComboBox.Items.AddRange($Items)
  186.  
  187.     }
  188.  
  189.     elseif ($Items -is [System.Collections.IEnumerable])
  190.  
  191.     {
  192.  
  193.         $ComboBox.BeginUpdate()
  194.  
  195.         foreach ($obj in $Items)
  196.  
  197.         {
  198.  
  199.             $ComboBox.Items.Add($obj)
  200.  
  201.         }
  202.  
  203.         $ComboBox.EndUpdate()
  204.  
  205.     }
  206.  
  207.     else
  208.  
  209.     {
  210.  
  211.         $ComboBox.Items.Add($Items)
  212.  
  213.     }
  214.  
  215.    
  216.  
  217.     $ComboBox.DisplayMember = $DisplayMember
  218.  
  219.     $ComboBox.ValueMember = $ValueMember
  220.  
  221. }
  222.  
  223. #endregion
  224.  
  225.  
  226.  
  227. $officelocation_SelectedIndexChanged={
  228.  
  229.     #TODO: Place custom script here
  230.  
  231.    
  232.  
  233. }
  234.  
  235.  
  236.  
  237. $GivenName_TextChanged={
  238.  
  239.     #TODO: Place custom script here
  240.  
  241.    
  242.  
  243. }
  244.  
  245.  
  246.  
  247. $buttonCreateUser_Click={
  248.  
  249.     #TODO: Place custom script here
  250.  
  251.     Import-Module ActiveDirectory
  252.  
  253.    
  254.  
  255. }
  256.  
  257.  
  258.  
  259. $Displaynametext_TextChanged={
  260.  
  261.     #TODO: Place custom script here
  262.  
  263.        
  264.  
  265.     <#
  266.  
  267.     $Displaynametext_TextChanged = $GivenName_TextChanged + $Surname_TextChanged
  268.  
  269.     #>
  270.  
  271. }
  272.  
  273.  
  274.  
  275. $Surname_TextChanged={
  276.  
  277.     #TODO: Place custom script here
  278.  
  279.    
  280.  
  281. }
  282.  
  283.  
  284.  
  285.  
  286.  
  287. $displaynameload_Click={
  288.  
  289.     #TODO: Place custom script here
  290.  
  291.     $First = $GivenName.text
  292.  
  293.     $last = $Surname.Text
  294.  
  295.     $labeldisplayname.Text = $First + " " + $last
  296.  
  297. }
Attachments
user creation gui.png
user creation gui.png (18.78 KiB) Viewed 376 times

jvierra
Posts: 13934
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Automatically Populating Label/text boxs

Post by jvierra » Wed Aug 14, 2019 2:27 pm

I am sorry but it is almost impossible to under4stand your question. Can you just ask a single question.

Also posting a snippet of code and an image really doesn't help much in this case. It is better to create a simple PSF file that illustrates your question and post the PSF file as an attachment.

Don't try to explain what you are doing. Ask a basic question about how to do some single thing. Take this a step at a time as that ill be easier for us to understand.

jvierra
Posts: 13934
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Automatically Populating Label/text boxs

Post by jvierra » Wed Aug 14, 2019 2:29 pm

The best I can guess here is that you want to know how to count the characters in a string.


# get the number of characters in a string
$mystring ='some set of characters'
$mystring.Length

jvierra
Posts: 13934
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Automatically Populating Label/text boxs

Post by jvierra » Wed Aug 14, 2019 2:32 pm

A couple of suggestions.

Use the "Validating" event to check the length and display an error.

You can also use the "TextChanged" event to assign the full name as the name is typed.

jvierra
Posts: 13934
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Automatically Populating Label/text boxs

Post by jvierra » Wed Aug 14, 2019 2:43 pm

I think this is what you are trying to ask.
Attachments
Test-Junk1.psf
(23.12 KiB) Downloaded 14 times

User avatar
Reddrgn2
Posts: 17
Joined: Wed May 23, 2018 6:13 am

Re: Automatically Populating Label/text boxs

Post by Reddrgn2 » Thu Aug 15, 2019 5:53 am

Apologies, I wanted to give as much information as to the idea/goal. To show how things are laying out on the gui and in the script part. I didn't think of adding the PSF file, will make note to do that next time.

To the point you nailed what I was wanting to do, thanks

jvierra
Posts: 13934
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Automatically Populating Label/text boxs

Post by jvierra » Thu Aug 15, 2019 7:04 am

Sometimes I guess well. Good luck.

Locked