propertygrid element attributes

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 1 year and 11 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
Lembasts
Posts: 405
Last visit: Wed Mar 20, 2024 1:40 pm
Has voted: 1 time
Been upvoted: 1 time

propertygrid element attributes

Post by Lembasts »

I have just discovered the propertygrid control.
I can assign an object to it using the selectedobject property.
What I cant work is how to assign attributes to elements within the object such as descriptionattribute and categoryattribute.
I found some reference to system.componentmodel but cannot find examples of how to use this in powershell to assign element attributes in the propertygrid.
Thanks
David
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: propertygrid element attributes

Post by jvierra »

You have to use the events on the control to assign the property. The best method depends on the object and the property types of the object.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: propertygrid element attributes

Post by jvierra »

Here is about the simplest example I can come up with on short notice.

Run it at a prompt and see that it can directly set a string object.
  1. Add-Type -AssemblyName System.Windows.Forms
  2.  
  3. function ObjectViewer {
  4.     Param($obj)
  5.     $form = new-object "System.Windows.Forms.Form"
  6.     $form.Size = new-object System.Drawing.Size @(600, 600)
  7.     $PG = new-object "System.Windows.Forms.PropertyGrid"
  8.     $PG.Dock = [System.Windows.Forms.DockStyle]::Fill
  9.     $form.text = "$args"
  10.     $PG.selectedobject = $obj
  11.     $form.Controls.Add($PG)
  12.     $form.topmost = $true
  13.     $form.showdialog()
  14. }
  15.  
  16. $o = [System.Windows.Forms.TextBox]::New()
  17.  
  18. # when visible change the "Text" property
  19. ObjectViewer $o
  20.  
  21. # Now display the Text of the control
  22. $o.Text
This topic is 1 year and 11 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