简体   繁体   中英

Parse color dialog results into brush

How do I parse the result from a color dialog so that I can set the color of a brush to its value?

This is what I have and what I want to do.

 let b = Brushes.Black
 btnColor.Click.Add(fun _ ->
 ColorDialog.ShowDialog() (* Here I want to set the selected color to my brush b *) |> ignore )

I'll take a guess that you're talking about System.Windows.Forms here.

open System.Drawing
open System.Windows.Forms

let getColorFromUser initialColor =
    use dlg = new ColorDialog(Color = initialColor)
    if dlg.ShowDialog() = DialogResult.OK then
        dlg.Color
    else
        initialColor

// example with mutation
let mutable b = new SolidBrush(Color.Black)

b <- new SolidBrush(getColorFromUser(Color.Black))

See the Values docs for more info on mutable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM