在Windows应用程序中传递消息 [英] Passing messages within an Windows Application

查看:85
本文介绍了在Windows应用程序中传递消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的应用程序,我有一个分割窗口,一边是树视图,另一边是richeditbox。当我双击树视图上的条目时,richeditbox必须加载文件。 (我已经为treeview和richeditbox派生了我自己的类。)



在C ++中我会使用SendMessage传递用户消息,这样可以让richedit接收并且处理消息,我还可以在其他地方处理它,如果我需要的话。



我在c#中看不到这样做的好方法,现在有更好的方法吗?



非常感谢

解决方案

您可以向 NodeMouseClicked [ ^ ]事件(通过选择事件选项卡(带有闪电的选项卡)转到树视图的事件中在属性窗口中,找到你想要的事件,然后在空白处双击以添加处理程序。)



然后,在该处理程序中你可以使用Stream对象,如 StreamReader [ ^ ]读取文件内容并使用.Text属性或.AppendText()方法将其放在RichTextBox中。



如果您使用的是Windows窗体(不是WPF),请这是一个关于将文件加载到RichTextBox的好教程 [ ^ ]





在主窗体构造函数中,添加:



< pre lang =c#> treeView1.NodeMouseClicked + = new TreeNodeMouseClickEventHandler(treeView1_NodeMouseClick( object sender,TreeNodeMouseClickEventArgs e );





然后,在.cs文件格式中,添加以下功能:



  private   void  treeView1_NodeMouseClick( object  sender,TreeNodeMouseClickEventArgs e)
{
string fileName = e.Node.Text;
richTextbox1.LoadFile(fileName,RichTextBoxStreamType.RichText);
}





没有必要尝试传递像c / c ++那样的消息和.NET RichTextBox不支持通过这样的消息加载文件。上面的代码很简单,效果很好,不要过于复杂,因为这就是旧方式。


Im writing a simple app, i have a splitter window with a tree view one side and a richeditbox the other. When i double click an entry on the tree view, the richeditbox has to load a file. (I have derived my own classes for the treeview and richeditbox).

In C++ I would use SendMessage to pass a user message, this allows the richedit to pick up and handle the message, i could also handle it in various other places if i needed.

I cant see a nice way of doing this within c#, is there a better way to do this now?

Many Thanks

解决方案

You can add an event handler to the NodeMouseClicked[^] event in the tree view (go to the events of the tree view by selecting the events tab (the one with the lightning bolt) in the property window, find the event you want, then double click in the white space to add the handler).

Then, in that handler you can use the Stream objects, like StreamReader[^] to read in the contents of the file and place it in the RichTextBox by using the .Text property or .AppendText() method.

If you are using Windows Forms (not WPF) here is a good tutorial on loading files into a RichTextBox[^]

[Edit]
In the main form Constructor, add:

treeView1.NodeMouseClicked += new TreeNodeMouseClickEventHandler(treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e);



Then, in the form .cs file, add the following function:

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    string fileName = e.Node.Text;
    richTextbox1.LoadFile(fileName, RichTextBoxStreamType.RichText);
}



There is no need to try to pass "messages" like c/c++ did, and the .NET RichTextBox doesn't support loading files through messages like that. The above code is simple and works very well, don't over-complicate things because thats how they were done "the old way".


这篇关于在Windows应用程序中传递消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆