is dynamic dialogs possible?

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 3 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
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

is dynamic dialogs possible?

Post by Domtar »

Hi all,

Since I have no wish to lose my time, before I put too much effort into this, I thought about asking this question.

I'm building a GUI where one of the options is to select the SCCM Distribution Points where to replicate the packages we create. That DPs list changes from time to time whenever we open a new location. Seeing that, I'm looking for a way to dynamically create the controls I need.

My question is: is it possible to loop thru a list and dynamically create a control for each item in the list? I tried it for like an hour and I am not near a solution.

Maybe what I want cannot be done?

Thanks!
rslaav
Posts: 25
Last visit: Sat Oct 01, 2022 6:11 am
Has voted: 2 times

Re: is dynamic dialogs possible?

Post by rslaav »

excellent issue, i'd like to know as well :)

note: I think we need to write lambda expressions in this situation, from what I gathered earlier. I am looking at similar problems, but not with gui issues.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: is dynamic dialogs possible?

Post by jvierra »

Yes. There are many ways to do this. I have used a FlowLayoutPanel to generate and position controls. Another way is to use a TabControl to create collections of controls.

I would first start by rethinking what you need to do as ListBoxes and DataGrids can usually perform the same tasks and actually are controls that auto-generate other controls.

What you are asking is a co0mmion thing in Forms. If you search you will find many examples of "Programmatically created controls".

Here is an example of a form that generates all of its controls of of any arbitrary data row object.

Code: Select all

$bindingSource = New-Object System.Windows.Forms.BindingSource
$bindingSource.DataSource = $customer

$form1_Load={
	
	$Customer.DataView.DataViewManager.DataSet.Tables[0].Columns | 
		ForEach{
			$tb = New-Object System.Windows.Forms.TextBox
			$tb.Width = 300
			$lb = New-Object System.Windows.Forms.Label
			$lb.Text = $_.ColumnName
			$lb.Width = 150
			$lb.Dock = 'Fill'
			$lb.Textalign = 'MiddleRight'
			$tablelayoutpanel1.Controls.Add($lb,0, $row)
			$tablelayoutpanel1.Controls.Add($tb,1, $row)		
			$tb.DataBindings.Add('Text', $bindingSource, $_.ColumnName)
			$row++
    	}
}


$buttonSave_Click={
	$Customer.EndEdit()
}

$form1_FormClosing=[System.Windows.Forms.FormClosingEventHandler]{
	if($form1.DialogResult -eq 'Cancel'){
		$Customer.CancelEdit()
    }
}
User avatar
Domtar
Posts: 133
Last visit: Mon Mar 11, 2024 5:38 am
Has voted: 2 times

Re: is dynamic dialogs possible?

Post by Domtar »

i will study your piece of code.

thanks for the help!
This topic is 3 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