updating labels of multiple objects with incremental names

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 5 years and 10 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
seanoy
Posts: 6
Last visit: Mon Jun 10, 2019 9:33 pm

updating labels of multiple objects with incremental names

Post by seanoy »


Hi all,
im having some issues with updating labels of multiple label objects with incremental names: like "labelService1", "labelService2" etc
this is the loop im using but i cant get my head around it to make it work... :

Code: Select all

# loading services names from ini file
for ($i = 0; $i -lt $script:ini["ServicesNames"].count; $i++)
	{
                #these are several attempts i tried to play with... none works :/
		#$labelName = '$labelService'+$i
		#$labelName.Text = $script:ini["ServicesNames"]["Service$($i)"] 
		$labelService"$i".text = $script:ini["ServicesNames"]["Service$($i)"]
                $labelService$($i).text = $script:ini["ServicesNames"]["Service$($i)"] 
                ("$labelService+$i").text = $script:ini["ServicesNames"]["Service$($i)"] 
		
	}

	
	# this is what i actually want to perform:
	$labelService0.text = $script:ini["ServicesNames"]["Service0"]
	$labelService1.text = $script:ini["ServicesNames"]["Service1"]
	$labelService2.text = $script:ini["ServicesNames"]["Service2"]



how can i access an object with a variable in its name? or am i going at it the wrong way?

all tips are welcomed :)
thanks,
Sean
[/size]
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: updating labels of multiple objects with incremental names

Post by jvierra »

Without know what "$ini" is this is hard to anser but if your code correctly telss us then this is how to do this:

Code: Select all

# loading services names from ini file
for ($i = 0; $i -lt $ini['ServicesNames'].count-1; $i++){
    $form.Contrls["labelService$i"].text = $ini["ServicesNames"]["Service$i"]
}
User avatar
seanoy
Posts: 6
Last visit: Mon Jun 10, 2019 9:33 pm

Re: updating labels of multiple objects with incremental names

Post by seanoy »

Hi jvierra
how are you? thank you for your reply!
form.control sounds like the way to do it, i just need to figure out how to access the labels as this does not work..
im trying to write the label's text (initial text is "Service #") to a richtextbox:
  1. $richtextbox1.Text += $form1.Controls("labelService$i").text
  2. $richtextbox1.Text += $form1.Controls["labelService$i"].text
  3.  
the ini file is a config file to load some names from. im using the get-inicontent function from: http://gallery.technet.microsoft.com/sc ... d7a76e8d91

thanks again

Sean
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: updating labels of multiple objects with incremental names

Post by jvierra »

Are you using SPS? What are the actual label names?

$<a label control>.Name

If the label is in a container then use the "Controls" property of the container.
User avatar
seanoy
Posts: 6
Last visit: Mon Jun 10, 2019 9:33 pm

Re: updating labels of multiple objects with incremental names

Post by seanoy »

Jvierra like always you are the bauss :D
  1. $groupboxServices.Controls["labelService$i"].text = $script:ini["ServicesNames"]["Service$($i)"]
they were in a groupbox.

thank you very much for all the help!

Sean
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: updating labels of multiple objects with incremental names

Post by jvierra »

Just another brick in the waull.
You are very welcome.
This topic is 5 years and 10 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