0

enter image description hereI have a textbox (TextBoxMoeda) made by me, but, even when the textbox property readonly is set true. nothing happens. Anyone knows how to implement the code to resolve this problem?. Please help me?

public class TextBoxMoeda : TextBox
{
    private double dp;
    private string fmt = string.Empty;
    private int _CasasDecimais = 0;

    [Category("TextBoxMoeda")]
    public virtual bool SomenteLeitura
    {
        get => base.ReadOnly;
        set
        {
            base.ReadOnly = value;
            Invalidate();
        }
    }
    //Code Continues .......
    }

enter image description here

  • Does this answer your question? [Make TextBox uneditable](https://stackoverflow.com/questions/14598024/make-textbox-uneditable) –  Aug 06 '21 at 16:41

1 Answers1

1

WPF

You want the IsReadOnly property, not the ReadOnly property.

For example:

  • Create a new WPF App (.NET Framework) project called DeleteMe
  • In MainWindow.xaml, delete the Grid
  • In MainWindow.xaml.cs, use this code:
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

namespace DeleteMe
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var tm = new TextBoxMoeda();
            this.AddChild(tm);
            tm.SomenteLeitura = true;
        }

        public class TextBoxMoeda : TextBox
        {
            [Category("TextBoxMoeda")]
            public virtual bool SomenteLeitura
            {
                get => base.IsReadOnly;
                set
                {
                    base.IsReadOnly = value;
                }
            }            
        }
    }
}

The textbox will be readonly (i.e. I can't type anything into the textbox).

WinForms

The code already appears to work in WinForms. These steps will produce a working project:

  • Create a new Windows Forms App project called DeleteMeAgain
  • Overwrite all of the Form1.cs code with this:
using System.ComponentModel;
using System.Windows.Forms;

namespace DeleteMeAgain
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            var tm = new TextBoxMoeda();
            this.Controls.Add(tm);
            tm.SomenteLeitura = true;
        }

        public class TextBoxMoeda : TextBox
        {
            [Category("TextBoxMoeda")]
            public virtual bool SomenteLeitura
            {
                get => base.ReadOnly;
                set
                {
                    base.ReadOnly = value;
                    Invalidate();
                }
            }
        }
    }
}

The textbox will be readonly (i.e. I can't type anything into the textbox).

Windows Form: User Control

Do this:

  • Create a new Windows Forms App called DeleteMe3
  • Set Target Framework to .NET Core 3.1 (Long-term support)
  • In your project, create a new UserControl with the filename TextBoxStack.cs
  • Overwrite all of the code for that user control with this:
using System.ComponentModel;
using System.Windows.Forms;

namespace DeleteMe3
{
    public partial class TextBoxStack : TextBox
    {
        [Category("TextBoxStack")]
        public virtual bool SomenteLeitura
        {
            get => base.ReadOnly;
            set
            {
                base.ReadOnly = value;
                Invalidate();
            }
        }
    }
}
  • An error will appear. Go to the source of the error and delete that line.
  • Build the solution
  • Add your new user control to Form1
  • Select the new user control on the form
  • Change the SomenteLeitura property to True
  • Run the project. The textbox should be grey and readonly.
Robson
  • 2,008
  • 2
  • 7
  • 26
  • 1
    I'm using Windows Form and I'm getting this error: TextBox doesn't have a definition to IsReadOnly. – Oto Emerson Aug 06 '21 at 16:26
  • That's odd - I assume it was WPF, because I tried the code in WinForms and it just worked perfectly. I'll edit my answer. – Robson Aug 06 '21 at 16:31
  • I've added that. Hopefully you can see a difference between your project and my code above. If not, can you edit your original question to include the code where you create the textbox and the code where you set the property to readonly. – Robson Aug 06 '21 at 16:37
  • TextBoxCoin is a user control. I did this, drag it from a toolbox and put it on a form. And ....... Nothing happened. TextBox changes color to gray when I set the readonly option to true, but I can still write to it. – Oto Emerson Aug 06 '21 at 16:53
  • 1
    I've made it as a user control, added it to the form, set the SomenteLeitura property to True... works perfectly. The textbox is now grey and readonly. I'll add some stuff to the solution. – Robson Aug 06 '21 at 17:05
  • I've added steps to produce a fully working solution. Please follow those steps very carefully and see if it works. If it works: compare the working solution to your solution - what is different? – Robson Aug 06 '21 at 17:20
  • It worked. But I stiil don't know what happened with my class. Thank you for your effort. – Oto Emerson Aug 06 '21 at 17:38
  • That's excellent. I would suggest you do a comparison between the working project and your original project, to spot any differences. For each difference, change it and see if that makes it work. It may be worth just copying the working user control over to your project and then incrementally adding your other code into that, making sure that it works with each addition. – Robson Aug 06 '21 at 17:41
  • Ok. Thank you for every thing. – Oto Emerson Aug 06 '21 at 17:43