【问题标题】:Textbox readonly property not working in TextBox made by me文本框只读属性在我制作的文本框中不起作用
【发布时间】:2021-08-06 16:07:15
【问题描述】:

我有一个我自己制作的文本框 (TextBoxMoeda),但是,即使文本框属性 readonly 设置为 true。什么都没发生。任何人都知道如何实现代码来解决这个问题?请帮帮我?

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 .......
    }

【问题讨论】:

标签: c#


【解决方案1】:

WPF

您需要IsReadOnly 属性,而不是ReadOnly 属性。

例如:

  • 创建一个名为DeleteMe 的新WPF App (.NET Framework) 项目
  • MainWindow.xaml 中,删除Grid
  • MainWindow.xaml.cs 中,使用此代码:
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;
                }
            }            
        }
    }
}

文本框将是只读的(即我无法在文本框中输入任何内容)。

WinForms

代码似乎已经在 WinForms 中运行。这些步骤将产生一个工作项目:

  • 创建一个名为DeleteMeAgain 的新Windows Forms App 项目
  • 用这个覆盖所有Form1.cs 代码:
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();
                }
            }
        }
    }
}

文本框将是只读的(即我无法在文本框中输入任何内容)。

Windows 窗体:用户控制

这样做:

  • 创建一个名为DeleteMe3 的新Windows Forms App
  • Target Framework 设置为.NET Core 3.1 (Long-term support)
  • 在您的项目中,创建一个文件名为TextBoxStack.cs 的新用户控件
  • 用这个覆盖该用户控件的所有代码:
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();
            }
        }
    }
}
  • 将出现错误。转到错误源并删除该行。
  • 构建解决方案
  • 将您的新用户控件添加到Form1
  • 选择表单上的新用户控件
  • SomenteLeitura 属性更改为True
  • 运行项目。文本框应为灰色且只读。

【讨论】:

  • 我正在使用 Windows 窗体,但出现此错误:TextBox 没有 IsReadOnly 的定义。
  • 这很奇怪 - 我认为它是 WPF,因为我在 WinForms 中尝试了代码并且它运行良好。我将编辑我的答案。
  • 好的。谢谢你的帮助。
  • 我已经添加了。希望您能看到您的项目和我上面的代码之间的区别。如果没有,您是否可以编辑原始问题以包含创建文本框的代码以及将属性设置为只读的代码。
  • 我已将其作为用户控件,将其添加到表单中,将 SomenteLeitura 属性设置为 True... 完美运行。文本框现在是灰色的并且是只读的。我会在解决方案中添加一些东西。
猜你喜欢
  • 2021-08-06
  • 2011-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-03
相关资源
最近更新 更多