Click here to Skip to main content
15,897,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

1 solution

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:

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


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

C#
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".
 
Share this answer
 
v4
Comments
Adrian_Brown 25-Jun-13 9:06am    
The problem is i wanted to do it via a user message, but the other issue with this is I would have to gain access to the main form window to get access to the richtextbox. The way ive got it at the moment is the main form has a splitter, pane1 is the tree view which handles the Mouse Click event, pane 2 is the richeditbox. The only way i can see to get the Mouse Click handler in the treeview to do anything with the richtextbox is to get access to the main form first.
sjelen 25-Jun-13 9:43am    
The event handler code does not need to be in treeview class. You can put it in your richtextbox class and it'll have access it needs.
Then, in main form, right after both treeview and richtextbox are instanced you can wire up treeview event with handler from richtexbox class, something like:
MainForm.MyTreeView.Click += new System.EventHandler(MainForm.MyRichTextBox.MyClickHandler);
Ron Beyer 25-Jun-13 10:16am    
I think you are over complicating things, take a look at my updated answer.
Adrian_Brown 25-Jun-13 12:49pm    
Ahh ok, so your saying put the handler in the main form rather than the sub form. I see what your doing :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900