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.
User avatar
localpct
Posts: 271
Joined: Wed Sep 09, 2015 12:53 pm

Re: ColorDialog

Post by localpct » Thu Jul 12, 2018 9:26 pm

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

User avatar
jvierra
Posts: 12899
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: ColorDialog

Post by jvierra » Fri Jul 13, 2018 2:11 am

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: 271
Joined: Wed Sep 09, 2015 12:53 pm

Re: ColorDialog

Post by localpct » Mon Jul 16, 2018 5:48 pm

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.

User avatar
jvierra
Posts: 12899
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: ColorDialog

Post by jvierra » Mon Jul 16, 2018 5:59 pm

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.

Locked