简体   繁体   English

如何让用户更改文本框的文本颜色

[英]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. 我正在创建一个WinForms清单应用程序,用户可以在其中为要添加的每个新项目或要编辑的现有项目的一系列文本框中输入数据。

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 1)用户单击文本框旁边的按钮2)按钮显示预定颜色的列表(例如红色,绿色和蓝色)3)用户可以单击其中一种,然后返回并更改textbox.text color属性

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. 我不确定Visual Studio是否有一些简洁的内置色轮控件可以为我返回选定的颜色,或者我所描述的方式是否是最简单,最直接的方法。 (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

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!!! 如果您使用Rich Text,那么当您需要生成报告时,它将成为后方的真正痛苦!!! 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??? 最终,您会遇到一个看不见文本的用户,因为选定的Windows桌面颜色配置文件使它们的自定义颜色不可见-并因此而受到责备??? The developer. 开发人员。

Spot this kind of issue way before it happens, and you'll save yourself a lot of grief. 在这种问题发生之前就找出它,您会为自己省去很多麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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