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.
This topic is 4 years and 8 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
User avatar
Reddrgn2
Posts: 49
Last visit: Wed Nov 08, 2023 10:34 am
Answers: 1

Automatically Populating Label/text boxs

Post by Reddrgn2 »

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.     #TODO: Initialize Form Controls here
  3.    
  4. }
  5.  
  6. $EmployeeNumber_Click={
  7.     #TODO: Place custom script here
  8.    
  9. }
  10.  
  11. $employeeID_TextChanged={
  12.     #TODO: Place custom script here
  13.     #$employeeID_TextChanged = Read-Host 'Please enter Emp ID'
  14.    
  15.    
  16.    
  17. }
  18.  
  19. $labelDepartment_Click={
  20.     #TODO: Place custom script here
  21.    
  22. }
  23.  
  24. $department_TextChanged={
  25.     #TODO: Place custom script here
  26.    
  27. }
  28.  
  29. #region Control Helper Functions
  30. function Update-ComboBox
  31. {
  32. <#
  33.     .SYNOPSIS
  34.         This functions helps you load items into a ComboBox.
  35.    
  36.     .DESCRIPTION
  37.         Use this function to dynamically load items into the ComboBox control.
  38.    
  39.     .PARAMETER ComboBox
  40.         The ComboBox control you want to add items to.
  41.    
  42.     .PARAMETER Items
  43.         The object or objects you wish to load into the ComboBox's Items collection.
  44.    
  45.     .PARAMETER DisplayMember
  46.         Indicates the property to display for the items in this control.
  47.        
  48.     .PARAMETER ValueMember
  49.         Indicates the property to use for the value of the control.
  50.    
  51.     .PARAMETER Append
  52.         Adds the item(s) to the ComboBox without clearing the Items collection.
  53.    
  54.     .EXAMPLE
  55.         Update-ComboBox $combobox1 "Red", "White", "Blue"
  56.    
  57.     .EXAMPLE
  58.         Update-ComboBox $combobox1 "Red" -Append
  59.         Update-ComboBox $combobox1 "White" -Append
  60.         Update-ComboBox $combobox1 "Blue" -Append
  61.    
  62.     .EXAMPLE
  63.         Update-ComboBox $combobox1 (Get-Process) "ProcessName"
  64.    
  65.     .NOTES
  66.         Additional information about the function.
  67. #>
  68.    
  69.     param
  70.     (
  71.         [Parameter(Mandatory = $true)]
  72.         [ValidateNotNull()]
  73.         [System.Windows.Forms.ComboBox]
  74.         $ComboBox,
  75.         [Parameter(Mandatory = $true)]
  76.         [ValidateNotNull()]
  77.         $Items,
  78.         [Parameter(Mandatory = $false)]
  79.         [string]$DisplayMember,
  80.         [Parameter(Mandatory = $false)]
  81.         [string]$ValueMember,
  82.         [switch]
  83.         $Append
  84.     )
  85.    
  86.     if (-not $Append)
  87.     {
  88.         $ComboBox.Items.Clear()
  89.     }
  90.    
  91.     if ($Items -is [Object[]])
  92.     {
  93.         $ComboBox.Items.AddRange($Items)
  94.     }
  95.     elseif ($Items -is [System.Collections.IEnumerable])
  96.     {
  97.         $ComboBox.BeginUpdate()
  98.         foreach ($obj in $Items)
  99.         {
  100.             $ComboBox.Items.Add($obj)
  101.         }
  102.         $ComboBox.EndUpdate()
  103.     }
  104.     else
  105.     {
  106.         $ComboBox.Items.Add($Items)
  107.     }
  108.    
  109.     $ComboBox.DisplayMember = $DisplayMember
  110.     $ComboBox.ValueMember = $ValueMember
  111. }
  112. #endregion
  113.  
  114. $officelocation_SelectedIndexChanged={
  115.     #TODO: Place custom script here
  116.    
  117. }
  118.  
  119. $GivenName_TextChanged={
  120.     #TODO: Place custom script here
  121.    
  122. }
  123.  
  124. $buttonCreateUser_Click={
  125.     #TODO: Place custom script here
  126.     Import-Module ActiveDirectory
  127.    
  128. }
  129.  
  130. $Displaynametext_TextChanged={
  131.     #TODO: Place custom script here
  132.        
  133.     <#
  134.     $Displaynametext_TextChanged = $GivenName_TextChanged + $Surname_TextChanged
  135.     #>
  136. }
  137.  
  138. $Surname_TextChanged={
  139.     #TODO: Place custom script here
  140.    
  141. }
  142.  
  143.  
  144. $displaynameload_Click={
  145.     #TODO: Place custom script here
  146.     $First = $GivenName.text
  147.     $last = $Surname.Text
  148.     $labeldisplayname.Text = $First + " " + $last
  149. }
Attachments
user creation gui.png
user creation gui.png (18.78 KiB) Viewed 1830 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Automatically Populating Label/text boxs

Post by jvierra »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Automatically Populating Label/text boxs

Post by jvierra »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Automatically Populating Label/text boxs

Post by jvierra »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Automatically Populating Label/text boxs

Post by jvierra »

I think this is what you are trying to ask.
Attachments
Test-Junk1.psf
(23.12 KiB) Downloaded 103 times
User avatar
Reddrgn2
Posts: 49
Last visit: Wed Nov 08, 2023 10:34 am
Answers: 1

Re: Automatically Populating Label/text boxs

Post by Reddrgn2 »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Automatically Populating Label/text boxs

Post by jvierra »

Sometimes I guess well. Good luck.
This topic is 4 years and 8 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