473,569 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TreeView multiple selections

Hi,

Is there a way for TreeView to have multiple selections? But I am not
talking about its checked boxes.

I want a way similar to ListView with MultiSelect = True. So I can use
[Ctrl] or [Shift] key and click to make multiple selections. Then when I
simply click one item, all previous selections are gone.

Thanks in advance.

Li

Oct 1 '07 #1
18 15346
Hi Li,

Yes, the .Net TreeView control does not support multiple selection by
default. We normally enable checkbox for all the TreeNodes, so that we may
use CheckBox to indicate the multiple selection. However, if you really
wanted to get the true multiple selection function in TreeView, you have to
customize TreeView control. The articles below provide the extended version
of TreeView with multiple selection function:
"C# TreeView with multiple selection"
http://www.codeproject.com/cs/miscctrl/treeviewms.asp
"Multi-Select TreeView"
http://windowsclient.net/downloads/f...entry1273.aspx
"Multi-Select TreeView Control in C#"
http://www.codeproject.com/cs/miscctrl/mwcontrols.asp

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 2 '07 #2
Hi Li,

Have you reviewed my reply to you? Does it make sense to you? If you still
need any help, please feel free to feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 5 '07 #3
Hi, Jeffrey:

Thanks a lot for the help!
I have read your response but haven't got a chance to read the articles and
try them out. I will do that after I finish my current work.

Thanks again.

Li

""Jeffrey Tan[MSFT]"" wrote:
Hi Li,

Have you reviewed my reply to you? Does it make sense to you? If you still
need any help, please feel free to feedback, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 5 '07 #4
Hi Li,

Thank you for the confirmation.

Ok, if you need any further help, please feel free to post, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 8 '07 #5
Hi, Jeffrey:

Now I am testing the first article: "C# TreeView with multiple selection".
The codes look ok. But I encounter a problem from TreeView.

After I click to select a node, then I click it again, it will not trigger
OnBeforeSelect or OnAfterSelect.
But if I click other nodes, i.e., not the last selected node, both of them
will be triggered.

Is TreeView designed by this way? If so, how to workaround to catch
selecting twice the node while pressing CTRL to un-select it?

By the way, I am using VB.net, not C#. But I think here is a raising event
issue.

Thanks in advance.

Li

""Jeffrey Tan[MSFT]"" wrote:
Hi Li,

Thank you for the confirmation.

Ok, if you need any further help, please feel free to post, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 12 '07 #6
Hi Li,

Thanks for your feedback.

Yes, this behavior also occurs in build-in TreeView control and is by
design. To workaround this behavior, you may handle the TreeView.Click
event. In this event, you may use check the mouse position to determine
which node is clicked. The code below demonstrates this logic:

Private Sub TreeView1_Click (ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles TreeView1.Click
Dim pt As Point = Me.TreeView1.Po intToClient(New
Point(Control.M ousePosition.X, Control.MousePo sition.Y))
Dim tn As TreeNode = Me.TreeView1.Ge tNodeAt(pt)
If Not (tn Is Nothing) Then
'Perform your logic based on the clicked node
End If
End Sub

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 15 '07 #7
Thanks!

""Jeffrey Tan[MSFT]"" wrote:
Hi Li,

Thanks for your feedback.

Yes, this behavior also occurs in build-in TreeView control and is by
design. To workaround this behavior, you may handle the TreeView.Click
event. In this event, you may use check the mouse position to determine
which node is clicked. The code below demonstrates this logic:

Private Sub TreeView1_Click (ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles TreeView1.Click
Dim pt As Point = Me.TreeView1.Po intToClient(New
Point(Control.M ousePosition.X, Control.MousePo sition.Y))
Dim tn As TreeNode = Me.TreeView1.Ge tNodeAt(pt)
If Not (tn Is Nothing) Then
'Perform your logic based on the clicked node
End If
End Sub

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 15 '07 #8
Hi, Jeffrey:

Look like for TreeView, if I click a node (selected), I can't set its
BackColor or ForeColor in codes. It is always painted as Highlighted.

Is it by designed? If so, how can I re-paint it to normal when selecting
twice with [CTRL]?

I suspect Microsoft has made changes to TreeView so 2005 version is
different from older version (2002) the article's codes run on.

Thanks.

Li

""Jeffrey Tan[MSFT]"" wrote:
Hi Li,

Thanks for your feedback.

Yes, this behavior also occurs in build-in TreeView control and is by
design. To workaround this behavior, you may handle the TreeView.Click
event. In this event, you may use check the mouse position to determine
which node is clicked. The code below demonstrates this logic:

Private Sub TreeView1_Click (ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles TreeView1.Click
Dim pt As Point = Me.TreeView1.Po intToClient(New
Point(Control.M ousePosition.X, Control.MousePo sition.Y))
Dim tn As TreeNode = Me.TreeView1.Ge tNodeAt(pt)
If Not (tn Is Nothing) Then
'Perform your logic based on the clicked node
End If
End Sub

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 16 '07 #9
Hi Li,

Thanks for your feedback.

Yes, .Net TreeView control encapsulates Win32 native tree view control. All
these behaviors are by design by the win32 tree view control.

To customize the hightlighted node colors, we may use custom-draw to get it
done. Fortunately, .Net2.0 has added the build-in support for custom-draw.

To use custom draw for your task, you may set TreeView.DrawMo de to
OwnerDrawText and handle TreeView.DrawNo de event. Then, in this event, you
may check the status of each node and paints its text and background
rectangle based on your requirement. The code snippet demonstrates this
logic:

Private tagFont As New Font("Helvetica ", 8, FontStyle.Bold)

Private Sub TreeView1_DrawN ode(ByVal sender As System.Object, ByVal e
As System.Windows. Forms.DrawTreeN odeEventArgs) Handles TreeView1.DrawN ode
' Draw the background and node text for a selected node.
If (e.State And TreeNodeStates. Selected) <0 Then

' Draw the background of the selected node. The NodeBounds
' method makes the highlight rectangle large enough to
' include the text of a node tag, if one is present.
e.Graphics.Fill Rectangle(Brush es.Green, NodeBounds(e.No de))

' Retrieve the node font. If the node font has not been set,
' use the TreeView font.
Dim nodeFont As Font = e.Node.NodeFont
If nodeFont Is Nothing Then
nodeFont = CType(sender, TreeView).Font
End If

' Draw the node text.
e.Graphics.Draw String(e.Node.T ext, nodeFont, Brushes.White, _
e.Bounds.Left - 2, e.Bounds.Top)

' Use the default background and node text.
Else
e.DrawDefault = True
End If

' If a node tag is present, draw its string representation
' to the right of the label text.
If (e.Node.Tag IsNot Nothing) Then
e.Graphics.Draw String(e.Node.T ag.ToString(), tagFont, _
Brushes.Yellow, e.Bounds.Right + 2, e.Bounds.Top)
End If

' If the node has focus, draw the focus rectangle large, making
' it large enough to include the text of the node tag, if present.
If (e.State And TreeNodeStates. Focused) <0 Then
Dim focusPen As New Pen(Color.Black )
Try
focusPen.DashSt yle = System.Drawing. Drawing2D.DashS tyle.Dot
Dim focusBounds As Rectangle = NodeBounds(e.No de)
focusBounds.Siz e = New Size(focusBound s.Width - 1, _
focusBounds.Hei ght - 1)
e.Graphics.Draw Rectangle(focus Pen, focusBounds)
Finally
focusPen.Dispos e()
End Try
End If
End Sub

Private Function NodeBounds(ByVa l node As TreeNode) As Rectangle

' Set the return value to the normal node bounds.
Dim bounds As Rectangle = node.Bounds
If (node.Tag IsNot Nothing) Then

' Retrieve a Graphics object from the TreeView handle
' and use it to calculate the display width of the tag.
Dim g As Graphics = Me.TreeView1.Cr eateGraphics()
Dim tagWidth As Integer = CInt(g.MeasureS tring( _
node.Tag.ToStri ng(), tagFont).Width) + 6

' Adjust the node bounds using the calculated value.
bounds.Offset(t agWidth \ 2, 0)
bounds = Rectangle.Infla te(bounds, tagWidth \ 2, 0)
g.Dispose()
End If
Return bounds
End Function 'NodeBounds

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 17 '07 #10

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

Similar topics

1
3553
by: theoryboy | last post by:
I'm trying to implement multiple selection functionality in a Tix Hlist using a control-click. I've bound control-click to a function that uses selection_set to add to the selection, but it doesn't seem to work. Only the last clicked item appears selected (highlighted) in the display and the return from info_selection only ever contains the...
1
3063
by: jeffgeorge | last post by:
Trying to create multiple acct reports based on the selection in a list box. I've set the list box for multiple selections, and in the report data source, I have a SQL statement which is reading the list box(when there is a single selection) but no luck with multiple selections. I'm suspecting I need to write a bit of code using an array. ...
2
2093
by: Craig B. | last post by:
I am relativly new to access 2000 and am having some trouble with a report. I am not sure what I want to do is something I can do in access. I want to be able to choose from a combo box multiple selections in a form and from those selections present them in a report. For example select a selection A,B, and C out of 9 possible choices. ...
1
1309
by: Andrew | last post by:
Hi, friends, Is there a way to make a TreeView allow multiple selections, like in a List control? Thanks.
2
1592
by: Steffen Loringer | last post by:
Hi all, may be an easy question: How can I allow multiple selections in a dropdownlist? Thanks Steffen
1
1971
by: Yvonne | last post by:
We have a Contacts database which categorises our Contacts by three categories eg Country, Language and Skills. It has a combo box ( not bound) which is populated from a union query that gets its values from the 3 category fields. At present the after-update event on the combo box generates a report which selects those contacts matching the...
4
2857
by: tbayse | last post by:
Hello, I have a question about making multiple selection queries in Access. I am running windows XP and Access 2003. Up until this point I had a form where a user would make single selections from a combo box and the following combo box would filter based on the initial selection. I now would like to give the opportunity to make multiple...
0
1386
by: Gunnar Hurtig | last post by:
Hi All I am relatively new to Tkinter and am putting a wraparound to the ATNF ASAP program. In one part I present several long lists in list boxes for selection. My code will remember the multiple selections made but I woul also like to highlight and maintain the highlighted items as I make additional selections. I can maintain the highlighted...
0
3154
by: Germaris | last post by:
Hi there! Is it possible to make multiple selections in a ComboBox ? i.e. make n consecutive selections and store them in an array or make n selections in the open list of the CB by using (for example) the click-Shift Key keystroke Purpose is to allow the user to select multiple different issues of a magazine with a maximum ease of use......
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.