只读C#-在自定义的Winforms文本框中找不到要覆盖的合适方法

时间:2018-10-31 12:09:44

标签: c# wpf

我已经在winforms文本框中创建了拼写检查功能。通过遵循以下接受的答案。

Trying to use the C# SpellCheck class

现在我的问题是在某些情况下我想使我的文本框为只读。但是当我尝试以下

时,似乎readonly属性不起作用

spellbox.Readonly = true

我什至尝试将以下方法添加到类中,但是它显示错误为“ 找不到合适的方法来覆盖

    [DefaultValue(true)]
     public override bool ReadOnly 
{ get {box.Readonly;} set {box.Readonly = value;} }

代码:

    using System;
    using System.ComponentModel;
    using System.ComponentModel.Design.Serialization;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Forms.Integration;
    using System.Windows.Forms.Design;

    namespace Activity_Tracker_Coding
    {
        [Designer(typeof(ControlDesigner))]
        //[DesignerSerializer("System.Windows.Forms.Design.ControlCodeDomSerializer, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.Serialization.CodeDomSerializer, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        class SpellBox : ElementHost
        {
            public SpellBox()
            {
                box = new TextBox();
                base.Child = box;
                box.TextChanged += (s, e) => OnTextChanged(EventArgs.Empty);
                box.SpellCheck.IsEnabled = true;

                box.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
                this.Size = new System.Drawing.Size(100, 20);
            }
            public override string Text
            {
                get { return box.Text; }
                set { box.Text = value; }
            }

            [DefaultValue(false)]
            public bool Multiline
            {
                get { return box.AcceptsReturn; }
                set { box.AcceptsReturn = value; }
            }
            [DefaultValue(false)]
            public bool WordWrap
            {
                get { return box.TextWrapping != TextWrapping.NoWrap; }
                set { box.TextWrapping = value ? TextWrapping.Wrap : TextWrapping.NoWrap; }
            }
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
            public new System.Windows.UIElement Child
            {
                get { return base.Child; }
                set { /* Do nothing to solve a problem with the serializer !! */ }
            }
            private TextBox box;

        }
    }

2 个答案:

答案 0 :(得分:0)

使用WPF TextBox控件查看链接中的代码。在WPF中,只读属性为IsReadOnly

答案 1 :(得分:0)

我可以看到您是从ElementHost对象继承的,该对象不包含const buttonElement = document.getElementById("button"); buttonElement.classList.toggle("btn-toggled") 属性。

然后,您真正应该做的就是在System.FormatException: The string 'FALSE' is not a valid Boolean value. 类中创建自己的类,而不要重写。然后,该属性将访问public class MyExample { [XmlIgnore] public bool _booleanField { get; set; } [XmlElement("BooleanField")] public string BooleanFieldString { get => _booleanField.ToString().ToLower(); set => _booleanField = ConvertBooleanStringValue(value); } private bool ConvertBooleanStringValue(string booleanAsString) { switch (booleanAsString.ToUpper()) { case "TRUE": case "T": case "1": case "Y": case "YES": return true; case "FALSE": case "F": case "0": case "N": case "NO": return false; default: return false; } } } 只读属性。

ReadOnly