[英]readonly c# - No suitable Method found to override in a customized winforms textbox
我已经在winforms文本框中创建了拼写检查功能。 通过遵循以下接受的答案。
现在我的问题是在某些情况下我想使我的文本框为只读。 但是当我尝试以下操作时,似乎只读属性不起作用
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.