简体   繁体   中英

How to let a user change textbox text color

I'm creating a WinForms inventory application where the user can enter data into a series of textboxes for every new item they want to add or existing item they want to edit.

One of the textboxes is for Comments about the item, and I'd like to let the user select this text color to be whatever they'd like when they add it to the inventory. Is there a way to do this other than something like:

1) User clicks a button next to the textbox 2) Button displays a list of predetermined colors (say Red, Green, & Blue) 3) User can click one of those and then it goes back and changes the textbox.text color property

I wasn't sure if Visual Studio had some neat built-in colorwheel control that would return the selected color for me, or if the way I described is the simplest, most straightforward way to do it. (I'm assuming one thing I'll probably need is to use a Rich Text Box instead of a Text Box?)

I think you're looking for ColorDialog .

ColorDialog cd= new ColorDialog();
if (cd.ShowDialog() == DialogResult.OK)
    textBox1.ForeColor =  cd.Color;

My recommendation is - don't.

If you go with Rich Text, then it will become a real pain in the rear when you need to generate reports!!! Most report generators cannot process rich text.

It's probably best to skip over "features" like this comments color change and leave things at default. You might embellish by font size, or bold / underline / italics, or decorating around the text box, but leave the text generic. Eventually you'll run into a user who can't see the text because a selected Windows desktop color profile makes their custom color invisible -- and who gets blamed??? The developer.

Spot this kind of issue way before it happens, and you'll save yourself a lot of grief.

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