How to keep the default look of the TextBox changing the value of the ReadOnly property?

1

If the value of the ReadOnly property of a TextBox is false it means that I can change the content. However, when the value is true the content can no longer be changed, but when we change the value of ReadOnly to true the visual of TextBox and changed, see below in the image. A TextBox with your property ReadOnly set to true : p>

Otherwise,the%sof%visuallooksnormal,seebelowintheimage.

ATextBoxwithyourpropertyTextBoxsettoReadOnly(thisisthedefaultlook):

Here is the code that changes the value of the false property to play:

private void button1_Click(object sender, EventArgs e)
{
    if (textBoxExemplo.ReadOnly)
    {
        textBoxExemplo.ReadOnly = false;                
    }
    else
    {
        textBoxExemplo.ReadOnly = true;               
    }
}

Changing the look may be to indicate that the field may change, however, I'd like to keep the default look when I set the property ReadOnly to ReadOnly , how can I do this programmatically?

    
asked by anonymous 14.02.2016 / 02:19

3 answers

3

Following the content of the link quoted by @Felipe Assunção could cause TextBox to keep the default look when ReadOnly property was set to true .

By directly manipulating events ReadOnlyChanged and BackColorChanged of TextBox , you can prevent the background color of TextBox by activating ReadOnly from being changed and you can also add a change criterion of color, see the adaptation below.

1st solution (Events ReadOnlyChanged and BackColorChanged ) :

private void textBoxExemplo_ReadOnlyChanged(object sender, EventArgs e)
{
    suprimiMudancaBackColor = true;
    textBoxExemplo.BackColor = textBoxExemplo.ReadOnly ? Color.FromKnownColor(KnownColor.Control) : atualBackColor;
    suprimiMudancaBackColor = false;
}

private void textBoxExemplo_BackColorChanged(object sender, EventArgs e)
{
    if (suprimiMudancaBackColor) 
        return;

    atualBackColor = textBoxExemplo.BackColor;
}

It is necessary to set the variable Color atualBackColor; of type Color , which will temporarily store the default color of TextBox and the variable bool suprimiMudancaBackColor; responsible for suppressing the background color change of TexBox , both as global.

Explanation of results.

The result of this solution is that TextBox will keep the default color when the ReadOnly property is set to true , but when the user clicks TexBox with ReadOnly active color it will change through the FromKnownColor of the Color class by using a predefined color that is represented by the KnownColor , and when the user clicks out of TextBox (when he loses focus) will set the color to default color again or be white.

Note:

  

Perhaps this form will be more laborious when you have several    TextBox , but it is more efficient.

Source: Setting a read only Textbox default Backcolor

2nd solution (method mudarReadOnly ) :

A second solution I've worked on is creating a method to be used in the Loard event of the form, see below:

private void mudarReadOnly(TextBox textBox, bool readOnly) 
{
    Color backColorPadrao = textBox.BackColor;
    textBox.ReadOnly = readOnly;
    textBox.BackColor = backColorPadrao;
}

Explanation of results.

It gets two parameters, a TextBox and the option that will turn on disable the ReadOnly and before it changes ReadOnly it stores the default color and then resets the color of TextBox through color stored and will always keep the white color of TextBox .

Note:

  

This method can be improved, but the goal is to work with several    TextBox in a form.

    
15.02.2016 / 21:50
2

Just select your texBox and go to their properties and change the property BackColor to White . If you want to do this via code it can be added in Load of your Form :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.BackColor = Color.White;
        }
    }
}

This is a very simple way to get the same result as when your texBox has ReadOnly equal to false . But for the user it is better to actually have the tint in grayscale, as it is a visual form of it that identifies which fields can not be edited, as well as being a feature that is present in virtually every system. >     

14.02.2016 / 19:16
0

I program in VB6, and this does not happen when I change ReadOnly, try to change the "BackColor" of the text at runtime as Dener Oak mentioned, if this does not work, change the appearance of it from 3D to FLAT, which should be a win32 object flag!

    
14.02.2016 / 02:44