How to keep the default Textbox look changing the value of the Readonly property?

Asked

Viewed 1,099 times

1

If the value of the property ReadOnly of a TextBox for false means that I can change the content. However, when the value is true content can no longer be changed, however, when we change the value of ReadOnly for true the look of TextBox and changed, see below in the image.

A TextBox with your property ReadOnly defined for true:

Imagem mostrando o visual de um TextBox

Otherwise, the look of TextBox is normal, see below in the image.

A TextBox with your property ReadOnly defined for false (this is the standard look):

Imagem mostrando o visual de um TextBox

Here follows the code that changes the value of the property ReadOnly to be reproduced:

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

The visual change may be to indicate that the field may change, however I would like to keep the visual pattern when defining the property ReadOnly for true, how can I do this programmatically?

  • This works, if you can’t understand it later I’ll come back and translate http://stackoverflow.com/questions/18039901/setting-a-read-only-textbox-default-backcolor

  • @Felipeasunción adapted the example of the link you sent me. It worked, but the color is changed when the textbox takes focus through the mouse, but returns to normal when it loses focus.

3 answers

3


Following the content of the link cited by @Felipe Assunção TextBox keep the look standard when property ReadOnly was set to true.

Directly manipulating the events ReadOnlyChanged and BackColorChanged of TextBox, the background colour of the TextBox by activating the ReadOnly is changed and it is also possible to add a color change criterion, see the adaptation below.

1° 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 define the variable Color atualBackColor; type Color, which will temporarily store the standard color of the TextBox and the variable bool suprimiMudancaBackColor; responsible for removing the change in the background colour of the TexBox, both as global.

Explanation of the outcome.

The result of this solution is that the TextBox will keep the color standard when property ReadOnly is set for true, however, when the user clicks on the TexBox with the ReadOnly active his color will change through the method Fromknowncolor class Color using a preset color that is represented by the element Knowncolor, and when the user clicks off the TextBox (when he loses focus) will set the color to default color again or be white.

Note:

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

Source: Setting a read only Textbox default Backcolor

2° solution (method mudarReadOnly):

A second solution I worked out was to create a method to be used in the event Loard form, see below:

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

Explanation of the outcome.

It takes two parameters, one TextBox and the option that will turn on to disable the ReadOnly and before it makes the change of ReadOnly it stores the default color and then resets the color of the TextBox through the stored color and will always keep the white color of the TextBox.

Note:

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

2

Just select your own texBox and go on their properties and change the property BackColor for White. If you want to do this via code you can be added to 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;
        }
    }
}

For this is a very simple way to get the same result as when your texBox is with the ReadOnly equal to false. But for the user it is better that he really gets the coloring in grayscale, because it is a visual way of identifying that fields can not be edited, as it is also a feature that be present in virtually all systems.

  • I have already solved here, thanks for the reply, I will post a reply from me to help other people.

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 quoted by Dener Carvalho, if this does not work, change the appearance of it from 3D to FLAT, which must be a flag of Win32 objects!

  • 1

    It works if I change the Backcolor, textBoxExemplo.BackColor = Color.White;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.