C# textBox控件Enabled=false状态,设置字体显示为黑色

yanele 2012-02-18 04:18:58
我的程序是WindowsForm,在界面上有一个Panel,在Panel上又放了很多textBox控件与Lable控件等

当数据查询状态时,textBox控件Enabled=false,但字体变成灰色了,看不太清楚,设置字体颜色也不起作用;
this.textBox1.ForeColor = System.Drawing.SystemColors.WindowText;

如果设置this.textBox1.ReadOnly = true;虽然字体颜色是黑色了,但textBox1控件可以进入编辑状态,也不符合要求;
况且遍历Panel控件时,不能设置遍历过程中控件的ReadOnly属性.


不知道有没有办法解决:当this.textBox1.Enabled = false;时,字体颜色显示为黑色?
...全文
2785 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
simon545 2014-05-02
  • 打赏
  • 举报
回复
Enabled = false或True 时字体不一样
引用 18 楼 sofant 的回复:
[Quote=引用 17 楼 yanele 的回复:] 引用 13 楼 sofant 的回复: C# code public class MyTextBox : TextBox { public MyTextBox() { this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint |…… 我提到的需要实现了,但新的问题出现了,在控件上录入…… [/Quote] 只是示例,你自己变通一下

class MyTextBox:TextBox
    {
        protected override void OnEnabledChanged(EventArgs e)
        {
            base.OnEnabledChanged(e);

            this.SetStyle(ControlStyles.UserPaint |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.OptimizedDoubleBuffer, !this.Enabled);
            this.UpdateStyles();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (!this.Enabled)
                TextRenderer.DrawText(e.Graphics, this.Text, this.Font,new Point(-1,1), this.ForeColor);
            else
                base.OnPaint(e);
        }
    }
sofant 2012-02-20
  • 打赏
  • 举报
回复

public class MyTextBox : TextBox
{
public MyTextBox()
{
this.SetStyle(ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer, true);
this.UpdateStyles();
}

protected override void OnPaint(PaintEventArgs pevent)
{
TextRenderer.DrawText(pevent.Graphics, this.Text, this.Font, new Point(2, 2), this.ForeColor);
}
}
yanele 2012-02-20
  • 打赏
  • 举报
回复
试过了,还是有白边.
sofant 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 yanele 的回复:]
为什么我设置控件Enabled = false时,控件四边出现白边呢?
[/Quote]

我在win7下看到的边框和Enable=true时一样,不是白边。你可以重绘一下TextBox:
TextBoxRenderer.DrawTextBox(e.Graphics, this.Bounds, System.Windows.Forms.VisualStyles.TextBoxState.Readonly);


if (!this.Enabled)
{
TextBoxRenderer.DrawTextBox(e.Graphics, this.Bounds, System.Windows.Forms.VisualStyles.TextBoxState.Readonly);
TextRenderer.DrawText(e.Graphics, this.Text, this.Font, new Point(-1, 1), this.ForeColor);
}
else
base.OnPaint(e);
yanele 2012-02-20
  • 打赏
  • 举报
回复
为什么我设置控件Enabled = false时,控件四边出现白边呢?
sofant 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 yanele 的回复:]
引用 13 楼 sofant 的回复:
C# code

public class MyTextBox : TextBox
{
public MyTextBox()
{
this.SetStyle(ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |……

我提到的需要实现了,但新的问题出现了,在控件上录入……
[/Quote]

只是示例,你自己变通一下


class MyTextBox:TextBox
{
protected override void OnEnabledChanged(EventArgs e)
{
base.OnEnabledChanged(e);

this.SetStyle(ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer, !this.Enabled);
this.UpdateStyles();
}

protected override void OnPaint(PaintEventArgs e)
{
if (!this.Enabled)
TextRenderer.DrawText(e.Graphics, this.Text, this.Font,new Point(-1,1), this.ForeColor);
else
base.OnPaint(e);
}
}
yanele 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 sofant 的回复:]
C# code

public class MyTextBox : TextBox
{
public MyTextBox()
{
this.SetStyle(ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |……
[/Quote]
我提到的需要实现了,但新的问题出现了,在控件上录入数据时,控件背景色变了.退出录入时才返回到当前定义的颜色.
yanele 2012-02-20
  • 打赏
  • 举报
回复
如果设置this.textBox1.ReadOnly = true;虽然字体颜色是黑色了,但textBox1控件可以进入编辑状态,也不符合要求;况且遍历Panel控件时,不能设置遍历过程中控件的ReadOnly属性.
那一抹嫣红 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 liyajie865808403 的回复:]

引用 5 楼 xueye12gjj 的回复:

当数据查询状态时设置this.textBox1.ReadOnly = true;
当数据不查询状态时设置this.textBox1.ReadOnly = false;
行不


这样可以啊
[/Quote]

来拿分的
阆苑 2012-02-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 fredforstudy 的回复:]
LZ, 我遇到个问题, 不知道您是否可以帮忙解答下.我用的是 Textbox2.text = Textbox1.toString();
比如我在Textbox1中输入:TEST, 我希望在Textbox2中显示TEST
可是每次显示的值都是:System.Windows.Forms.TextBox, Text: TEST
我想去掉前边的"System.Windows.Forms.Text……
[/Quote]

Textbox2.text = Textbox1.toString,你是等于的textbox1这个控件呀,应该是textbox1.text
yanele 2012-02-20
  • 打赏
  • 举报
回复
看着代码,也改不来,郁闷!书到用时方恨少.
FredForStudy 2012-02-19
  • 打赏
  • 举报
回复
LZ, 我遇到个问题, 不知道您是否可以帮忙解答下.我用的是 Textbox2.text = Textbox1.toString();
比如我在Textbox1中输入:TEST, 我希望在Textbox2中显示TEST
可是每次显示的值都是:System.Windows.Forms.TextBox, Text: TEST
我想去掉前边的"System.Windows.Forms.TextBox, Text: ", 应该怎么操作?
yanele 2012-02-19
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 fredforstudy 的回复:]
LZ, 我遇到个问题, 不知道您是否可以帮忙解答下.我用的是 Textbox2.text = Textbox1.toString();
比如我在Textbox1中输入:TEST, 我希望在Textbox2中显示TEST
可是每次显示的值都是:System.Windows.Forms.TextBox, Text: TEST
我想去掉前边的"System.Windows.Forms.Text……
[/Quote]

this.TextBox2.Text=this.TextBox1.Text.Tirm();//这样就可以了

具体写在哪个事件,自己根据需要来处理.
yanele 2012-02-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xueye12gjj 的回复:]
当数据查询状态时设置this.textBox1.ReadOnly = true;
当数据不查询状态时设置this.textBox1.ReadOnly = false;
行不
[/Quote]

如果设置this.textBox1.ReadOnly = true;虽然字体颜色是黑色了,但textBox1控件可以进入编辑状态,也不符合要求;况且遍历Panel控件时,不能设置遍历过程中控件的ReadOnly属性.
q316085319 2012-02-19
  • 打赏
  • 举报
回复
来看看 !
zhouxingyu896 2012-02-19
  • 打赏
  • 举报
回复
关注
帮顶
liyajie865808403 2012-02-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 xueye12gjj 的回复:]

当数据查询状态时设置this.textBox1.ReadOnly = true;
当数据不查询状态时设置this.textBox1.ReadOnly = false;
行不
[/Quote]

这样可以啊
xueye12gjj 2012-02-18
  • 打赏
  • 举报
回复
当数据查询状态时设置this.textBox1.ReadOnly = true;
当数据不查询状态时设置this.textBox1.ReadOnly = false;
行不
ziyouli 2012-02-18
  • 打赏
  • 举报
回复
盖一层透明的panel上去
yanele 2012-02-18
  • 打赏
  • 举报
回复
控件有那么多,操作起来有点繁琐.
加载更多回复(2)

111,119

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧