Another refresh question

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 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
SvanGool
Posts: 37
Last visit: Mon Nov 27, 2023 2:02 am

Another refresh question

Post by SvanGool »

Hello,

I've been searching the form for a solution to my problem but I haven't found it. Maybe there is no solution.
I've got a user form with some image lists on it, based on groupmembership.
If a user is not a member of the group a grayed out image is shown on the button. When you click the button, a popup is shown to add the user to the group, after that the popup is closed and you return to the main form.

Is it possible to "refresh" the button with a new image? So the groupmembership needs to be checked again.

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

Re: Another refresh question

Post by jvierra »

You will have to code the change when you update the list. I can think of no way for a button to automatically know about the correct image.
User avatar
SvanGool
Posts: 37
Last visit: Mon Nov 27, 2023 2:02 am

Re: Another refresh question

Post by SvanGool »

In my test I've mannaged to change the image on the button. (by making a second button with the change action).
  1. $buttonRemoteToegang.Image = $imagelistDnstRemoteToegang.Images[0]
Is it possible to change the image on the mainform by clicking a button on the childform?
How do I call the $buttonRemoteToegang.image on the mainform?

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

Re: Another refresh question

Post by jvierra »

You can change the image on a button at any time by just setting the "image" property to a different image. No need for a new button.

You can change a parent form by address the parent property of the child form. Changes will not be visible until the event exits.

$childform.Parent.Controls['mycontrol'].Image = $newimage
User avatar
SvanGool
Posts: 37
Last visit: Mon Nov 27, 2023 2:02 am

Re: Another refresh question

Post by SvanGool »

I don't get it, would you be so kind to have al look at my test project?

So after you click the enable button on the childform it should change the image on the button of the mainform.

Thanks!
Attachments
ImageList.zip
(63.83 KiB) Downloaded 128 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Another refresh question

Post by jvierra »

What you are trying to do won't work. This is not how Windows forms is designed to work. With Sapien forms the variables are in a different scope and unavailable.

Your design is also suspect. Why would you think you need to do this? A modal dialog is intended as an independent and isolated object. It should operate on data. Show_ChildForm returns a dialog result. The result can be used to change the state of the calling form. Data from the calling form can be passed in a "Param" statement.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Another refresh question

Post by jvierra »

The following will switch images:
  1. # make mainform work like this
  2. $formMainForm_Load={
  3. }
  4.  
  5. $buttonOpenPopup_Click={
  6.     Show-ChildForm_psf ([ref]$buttonOpenPopup)
  7. }
  8.  
  9. #make childfrom work like this
  10. param(
  11.     [ref]$buttonOpenPopup
  12. )
  13. $formChildForm_Load={
  14. }
  15.  
  16. $buttonEnable_Click={
  17.     $buttonOpenPopup.Value.ImageIndex = 1
  18.     $formChildForm.Close()
  19. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Another refresh question

Post by jvierra »

I should also note that, in the case of this control, the image is updated immediately. Some operations may not happen immediately but images bound to an ImageList will update when changed by a child.
User avatar
SvanGool
Posts: 37
Last visit: Mon Nov 27, 2023 2:02 am

Re: Another refresh question

Post by SvanGool »

This is just a test. In my real project I have a button with an image on it. When a user is a member of the group "Remote Access" the image is "on" if the searched user is not a member (the image is gray) the admin can click the button and a popup is shown where the admin can click a button to add the user to the group, after that the popup is closed.

But when you return to the mainform the image is still gray, the admin has to close and reopen the form to get the right image.

That's why I wanted this option, so that the admin could see that the change was made.

The last solution works for me, thanks for you help! Again ;)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Another refresh question

Post by jvierra »

In you case the child form can access the variables of the calling function.

This will wok in the child:

Code: Select all

$buttonEnable_Click={
	$buttonOpenPopup.ImageIndex = 1
	$formChildForm.Close()
}
Since the child function I s called in the scope of the parent form then all objects that are defined can be changed. This would not be true of "ValueType" variables which must be passed as "[ref}" types.

Part of your issue is likely because you are using an ImageList and, once bound, changing the image is only possible via the image index or ImageKey.
This topic is 5 years 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