替换我的IF语句? [英] Replacement for my IF statement?
问题描述
你好!
我正在尝试将我的应用程序设置为在扩展特定节点时更改树视图的背景颜色.我有几个树视图,我需要能够对每个树视图进行处理.有没有比以下更好的方法了?
I am trying to set my application to change the background colour of a tree view when a specific node is expanded. I have several tree views and I need to be able to do it for each of them. Is there a better way to do this than the following:
Private Sub trvArmour_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles trvArmour.AfterSelect
'Clear the listbox.
'Perform the function to determine which node is selected.
'If the root node (0) or (1) or (2) or (3) is selected
'Collapse all tree nodes
'Then expand this selected tree node
'and change the background colour of treeview to this Argb colour.
lstShopItemSpec.Items.Clear()
Main()
If trvArmour.Nodes(0).IsSelected Or trvWeapons.Nodes(0).IsSelected Then
trvArmour.CollapseAll()
trvArmour.Nodes(0).Expand()
trvArmour.BackColor = Color.FromArgb(205, 127, 50)
ElseIf trvArmour.Nodes(1).IsSelected Then
trvArmour.CollapseAll()
trvArmour.Nodes(1).Expand()
trvArmour.BackColor = Color.FromArgb(192, 192, 192)
ElseIf trvArmour.Nodes(2).IsSelected Then
trvArmour.CollapseAll()
trvArmour.Nodes(2).Expand()
trvArmour.BackColor = Color.FromArgb(212, 175, 55)
ElseIf trvArmour.Nodes(3).IsSelected Then
trvArmour.CollapseAll()
trvArmour.Nodes(3).Expand()
trvArmour.BackColor = Color.FromArgb(185, 242, 255)
End If
End Sub
此代码分别检查每个根节点,然后执行正确的代码.但是,这仅适用于我的树视图之一-trvArmour.我需要将其应用于所有树状视图,但是我不想遍历每个人的代码 元素.
This code checks each root node individually and then performs the right piece of code. However, this only applies to one of my tree views - trvArmour. I need to apply to all of my tree views but I don't want to go through and repeat this code for each individual element.
霍普金斯大学
推荐答案
您不需要ElseIf.
You don't need an ElseIf.
有些人认为添加ElseIf语句可以使其运行更快,但是ElseIf本身就是最昂贵的运算符之一.
Some think that adding ElseIf statements makes it faster, however the ElseIf is one of the most costly operators itself.
如果我要避免花费一皮秒的时间,我会这样做(尽管它是UI,所以大部分由视频适配器完成)
I would do it this way if I would avoid any spending of a picosecond (although it is UI so most is done by the videoadapter)
Public Class Form1
Private Sub trvArmour_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles trvArmour.AfterSelect
'Clear the listbox.
'Perform the function to determine which node is selected.
'If the root node (0) or (1) or (2) or (3) is selected
'Collapse all tree nodes
'Then expand this selected tree node
'and change the background colour of treeview to this Argb colour.
lstShopItemSpec.Items.Clear()
Main()
If trvArmour.Nodes(0).IsSelected OrElse trvWeapons.Nodes(0).IsSelected Then
trvArmour.CollapseAll()
trvArmour.Nodes(0).Expand()
trvArmour.BackColor = Color.FromArgb(205, 127, 50)
Return
End If
If trvArmour.Nodes(1).IsSelected Then
trvArmour.CollapseAll()
trvArmour.Nodes(1).Expand()
trvArmour.BackColor = Color.FromArgb(192, 192, 192)
Return
End If
If trvArmour.Nodes(2).IsSelected Then
trvArmour.CollapseAll()
trvArmour.Nodes(2).Expand()
trvArmour.BackColor = Color.FromArgb(212, 175, 55)
Return
End If
If trvArmour.Nodes(3).IsSelected Then
trvArmour.CollapseAll()
trvArmour.Nodes(3).Expand()
trvArmour.BackColor = Color.FromArgb(185, 242, 255)
Return
End If
End Sub
End Class
在这种情况下,您不能使用Select Case.
You cannot use a Select Case in this case.
也在我使用的OrElse上观看
Also watch at the OrElse I used
这篇关于替换我的IF语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!