简体   繁体   English

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

[英]readonly c# - No suitable Method found to override in a customized winforms textbox

I have created a spellcheck functionality in the winforms text-box. 我已经在winforms文本框中创建了拼写检查功能。 By following the below accepted answer. 通过遵循以下接受的答案。

Trying to use the C# SpellCheck class 尝试使用C#SpellCheck类

Now my problem is i want to make my textbox to be readonly in some cases. 现在我的问题是在某些情况下我想使我的文本框为只读。 But it seems to be the readonly property is not working when i try the following 但是当我尝试以下操作时,似乎只读属性不起作用

spellbox.Readonly = true

I even tried to add the following method to the class but it shows error as " No suitable method found to override " 我什至尝试将以下方法添加到类中,但是它显示错误为“ 找不到合适的方法来覆盖

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

Code: 码:

    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;

        }
    }

Looking at the code in the link it's using the WPF TextBox control. 查看链接中的代码,它使用WPF TextBox控件。 The property for readonly in WPF is IsReadOnly . WPF中readonly的属性是IsReadOnly

I can see that you're inheriting from ElementHost object, which doesn't contain a ReadOnly property. 我可以看到您是从ElementHost对象继承的,该对象不包含ReadOnly属性。

What you should actually do then is just create your own, inside your SpellBox class, without overriding. 然后,您真正应该做的就是在SpellBox类中创建自己的SpellBox ,而不要覆盖。 This property will then access the TextBox read only property. 然后,此属性将访问TextBox只读属性。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM