ColorDialog

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 8 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
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: ColorDialog

Post by localpct »

Hiya!

This script works perfectly fine the way it’s written for one button.

I’ve tried quite a few times to get another button to change the foreground color of a label.

It appears I write over the settings file ( obviously ) but I cannot seem to find a way to append the two actions.

Two buttons
One changes background ( from earlier )
One changes a label
Can I use the same settings file or do I have to make a new one?
Do I need another colordialog?
I know $colors holds the info I need to write to the file
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ColorDialog

Post by jvierra »

You have to design the structure first and make it global to the form. If the form runs for the first time the file will not exist o you will have to detect that and create it or supply a default file. This is where we get into the arena of program design. You will have to design a file structure that allows you to reload the file. There are many approaches to this and it does require some serious thinking and learning the first time.

Code: Select all

$settingsFile = Join-Path $PWD 'settings.clixml'
$tbxform_Load={

    if(Test-Path $settingsFile){
        # load existing settings
    	$script:colors = Import-Clixml
    	$tbxForm.BackColor = [System.Drawing.Color]::FromArgb($colors.Form.BackColor)
    	$textboxComputer.BackColor = [System.Drawing.Color]::FromArgb($colors.textboxComputer.BackColor)
    }else{
        # create new settings file from current values
    	$colors = @{
    		tbxForm   = @{
    			BackColor  = $tbxForm.BackColor.ToArgb()
    		}
            textboxComputer = {
                BackColor = $textboxComputer.BackColor.ToArgb()
            }
    	}
    	$colors | Export-Clixml $settingsFile
    }
}
I use an XML file most of the time as I cn easily create a structure that can be easily automated based on the choice of file entries. A function can be designed that reads through the file and sets the colors and, when closing the form I can walk the file and assign the current changes. I also use a context menu item that is added when the context menu is opened if there is a setting with a saved item.

Another method is to capture the mouse and highlight all controls that can be customized then, on a click on the control pop up the color dialog and setting code. This is usually initiated by a main menu subitem on the "Tools" menu.

There are many ways to approach this that allow for simple coding. Experience with code design is necessary and this is one way to get that experience. The final design is driven by requirements for the functionality.
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: ColorDialog

Post by localpct »

Sorry for the delay, I just got back from vacation. This gives me a lot to chew on in learning. Thank you for the very informative response.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ColorDialog

Post by jvierra »

It would be best to digest the concepts and then design an approach that matches what you need. Your requirements may be more or less complex.
This topic is 5 years and 8 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