{"id":135,"date":"2009-11-19T13:04:21","date_gmt":"2009-11-19T12:04:21","guid":{"rendered":"http:\/\/www.internet-tools.co.uk\/blog\/?p=135"},"modified":"2009-11-19T13:04:21","modified_gmt":"2009-11-19T12:04:21","slug":"vb-net-2008-hints-and-tips-including-flicker-free-listviews","status":"publish","type":"post","link":"https:\/\/www.internet-tools.co.uk\/blog\/index.php\/2009\/11\/19\/vb-net-2008-hints-and-tips-including-flicker-free-listviews\/","title":{"rendered":"VB.NET 2008 &#8211; Hint&#8217;s and Tip&#8217;s (including flicker-free ListViews)"},"content":{"rendered":"<p>Hi Avid Reader,<\/p>\n<p>As you probably know by now, I&#8217;m a VB programmer. I&#8217;ve spent many year using VB6, and am now (under duress) having to migrate to VB.NET.<\/p>\n<p>The purpose of this article is to show you some cool hint&#8217;s, tip&#8217;s, trick&#8217;s and bug-fixes that I&#8217;ve found along the way. I&#8217;ll be updating this Blog entry as time moves forward, so stay tuned to this one!<\/p>\n<h2><strong>List View Flickers<\/strong><\/h2>\n<p>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 &#8211; e.g. lvInfo.Items.Add(&#8220;Hello World&#8221;)<\/p>\n<p>This can be fixed using a little bit of Windows &#8216;SendMessage&#8217; tweaking;<\/p>\n<pre><span style=\"color: #0000ff;\">' Place at the top of the Form Class\r\n<\/span>\r\n<span style=\"color: #0000ff;\">Declare Auto Function SendMessage Lib \"user32.dll\" (ByVal hWnd As IntPtr, _\r\n ByVal msg As Integer, _\r\n ByVal wParam As IntPtr, _\r\n ByVal lParam As IntPtr) As IntPtr<\/span>\r\n\r\n <span style=\"color: #0000ff;\">Const LVM_FIRST = &amp;H1000\r\n Const LVM_SETEXTENDEDLISTVIEWSTYLE = (LVM_FIRST + 54)\r\n Const LVM_GETEXTENDEDLISTVIEWSTYLE = (LVM_FIRST + 55)\r\n Const LVS_EX_DOUBLEBUFFER = &amp;H10000\r\n Const LVS_EX_BORDERSELECT = &amp;H8000<\/span><\/pre>\n<pre><span style=\"color: #0000ff;\">' Place in the Form_Load event\r\n\r\n Dim styles As Long\r\n styles = SendMessage(lvInfo.Handle, LVM_GETEXTENDEDLISTVIEWSTYLE, 0, 0&amp;)\r\n styles = styles Or LVS_EX_DOUBLEBUFFER Or LVS_EX_BORDERSELECT\r\n Call SendMessage(lvInfo.Handle, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, styles)<\/span><\/pre>\n<p>This fix gives you a nice, smooth update each time.<\/p>\n<p>The next fix is &#8220;How to keep the last item entered into a ListView visible&#8230;&#8221;<\/p>\n<pre><span style=\"color: #0000ff;\">lv = myFormName.lvInfo.Items.Add(\"My Information to Show\")\r\nlv.EnsureVisible()<\/span><\/pre>\n<p>Simple, but effective!<\/p>\n<h2>Getting the App.Path in VB.NET<\/h2>\n<p>Another simple one (when you know it!) :-<\/p>\n<pre><span style=\"color: #0000ff;\">Dim AppPath As String = Application.StartupPath()<\/span><\/pre>\n<h2>Reading and Writing INI Files<\/h2>\n<p>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&#8230;.) &#8211; This Class will give you a simple way of reading and writing INI Files.<\/p>\n<pre><span style=\"color: #0000ff;\">Public Class IniFile\r\n ' API functions\r\n Private Declare Ansi Function GetPrivateProfileString _\r\n Lib \"kernel32.dll\" Alias \"GetPrivateProfileStringA\" _\r\n (ByVal lpApplicationName As String, _\r\n ByVal lpKeyName As String, ByVal lpDefault As String, _\r\n ByVal lpReturnedString As System.Text.StringBuilder, _\r\n ByVal nSize As Integer, ByVal lpFileName As String) _\r\n As Integer\r\n Private Declare Ansi Function WritePrivateProfileString _\r\n Lib \"kernel32.dll\" Alias \"WritePrivateProfileStringA\" _\r\n (ByVal lpApplicationName As String, _\r\n ByVal lpKeyName As String, ByVal lpString As String, _\r\n ByVal lpFileName As String) As Integer\r\n Private Declare Ansi Function GetPrivateProfileInt _\r\n Lib \"kernel32.dll\" Alias \"GetPrivateProfileIntA\" _\r\n (ByVal lpApplicationName As String, _\r\n ByVal lpKeyName As String, ByVal nDefault As Integer, _\r\n ByVal lpFileName As String) As Integer\r\n Private Declare Ansi Function FlushPrivateProfileString _\r\n Lib \"kernel32.dll\" Alias \"WritePrivateProfileStringA\" _\r\n (ByVal lpApplicationName As Integer, _\r\n ByVal lpKeyName As Integer, ByVal lpString As Integer, _\r\n ByVal lpFileName As String) As Integer\r\n Dim strFilename As String\r\n\r\n ' Constructor, accepting a filename\r\n Public Sub New(ByVal Filename As String)\r\n strFilename = Filename\r\n End Sub\r\n\r\n ' Read-only filename property\r\n ReadOnly Property FileName() As String\r\n Get\r\n Return strFilename\r\n End Get\r\n End Property\r\n\r\n Public Function GetString(ByVal Section As String, _\r\n ByVal Key As String, ByVal [Default] As String) As String\r\n ' Returns a string from your INI file\r\n Dim intCharCount As Integer\r\n Dim objResult As New System.Text.StringBuilder(256)\r\n intCharCount = GetPrivateProfileString(Section, Key, _\r\n [Default], objResult, objResult.Capacity, strFilename)\r\n If intCharCount &gt; 0 Then GetString = _\r\n Left(objResult.ToString, intCharCount)\r\n End Function\r\n\r\n Public Function GetInteger(ByVal Section As String, _\r\n ByVal Key As String, ByVal [Default] As Integer) As Integer\r\n ' Returns an integer from your INI file\r\n Return GetPrivateProfileInt(Section, Key, _\r\n [Default], strFilename)\r\n End Function\r\n\r\n Public Sub WriteString(ByVal Section As String, _\r\n ByVal Key As String, ByVal Value As String)\r\n ' Writes a string to your INI file\r\n WritePrivateProfileString(Section, Key, Value, strFilename)\r\n Flush()\r\n End Sub\r\n\r\n Public Sub WriteInteger(ByVal Section As String, _\r\n ByVal Key As String, ByVal Value As Integer)\r\n ' Writes an integer to your INI file\r\n WriteString(Section, Key, CStr(Value))\r\n Flush()\r\n End Sub\r\n\r\n Private Sub Flush()\r\n ' Stores all the cached changes to your INI file\r\n FlushPrivateProfileString(0, 0, 0, strFilename)\r\n End Sub\r\n\r\n End Class\r\n\r\n<\/span><span style=\"color: #0000ff;\">\u00a0<\/span><\/pre>\n<p><span style=\"color: #000000;\">This can be used in the following manner:-<\/span><\/p>\n<pre><span style=\"color: #0000ff;\"> Dim settings As New IniFile(Application.StartupPath &amp; \"\\My_INI_File.ini\")\r\n gCustName = settings.GetString(\"General\", \"CustName\", \"Not Registered\")\r\n gCustSerial = settings.GetString(\"General\", \"CustSerial\", \"\")<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Hi Avid Reader, As you probably know by now, I&#8217;m a VB programmer. I&#8217;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&#8217;s, tip&#8217;s, trick&#8217;s and bug-fixes that I&#8217;ve found along the way. I&#8217;ll be updating this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-135","post","type-post","status-publish","format-standard","hentry","category-general-stuff"],"_links":{"self":[{"href":"https:\/\/www.internet-tools.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts\/135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.internet-tools.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.internet-tools.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.internet-tools.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.internet-tools.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=135"}],"version-history":[{"count":8,"href":"https:\/\/www.internet-tools.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"predecessor-version":[{"id":143,"href":"https:\/\/www.internet-tools.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/posts\/135\/revisions\/143"}],"wp:attachment":[{"href":"https:\/\/www.internet-tools.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.internet-tools.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.internet-tools.co.uk\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}