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.
Post Reply
User avatar
Reddrgn2
Posts: 8
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

Code: Select all

$form1_Load={
#TODO: Initialize Form Controls here

}

$EmployeeNumber_Click={
#TODO: Place custom script here

}

$employeeID_TextChanged={
#TODO: Place custom script here
#$employeeID_TextChanged = Read-Host 'Please enter Emp ID'



}

$labelDepartment_Click={
#TODO: Place custom script here

}

$department_TextChanged={
#TODO: Place custom script here

}

#region Control Helper Functions
function Update-ComboBox
{
<#
.SYNOPSIS
This functions helps you load items into a ComboBox.

.DESCRIPTION
Use this function to dynamically load items into the ComboBox control.

.PARAMETER ComboBox
The ComboBox control you want to add items to.

.PARAMETER Items
The object or objects you wish to load into the ComboBox's Items collection.

.PARAMETER DisplayMember
Indicates the property to display for the items in this control.

.PARAMETER ValueMember
Indicates the property to use for the value of the control.

.PARAMETER Append
Adds the item(s) to the ComboBox without clearing the Items collection.

.EXAMPLE
Update-ComboBox $combobox1 "Red", "White", "Blue"

.EXAMPLE
Update-ComboBox $combobox1 "Red" -Append
Update-ComboBox $combobox1 "White" -Append
Update-ComboBox $combobox1 "Blue" -Append

.EXAMPLE
Update-ComboBox $combobox1 (Get-Process) "ProcessName"

.NOTES
Additional information about the function.
#>

param
(
[Parameter(Mandatory = $true)]
[ValidateNotNull()]
[System.Windows.Forms.ComboBox]
$ComboBox,
[Parameter(Mandatory = $true)]
[ValidateNotNull()]
$Items,
[Parameter(Mandatory = $false)]
[string]$DisplayMember,
[Parameter(Mandatory = $false)]
[string]$ValueMember,
[switch]
$Append
)

if (-not $Append)
{
$ComboBox.Items.Clear()
}

if ($Items -is [Object[]])
{
$ComboBox.Items.AddRange($Items)
}
elseif ($Items -is [System.Collections.IEnumerable])
{
$ComboBox.BeginUpdate()
foreach ($obj in $Items)
{
$ComboBox.Items.Add($obj)
}
$ComboBox.EndUpdate()
}
else
{
$ComboBox.Items.Add($Items)
}

$ComboBox.DisplayMember = $DisplayMember
$ComboBox.ValueMember = $ValueMember
}
#endregion

$officelocation_SelectedIndexChanged={
#TODO: Place custom script here

}

$GivenName_TextChanged={
#TODO: Place custom script here

}

$buttonCreateUser_Click={
#TODO: Place custom script here
Import-Module ActiveDirectory

}

$Displaynametext_TextChanged={
#TODO: Place custom script here

<#
$Displaynametext_TextChanged = $GivenName_TextChanged + $Surname_TextChanged
#>
}

$Surname_TextChanged={
#TODO: Place custom script here

}


$displaynameload_Click={
#TODO: Place custom script here
$First = $GivenName.text
$last = $Surname.Text
$labeldisplayname.Text = $First + " " + $last
}
Attachments
user creation gui.png
user creation gui.png (18.78 KiB) Viewed 154 times

User avatar
jvierra
Posts: 13681
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.

User avatar
jvierra
Posts: 13681
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

User avatar
jvierra
Posts: 13681
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.

User avatar
jvierra
Posts: 13681
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 5 times

User avatar
Reddrgn2
Posts: 8
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

User avatar
jvierra
Posts: 13681
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.

Post Reply