473,385 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Treeview and ContextMenuStrip

Let's say I have a Treeview control on a form. Each leaf node in the
Treeview has a ContextMenuStrip, each with one ToolStripMenuItem, and
all ToolStripMenuItems Click event is handled by a comment event
handler.

How do I determine which Treeview Node signalled the menu event in the
common event handler?
Oct 17 '07 #1
7 4384
Joe,

When creating the ContextMenuStrip for the nodes in the treeview
(assuming you have a separate ContextMenuStrip for each node), I would
assign the Tag property to the node in the tree view. Then, in the event
handler, you can get the Tag property and know which node triggered the
menu.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe Cool" <jo*****@home.netwrote in message
news:31********************************@4ax.com...
Let's say I have a Treeview control on a form. Each leaf node in the
Treeview has a ContextMenuStrip, each with one ToolStripMenuItem, and
all ToolStripMenuItems Click event is handled by a comment event
handler.

How do I determine which Treeview Node signalled the menu event in the
common event handler?
Oct 18 '07 #2
Since you are already using a treeview maybe you can answer this
question for me. I am new to C# and am trying to populate a treeview
from a SQL 2005 table. Do you have s snippit of code that will do
that or know a URL where I can see how?

I would really appreciate the help.

Thanks
On Wed, 17 Oct 2007 23:37:35 GMT, Joe Cool <jo*****@home.netwrote:
>Let's say I have a Treeview control on a form. Each leaf node in the
Treeview has a ContextMenuStrip, each with one ToolStripMenuItem, and
all ToolStripMenuItems Click event is handled by a comment event
handler.

How do I determine which Treeview Node signalled the menu event in the
common event handler?
Oct 18 '07 #3
On Thu, 18 Oct 2007 00:42:26 GMT, xm******@yahoo.com wrote:
>Since you are already using a treeview maybe you can answer this
question for me. I am new to C# and am trying to populate a treeview
from a SQL 2005 table. Do you have s snippit of code that will do
that or know a URL where I can see how?
Do you know how to either a) read a SQL2005 table, or b) populate a
treeview?
>
I would really appreciate the help.

Thanks
On Wed, 17 Oct 2007 23:37:35 GMT, Joe Cool <jo*****@home.netwrote:
>>Let's say I have a Treeview control on a form. Each leaf node in the
Treeview has a ContextMenuStrip, each with one ToolStripMenuItem, and
all ToolStripMenuItems Click event is handled by a comment event
handler.

How do I determine which Treeview Node signalled the menu event in the
common event handler?
Oct 18 '07 #4
Joe Cool wrote:
Let's say I have a Treeview control on a form. Each leaf node in the
Treeview has a ContextMenuStrip, each with one ToolStripMenuItem, and
all ToolStripMenuItems Click event is handled by a comment event
handler.

How do I determine which Treeview Node signalled the menu event in the
common event handler?
The ContextMenuStrip will be passed as the "sender" argument to the
event handler, as in

Sub xyz_Click( sender as Object, e as EventArgs )

Dim cms as ContextMenuStrip _
= DirectCast( sender, ContextMenuStrip )

From that, you can get the control that caused the ContextMenuStrip to
be shown, which will either be the TreeView or the TreeNode (I can't
remember which), as in

either
Dim tn as TreeNode _
= cms.SourceControl
or
Dim tn as TreeNode _
= DirectCast(cms.SourceControl,TreeView).SelectedNod e

HTH,
Phill W.
Oct 18 '07 #5
I can connect to the DB, create the dataadapter, populate a data set.
And I know how to add nodes to a treeview. The problem is how to use
the dataset to populate a treeview.

I have not found a Windows Forms C# example that can handle an
unspecified level of node nesting and Parent = Child. See child rows
15.

I would really appreciate the help.

Thank you.

I have a SQL Server 2005 table containing this data shown below.
Child...Parent...Depth.....Hierarchy
1.........NULL.....0.........01
2..........1.......1..........01.02
5..........2.......2..........01.02.05
6..........2.......2..........01.02.06
3..........1.......1..........01.03
7..........3.......2..........01.03.07
11.........7.......3..........01.03.07.11
14.........11......4..........01.03.07.11.14
12.........7.......3..........01.03.07.12
13.........7.......3..........01.03.07.13
8..........3.......2..........01.03.08
9..........3.......2..........01.03.09
4..........1.......1..........01.04
10.........4.......2..........01.04.10
15.........NULL....0..........15
15.........15......1..........15.15
16.........15......1..........15.16
18.........16......2..........15.16.18
17.........15......1..........15.17

On Thu, 18 Oct 2007 00:59:59 GMT, Joe Cool <jo*****@home.netwrote:
>On Thu, 18 Oct 2007 00:42:26 GMT, xm******@yahoo.com wrote:
>>Since you are already using a treeview maybe you can answer this
question for me. I am new to C# and am trying to populate a treeview
from a SQL 2005 table. Do you have s snippit of code that will do
that or know a URL where I can see how?

Do you know how to either a) read a SQL2005 table, or b) populate a
treeview?
>>
I would really appreciate the help.

Thanks
On Wed, 17 Oct 2007 23:37:35 GMT, Joe Cool <jo*****@home.netwrote:
>>>Let's say I have a Treeview control on a form. Each leaf node in the
Treeview has a ContextMenuStrip, each with one ToolStripMenuItem, and
all ToolStripMenuItems Click event is handled by a comment event
handler.

How do I determine which Treeview Node signalled the menu event in the
common event handler?
Oct 22 '07 #6
I assume that the records you get back from the dataset have the child
records after their parent. If not, then the process becomes much
more complicated.

The brute force method is, for each record, loop through the treeview
nodes until you find the parent and add the new node.

If the Child field was unique, then you could use Child as the key for
each node. That way you could loop through the records in the
dataset, directly locate the parent using the Parent value as the key,
and add the new node. Yours are not unique, so unless you can modify
the data this technique will not work for you. I'm not sure exactly
what your data looks like, but maybe you can do this if you make the
key be a combination of Child and Depth if that combination is unique.
On Mon, 22 Oct 2007 14:24:54 GMT, xm******@yahoo.com wrote:
>I can connect to the DB, create the dataadapter, populate a data set.
And I know how to add nodes to a treeview. The problem is how to use
the dataset to populate a treeview.

I have not found a Windows Forms C# example that can handle an
unspecified level of node nesting and Parent = Child. See child rows
15.

I would really appreciate the help.

Thank you.

I have a SQL Server 2005 table containing this data shown below.
Child...Parent...Depth.....Hierarchy
1.........NULL.....0.........01
2..........1.......1..........01.02
5..........2.......2..........01.02.05
6..........2.......2..........01.02.06
3..........1.......1..........01.03
7..........3.......2..........01.03.07
11.........7.......3..........01.03.07.11
14.........11......4..........01.03.07.11.14
12.........7.......3..........01.03.07.12
13.........7.......3..........01.03.07.13
8..........3.......2..........01.03.08
9..........3.......2..........01.03.09
4..........1.......1..........01.04
10.........4.......2..........01.04.10
15.........NULL....0..........15
15.........15......1..........15.15
16.........15......1..........15.16
18.........16......2..........15.16.18
17.........15......1..........15.17

On Thu, 18 Oct 2007 00:59:59 GMT, Joe Cool <jo*****@home.netwrote:
>>On Thu, 18 Oct 2007 00:42:26 GMT, xm******@yahoo.com wrote:
>>>Since you are already using a treeview maybe you can answer this
question for me. I am new to C# and am trying to populate a treeview
from a SQL 2005 table. Do you have s snippit of code that will do
that or know a URL where I can see how?

Do you know how to either a) read a SQL2005 table, or b) populate a
treeview?
>>>
I would really appreciate the help.

Thanks
On Wed, 17 Oct 2007 23:37:35 GMT, Joe Cool <jo*****@home.netwrote:

Let's say I have a Treeview control on a form. Each leaf node in the
Treeview has a ContextMenuStrip, each with one ToolStripMenuItem, and
all ToolStripMenuItems Click event is handled by a comment event
handler.

How do I determine which Treeview Node signalled the menu event in the
common event handler?
Oct 22 '07 #7
can u send the sample code to create different contextmenu for different
levels in a treeview...
Oct 23 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Phill | last post by:
Here's what I'm doing to make the right context mnue show up based on what node was cliked: private void tvwMenu_MouseUp(object sender, MouseEventArgs e) { //Select Node When Right Clicked &...
4
by: humble.apprentice | last post by:
Hi everybody, I've got a TreeView and when ever I right-click on it, the ContextMenuStrip should comes up. The problem is the following, if the user didn't left-click an then right click the...
1
by: Ron M. Newman | last post by:
I have a context menu strip. I can Add elements to its "Items", but there is nothing in there to add a sub context menu strip. Menu Item 1 Item 2 Sub Item 1 <-- impossible to add! How do...
7
by: Dave | last post by:
How do I get my ContextMenuStrip to receive key down preview events? I have the event hooked but I don't see the key strokes in the event? Dave
1
by: =?Utf-8?B?QnJhZA==?= | last post by:
i have a menu system that is generated dynamically everything works good except for one minor astetic.... I click on a menu item that displays a ContextMenuStrip Popup Menu If an item on...
2
by: Kevin | last post by:
If someone could try this little test I would be most greatful: In a project, create a new class. Make this class inherit from TreeView Switch to designer mode and drop a ContextMenuStrip onto...
7
by: Joe Cool | last post by:
Let's say I have a Treeview control on a form. Each leaf node in the Treeview has a ContextMenuStrip, each with one ToolStripMenuItem, and all ToolStripMenuItems Click event is handled by a comment...
1
by: johnnypung | last post by:
I have a treeview opening a contextMenuStrip on right click. However the right click would not select the node that it right clicks on. How should I make it so the right click will select the node,...
9
by: =?Utf-8?B?Z2luYWNyZXNzZQ==?= | last post by:
Hi, I have a form (Form1) with a ContextMenuStrip (ContextMenuStrip1). If I pass Form1 to a Utility Sub with the following code: Public Sub Utility(theForm as Object) with theForm For Each...
2
by: eBob.com | last post by:
I've been working on an app which has an array of RichTextBoxes. And I have a context menu for the RTBs. The context menu has two levels; the first level has two items, "Load Sample Text File"...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.