This VB sample shows how to change image in the node in a two ways - by changing ImageIndex/SelectedImageIndex and by exchanging images in the ImageList attached to FlyGrid.Net.
Public Class Form1
Private random As System.Random ' a random numbers generator
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetupGrid()
End Sub
Private Sub AddNodes(ByVal grid As FlyGrid, ByVal prefix As String, ByVal nodes As NodeCollection)
If Me.random Is Nothing Then
Me.random = New System.Random(grid.Images.Images.Count)
End If
Dim v_arrNode As Node() = New Node(10 - 1) {}
Dim v_imageIndex As Integer = 0
For v_i As Integer = 0 To v_arrNode.Length - 1
v_imageIndex = Me.random.Next(0, grid.Images.Images.Count)
Dim v_arrobj As Object() = New Object(grid.Columns.VisibleColumns.Length - 1) {}
v_arrobj(0) = String.Concat(prefix, "Node", v_i.ToString)
v_arrNode(v_i) = New TreeViewNode(v_arrobj, v_imageIndex)
Next v_i
nodes.AddRange(v_arrNode)
End Sub
Private Sub SetupGrid()
grid.BeginInit()
Try
AddNodes(grid, "", grid.Rows.Items)
For Each v_node As Node In grid.Rows.Items
AddNodes(grid, "Sub", v_node.Items)
For Each v_subnode As Node In v_node.Items
AddNodes(grid, "Subsub", v_subnode.Items)
Next v_subnode
Next v_node
'select the first node
grid.Selected = grid.Rows.Items(0)
Finally
grid.EndInit()
End Try
End Sub
Private Sub btnReplaceImage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplaceImage1.Click
'this function just changes image index of node - so replaces graphic appearance of the node
Dim selectedNode As TreeViewNode = grid.Selected
If (selectedNode Is Nothing) Then
MessageBox.Show("Please select a node")
Else
'change image on a random basis (random generator is created in AddNodes procedure)
selectedNode.ImageIndex = ChangeImageIndex(selectedNode.ImageIndex)
selectedNode.SelectedImageIndex = selectedNode.ImageIndex
End If
End Sub
Private Function ChangeImageIndex(ByVal currentImageIndex As Integer) As Integer
Dim newImageIndex = random.Next(grid.Images.Images.Count)
While (newImageIndex = currentImageIndex)
newImageIndex = random.Next(grid.Images.Images.Count)
End While
Return newImageIndex
End Function
Private Sub btnReplaceImage2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReplaceImage2.Click
'this fprocedure just replaces images in the image list and replaces graphic appearance of the node
Dim selectedNode As TreeViewNode = grid.Selected
If (selectedNode Is Nothing) Then
MessageBox.Show("Please select a node")
Else
'change image on a random basis (random generator is created in AddNodes procedure)
Dim selectedImageIndex As Integer = selectedNode.ImageIndex
Dim newImageIndex As Integer = ChangeImageIndex(selectedImageIndex)
Dim selImage As System.Drawing.Image = grid.Images.Images(selectedImageIndex)
Dim newImage As System.Drawing.Image = grid.Images.Images(newImageIndex)
'exchange images in the image list
grid.Images.Images(selectedImageIndex) = newImage
grid.Images.Images(newImageIndex) = selImage
grid.InvalidateNode(selectedNode)
End If
End Sub
End Class