简体   繁体   中英

C# Windows forms textbox greyed out

I'll try to be as specific as possible. I'm using visual basic 2010 c# express edition. I'm trying to create a textbox that is filled with information from the program. Suppose I put the text "Hello" in the textbox, when I run it, the form has a textbox saying Hello. Here, the user can select the text and copy it. Basically, when the mouse goes over the textbox, it changes appearance and the textbox is interactive. What I need to make is the textbox as not-interactive. In the textbox properties, there is an option called "Enabled". If I make it as False, all my requirements are satisfied. But the textbox is greyed out. Is there any way to get "Enabled" to false and still make the textbox look not greyed out. My query is regarding aesthetics.

You can make the textbox readonly:

Creating a Read-Only Text Box (Windows Forms)

To make the background gray, you probably need to change the background color:

txtFoo.BackColor = ...;

And if you do no want to make the text selectable, set Enabled = false;

You can create your own control that will look exactly like a TextBox but will be static. It's very easy to achieve that. Right-click on your project name in Solution Explorer and choose: Add > New Item... > Custom Control . You can name it somehow, eg DisabledTextBox .

Here's the full code of the new control.

public partial class DisabledTextBox : Control
{
    public DisabledTextBox()
    {
        InitializeComponent();
        DoubleBuffered = true; // To avoid flickering
    }

    protected override void OnPaint(PaintEventArgs pe)
    {
        pe.Graphics.Clear(SystemColors.Window); // White background
        pe.Graphics.DrawRectangle(SystemPens.ActiveBorder, new Rectangle(0, 0, Width - 1, Height - 1)); // Gray border
        pe.Graphics.DrawString(Text, Font, SystemBrushes.WindowText, 1, 3); // Our text
    }

    protected override void OnTextChanged(EventArgs e)
    {
        base.OnTextChanged(e);
        Invalidate(); // We want to repaint our control when text changes
    }

    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);
        Height = Font.Height + 7; // This limit the height of our control so it will beahave like a normal TextBox
    }
}

When you compile it, your new control will be available in Toolbox, so you can use like any other control. It will look exactly like TextBox.

在此处输入图片说明

textBox.BackColor = System.Drawing.SystemColors.Window;

将ReadOnly属性设置为True应该可以解决问题

I was looking for the easy solution to this question>

Here is what worked for me:

textBox Enabled property -- true

textBox ReadOnly property -- true

And below line of code to get rid of they greyed out area.

public Test_class()
{
InitializeComponent();
textBox.BackColor = System.Drawing.SystemColors.Window;
}

Yes, the user still can select the value in the text box but not entering a new value or edit old one.

Cheers!

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