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.
Locked
User avatar
Domtar
Posts: 90
Joined: Sun Mar 03, 2013 12:42 pm

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: 14
Joined: Mon Jun 08, 2020 2:01 pm

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: 14452
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 90
Joined: Sun Mar 03, 2013 12:42 pm

Re: is dynamic dialogs possible?

Post by Domtar »

i will study your piece of code.

thanks for the help!

Anayazius
Posts: 6
Joined: Tue Jun 23, 2020 5:04 am

Re: is dynamic dialogs possible?

Post by Anayazius »

it became clearer to me now. thank you!

Locked