Archive

Archive for November, 2009

VB.NET 2008 – Hint’s and Tip’s (including flicker-free ListViews)

November 19th, 2009 No comments
Rating 4.75 out of 5


Hi Avid Reader,

As you probably know by now, I’m a VB programmer. I’ve spent many year using VB6, and am now (under duress) having to migrate to VB.NET.

The purpose of this article is to show you some cool hint’s, tip’s, trick’s and bug-fixes that I’ve found along the way. I’ll be updating this Blog entry as time moves forward, so stay tuned to this one!

List View Flickers

This is just plain stupid. The standard ListView control (as part of the Common Control Library 6) invalidates the entire control each time a new item is added – e.g. lvInfo.Items.Add(“Hello World”)

This can be fixed using a little bit of Windows ‘SendMessage’ tweaking;

' Place at the top of the Form Class

Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, _
 ByVal msg As Integer, _
 ByVal wParam As IntPtr, _
 ByVal lParam As IntPtr) As IntPtr

 Const LVM_FIRST = &H1000
 Const LVM_SETEXTENDEDLISTVIEWSTYLE = (LVM_FIRST + 54)
 Const LVM_GETEXTENDEDLISTVIEWSTYLE = (LVM_FIRST + 55)
 Const LVS_EX_DOUBLEBUFFER = &H10000
 Const LVS_EX_BORDERSELECT = &H8000
' Place in the Form_Load event

 Dim styles As Long
 styles = SendMessage(lvInfo.Handle, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0&)
 styles = styles Or LVS_EX_DOUBLEBUFFER Or LVS_EX_BORDERSELECT
 Call SendMessage(lvInfo.Handle, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, styles)

This fix gives you a nice, smooth update each time.

The next fix is “How to keep the last item entered into a ListView visible…”

lv = myFormName.lvInfo.Items.Add("My Information to Show")
lv.EnsureVisible()

Simple, but effective!

Getting the App.Path in VB.NET

Another simple one (when you know it!) :-

Dim AppPath As String = Application.StartupPath()

Reading and Writing INI Files

Microsoft wants us to abandon INI Files and use XML instead. Whilst this may seem logical, its not always the simplest way of doing something (which sums VB.NET up really….) – This Class will give you a simple way of reading and writing INI Files.

Public Class IniFile
 ' API functions
 Private Declare Ansi Function GetPrivateProfileString _
 Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
 (ByVal lpApplicationName As String, _
 ByVal lpKeyName As String, ByVal lpDefault As String, _
 ByVal lpReturnedString As System.Text.StringBuilder, _
 ByVal nSize As Integer, ByVal lpFileName As String) _
 As Integer
 Private Declare Ansi Function WritePrivateProfileString _
 Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
 (ByVal lpApplicationName As String, _
 ByVal lpKeyName As String, ByVal lpString As String, _
 ByVal lpFileName As String) As Integer
 Private Declare Ansi Function GetPrivateProfileInt _
 Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _
 (ByVal lpApplicationName As String, _
 ByVal lpKeyName As String, ByVal nDefault As Integer, _
 ByVal lpFileName As String) As Integer
 Private Declare Ansi Function FlushPrivateProfileString _
 Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
 (ByVal lpApplicationName As Integer, _
 ByVal lpKeyName As Integer, ByVal lpString As Integer, _
 ByVal lpFileName As String) As Integer
 Dim strFilename As String

 ' Constructor, accepting a filename
 Public Sub New(ByVal Filename As String)
 strFilename = Filename
 End Sub

 ' Read-only filename property
 ReadOnly Property FileName() As String
 Get
 Return strFilename
 End Get
 End Property

 Public Function GetString(ByVal Section As String, _
 ByVal Key As String, ByVal [Default] As String) As String
 ' Returns a string from your INI file
 Dim intCharCount As Integer
 Dim objResult As New System.Text.StringBuilder(256)
 intCharCount = GetPrivateProfileString(Section, Key, _
 [Default], objResult, objResult.Capacity, strFilename)
 If intCharCount > 0 Then GetString = _
 Left(objResult.ToString, intCharCount)
 End Function

 Public Function GetInteger(ByVal Section As String, _
 ByVal Key As String, ByVal [Default] As Integer) As Integer
 ' Returns an integer from your INI file
 Return GetPrivateProfileInt(Section, Key, _
 [Default], strFilename)
 End Function

 Public Sub WriteString(ByVal Section As String, _
 ByVal Key As String, ByVal Value As String)
 ' Writes a string to your INI file
 WritePrivateProfileString(Section, Key, Value, strFilename)
 Flush()
 End Sub

 Public Sub WriteInteger(ByVal Section As String, _
 ByVal Key As String, ByVal Value As Integer)
 ' Writes an integer to your INI file
 WriteString(Section, Key, CStr(Value))
 Flush()
 End Sub

 Private Sub Flush()
 ' Stores all the cached changes to your INI file
 FlushPrivateProfileString(0, 0, 0, strFilename)
 End Sub

 End Class

 

This can be used in the following manner:-

 Dim settings As New IniFile(Application.StartupPath & "\My_INI_File.ini")
 gCustName = settings.GetString("General", "CustName", "Not Registered")
 gCustSerial = settings.GetString("General", "CustSerial", "")
Share
Categories: General Stuff Tags: