簡體   English   中英

只讀C#-在自定義的Winforms文本框中找不到要覆蓋的合適方法

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

我已經在winforms文本框中創建了拼寫檢查功能。 通過遵循以下接受的答案。

嘗試使用C#SpellCheck類

現在我的問題是在某些情況下我想使我的文本框為只讀。 但是當我嘗試以下操作時,似乎只讀屬性不起作用

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;

        }
    }

查看鏈接中的代碼,它使用WPF TextBox控件。 WPF中readonly的屬性是IsReadOnly

我可以看到您是從ElementHost對象繼承的,該對象不包含ReadOnly屬性。

然后,您真正應該做的就是在SpellBox類中創建自己的SpellBox ,而不要覆蓋。 然后,此屬性將訪問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