Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从包装类向RichTextBox发送事件? 出身背景_C#_.net_Winforms - Fatal编程技术网

C# 如何从包装类向RichTextBox发送事件? 出身背景

C# 如何从包装类向RichTextBox发送事件? 出身背景,c#,.net,winforms,C#,.net,Winforms,我正在使用无法直接显示Windows窗体元素的库构建一个C应用程序 当我想显示和编辑富文本时,我决定为RichTextBox构建一个包装类,这样它就可以处理所有繁重的重新布局和存储样式,而我的类只需处理在新库中显示它们 当前架构 目前,有3类与此问题有关: RichTextBox-.Net System.Windows.Forms.RichTextBox控件,应处理大部分逻辑,不能直接显示 Model-RichTextBox的一个子类,用于向StyledText公开句柄,以帮助它正确显示内容 P

我正在使用无法直接显示Windows窗体元素的库构建一个C应用程序

当我想显示和编辑富文本时,我决定为RichTextBox构建一个包装类,这样它就可以处理所有繁重的重新布局和存储样式,而我的类只需处理在新库中显示它们

当前架构 目前,有3类与此问题有关:

RichTextBox-.Net System.Windows.Forms.RichTextBox控件,应处理大部分逻辑,不能直接显示 Model-RichTextBox的一个子类,用于向StyledText公开句柄,以帮助它正确显示内容 PStyledText-设计用于显示RichTextBox中存储的数据,将模型作为字段。所有事件都必须经过PStyledText 问题 到目前为止,我已经使用包装器类处理了所有文本条目。这是一个笨拙的、越来越复杂的、明显重复的工作——RichTextBox类本身就是为了处理这种输入而构建的,并且会比我做得更好

因此,我需要某种方式将包装器类上激发的所有键盘事件发送到RichTextBox。我该怎么做

密码 模型 PStyledText
在GitHub上

向我们展示您拥有的相关代码有a和a。请在标记中指明您正在使用的UI工具包。@O.R.Mapper-System.Windows.form不确定挂起的是什么。您当然应该覆盖OnKeyDown和OnKeyPress方法,而不是隐藏它们。调用base.OnKeyXxx以触发事件。@HansPassant-我没有重写,因为库将自己的类PInputEventArgs传递给事件,所以RichTextBox没有合适的方法来重写。也就是说,我添加了base.OnKeyXxx方法,并删除了我自己处理输入的方式。请参阅更新的代码,它似乎仍然没有做任何事情。
/// <summary>
/// A utitlity class to look act as the Owner for the document
/// </summary>
public class Model : RichTextBox
{
    #region Fields
    /// <summary>
    /// The PStyledText this class is acting as a Owner for
    /// </summary>
    public PStyledText Subject { get; protected set; }


    #endregion Fields

    #region Constructors
    /// <summary>
    /// Create a Model to serve a PStyledText
    /// </summary>
    /// <param name="target">The PStyledText this is the Owner for</param>
    public Model(PStyledText target)
        : base()
    {
        Subject = target;
        CurIndex = 0;
    }
    #endregion

    #region Events

    /// <summary>
    /// Occurs when a key is depressed while the attatchedPStyledText has keyboardFocus
    /// </summary>
    /// <param name="e">The event describing the keydown</param>
    public void OnKeyDown(PInputEventArgs e)
    {
        base.OnKeyDown(new KeyEventArgs(e.KeyData));
    }

    public void OnKeyPress(PInputEventArgs e)
    {
        base.OnKeyPress(new KeyPressEventArgs(e.KeyChar));
    }

    public void OnKeyUp(PInputEventArgs e)
    {
        base.OnKeyUp(new KeyEventArgs(e.KeyData));
    }
    #endregion events


}
/// <summary>
/// A class to provide rich text with editing capabilities
/// </summary>
public class PStyledText : PNode
{
    #region Fields
    /// <summary>
    /// Model is an extention of RichTextBox, providing the logic for the class. 
    /// Because it has some major differences to Java's Document class, 
    /// many of the details of the implementation differ from the Java version
    /// </summary>
    public Model Document
    {
        get
        {
            return document;
        }
        protected set
        {
            document = value;
        }
    }

    private Model document;


    #endregion Fields

    #region Constructors
    /// <summary>
    /// Create an empty PStyledText object
    /// </summary>
    public PStyledText()
        : this("")
    { }


    /// <summary>
    /// Create a PStyledText object with set text
    /// </summary>
    /// <param name="text">The text to fill the object with</param>
    public PStyledText(String text)
    {
        Document = new Model(this);


        DefaultHandler = new TextEntryInputHandler(this);
        AddInputEventListener(DefaultHandler);

        AddInputEventListener(new PStyledTextSelectEventHandler());



    }
    #endregion Constructors

    #region Methods



    public override void OnClick(PInputEventArgs e)
    {
        base.OnClick(e);
        Document.OnClick(e);
        UpdateCarat();
    }
}

public class TextEntryInputHandler : PBasicInputEventHandler
{
    public PStyledText Owner { get; protected set; }

    public TextEntryInputHandler(PStyledText o)
    {
        Owner = o;
    }

    public override void OnKeyDown(object sender, PInputEventArgs e)
    {
        base.OnKeyDown(sender, e);
        Owner.Document.OnKeyDown(e);
        Owner.UpdateCarat();
    }

    public override void OnKeyPress(object sender, PInputEventArgs e)
    {
        base.OnKeyPress(sender, e);
        Owner.Document.OnKeyPress(e);
        Owner.UpdateCarat();
    }

    public override void OnKeyUp(object sender, PInputEventArgs e)
    {
        base.OnKeyUp(sender, e);
        Owner.Document.OnKeyUp(e);
        Owner.UpdateCarat();
    }

    #endregion

}