Contact

 
Google
Web www.alanphipps.com

 
   
 
   
www.alanphipps.com

.: Visual Basic .NET 2005 and XNA

 
 

 

 

 

 

Access CheckBox Value in a DataGridView Checkbox Column

When using a checkbox column in a DataGridView, i found it difficult access checkbox cell's value, after much research i came up with this which works for me.

Retrieve Value

Dim DGV1CBV As String = Me.DataGridView1.Rows(0).Cells(0).EditedFormattedValue.ToString


If DGV1CBV = "False" Then
' Add you Code Here
ElseIf DGV1CBV = "True" then
' Add Your Code here.
End IF

DGV1CBV = Nothing

 

Set Value

DataGridView1.Rows(0).Cells(0).Value = "True"

Or

DataGridView1.Rows(0).Cells(0).Value = "False"

 

Outlook Style VB.NET 2005 Modified DataGridView - Uses 2 Rows

This DatGridView has been modifed so that 2 rows appears as one, as shown below:

VBdotNET DataGridView with 2 Rows

Each row is accessed as it normally would be if it were un-modified. The modifications are implemented by using the following VB.NET Subs:

DataGridView1_RowsAdded

DataGridView1_SelectionChanged

This is not a complex user control that inherits from the existing DataGridView template, it simply uses the above subs to give the appearance that each row includes 2 standard rows as shown above. Feel free to use and modify this form as you see fit, but please read the code comments that are included with the project solution.

Modified DGV - 134Kb

 

A Multi-Select Treeview Control

The treeview control native to VB.NET does not have multi-select functionality. I therefore have added this option using the various treeview methods, BeforeSelect, AfterSelect, AfterCollapse, etc, etc. . Please fell free to use this code in your own applications:

A Multi-Select Treeview Control in VB.NET

When a node is selected, it is added to the SelectedNodes() array. Pressing the "Show Selected Nodes" will display the currently selected nodes in a message box.

Multi-Select Treeview - 100Kb

 

Extract the Associated Icon From a File in VB.NET Without Using the Windows ExtractIcon API

This example shows how to extract the icon from a file without using the ExtractIcon Win32 API.

Extract Icon in VB.NET Without Using Win32 API's

ExtractIcon Source Code - 68Kb

 

Determine Default Icon For File Type Without Using the Windows ExtractIcon API

The following functions will return the default icon for a file type, either as an icon object or as a bitmap.

3 functions are necessary:


Public Shared Function GetRandomString(ByVal NumberOfChars As Integer)

If NumberOfChars <= 1 Then
Return ""
Exit Function
End If

Dim RandomString As String = ""
Dim Rand As New Random
Dim CharArray() As Char = New Char(35) {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", _
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", _
"8", "9"}

For x As Integer = 0 To NumberOfChars
RandomString = RandomString & CharArray(Rand.Next(0, 34))
Next

Return RandomString

RandomString = Nothing
Rand = Nothing
Erase CharArray

End Function

This funcrtion gets a random string to be used as a temporary file name.

 


Public Shared Function GetIconAsImageFromFile(ByVal FilePath As String, ByVal IconWidth As Integer, ByVal IconHeight As Integer)

Dim myIcon As System.Drawing.Icon = Icon.ExtractAssociatedIcon(FilePath)
Dim ABitmap As New Bitmap(myIcon.ToBitmap(), CInt(IconWidth), CInt(IconHeight))

Return ABitmap

myIcon = Nothing
ABitmap = Nothing

End Function

This function gets the icon from the temporary file and returns it as a bitmap, change "Return ABitmap" to "Return myIcon" to return the icon as an icon object.

 


Public Shared Function GetIconAsImageForFileType(ByVal FilePath As String, ByVal IconWidth As Integer, ByVal IconHeight As Integer)

'Get the file extension from the given file path,
'The file path does not have to point to a real file, no file exists check takes place.
'If you dont have a file path then just make one up with the necessary file extension.

Dim FileExt As String = Mid(My.Computer.FileSystem.GetName(FilePath), _
InStrRev(My.Computer.FileSystem.GetName(FilePath), "."))

tryagain:

'Create a random file name
Dim tempfile As String = GetRandomString(10) & FileExt

' if the random file exists then recreate the random file name
If My.Computer.FileSystem.FileExists(tempfile) Then
GoTo tryagain
Else
'Create a temp file taht has the necessary file extension
FSw = New FileStream(My.Computer.FileSystem.SpecialDirectories.Temp & "\" & _
My.Computer.FileSystem.GetName(FilePath), FileMode.Create, FileAccess.Write)
FSw.Close()
End If

'Get the default icon for the temporary file and save it as a bitmap
Dim ABitmap As Bitmap = GetIconAsImageFromFile(tempfile, IconWidth, IconHeight)

'end function and return the bitmap
Return ABitmap

'delete teh temporary file
Try
My.Computer.FileSystem.DeleteFile(tempfile)
Catch ex As Exception
'MsgBox(ex.Message)
End Try

FileExt = Nothing
tempfile = Nothing
ABitmap = Nothing

End Function

This is the function that will get the default icon for a particular file type. It takes a file path as a parameter and then gets the default icon for the file extension of the file in the file path. The function does not check if the file exists so just make up any file path, it doesnt have to point to a real file. Valid Icon Widths and Hieghts are 8,8 - 16,16 - 24,24 and 32,32.

 

     
 
 
     

 

Web site contents © Copyright Alan Phipps 2006, All rights reserved.
Website templates
 
   
 
 

 

__PayPal

 
Please Donate to the Nvidia Geforce Go 7950 GTX Fund, All donations welcome. Thanks.