Passing data to child form control

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 8 years and 1 month 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
PsCustomObject
Posts: 137
Last visit: Thu Mar 28, 2024 3:34 am

Passing data to child form control

Post by PsCustomObject »

Ok title is not that self explanatory but could not come up with something better so let me try to explain.

I have a GUI project and a main form with couple of buttons each carrying on some task, each task (like getting mailboxes or AD users for example) writes a log through a function in a richtextbox used to keep a verbose log of every action taken.

I would like to move the aforementioned richtextbox to a child form that is shown upon user's request and was wondering how to handle this as if I try to send data to the control directly from the main form I get errors.

I thought of appending data to a variable each time an action is carried on and then display variable's data into the child richtexbox and this works but I end up with duplicate entries in the log which is not exactly what I want.

I'm sure there is a simple workaround to this or I'm doing something wrong but can't seem able to spot it.

Any idea/suggestion?

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

Re: Passing data to child form control

Post by jvierra »

If you are logging to a file just open the file in the child form. Why pass a huge chunk of data.

Consider using an MDO form and log to an MDI child.
User avatar
PsCustomObject
Posts: 137
Last visit: Thu Mar 28, 2024 3:34 am

Re: Passing data to child form control

Post by PsCustomObject »

Well actually the initial plan was not to log to a file, even if an option to export is present, but this has more to do with my inexperience with putting GUIs in front of PowerShell than anything else.

As I understand it is not possible to do what I originally planned to do so I'll just use the log file appraoch and get rid of it once the application is closed.

Thanks again for your time and putting me on the right track.

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

Re: Passing data to child form control

Post by jvierra »

If you create the child by hand in the main form then you can write to the box and have it display when needed. Just create a default forma nd add the rtb to it and set it to fill the form. However it cannot be dynamic.

You could also use an IE object to log to and it can shown or hidden without any disruption.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Passing data to child form control

Post by jvierra »

Here is an example of what you can do with an MDI form:
Attachments
Demo-MDIForm.ps1
(6.72 KiB) Downloaded 273 times
User avatar
PsCustomObject
Posts: 137
Last visit: Thu Mar 28, 2024 3:34 am

Re: Passing data to child form control

Post by PsCustomObject »

Beg with my ignorance, may I ask you to elaborate a bit the IE part (I'm not a fan of acronyms and IE spawns Internet Explorer in my mind).

As per the child form I've created it with Add - New object and then picked up an empty form then added a button to the main form calling the load-form, is there another appraoch to this?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Passing data to child form control

Post by jvierra »

IE - Internet Explorer
$ie=New-Object -ComObject InternetExplorer.Application
$ie.Navigate('about:blank')
$ie.Visible=$true
$ie.Document.Write('<style type="text/css">.red{color:blue}</style>')
$ie.Document.Write('<p class="red">Hello World</p>')
User avatar
PsCustomObject
Posts: 137
Last visit: Thu Mar 28, 2024 3:34 am

Re: Passing data to child form control

Post by PsCustomObject »

Thanks for the demo form I did not see it.

I'll play around with it and see what I can come up with, and thanks for IE part as well :-)

I guess I should be set for the moment, I just need to play around with the demo and IE and see what better fits the tool

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

Re: Passing data to child form control

Post by jvierra »

Here is a full demo of a logging MDI Form that has one form that write to another. It's a PSF so it is easier to analyze and modify.
Attachments
Demo-MDIForm2.psf
(21.81 KiB) Downloaded 267 times
User avatar
PsCustomObject
Posts: 137
Last visit: Thu Mar 28, 2024 3:34 am

Re: Passing data to child form control

Post by PsCustomObject »

Thanks a lot for the demo, I ended up using a txt log file whichi serving the purpose nicely.

Still not sure if I will get rid of the file on form close but that's another story.

Many thanks for the example it will prove anyhow useful and stimulating to keep learning or when I have a doubt .
This topic is 8 years and 1 month 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