Thursday, March 26, 2009

Save a byte array to a file

I've done this before and always seem to forget about it..  Found this here http://www.example-code.com/vbdotnet/save-byte-array.asp

How to save a byte array to a file in Visual Basic .NET
        Dim byteData() As Byte


' Create a file and write the byte data to a file.
Dim oFileStream As System.IO.FileStream
oFileStream = New System.IO.FileStream("bytes.dat", System.IO.FileMode.Create)
oFileStream.Write(byteData, 0, byteData.Length)
oFileStream.Close()



Friday, March 13, 2009

how to close a form when it loses focus vb.net

Put the the Me.Close call in the _Deactivate event.

The scenario is such:
- You have two forms, Form1 and Form2.  In Form1 you have a button and on the button_click event it just calls Form2.Show.  However when you click back on form1 you want Form2 to close.  You don't want it to go to the background, you want it out of there.  The solution is to close it in the DeActivate event

  Private Sub Form2_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Deactivate
    Me.Close()
  End Sub

Thursday, March 12, 2009

Read a comma delimited file in VB.NET

the snippets is
filParseText

The code is below:
Dim filename As String = FILE_NAME
Dim fields As String()
Dim delimiter As String = ","
Using parser As New TextFieldParser(filename)
parser.SetDelimiters(delimiter)
While Not parser.EndOfData
' Read in the fields for the current line
fields = parser.ReadFields()
' Add code here to use data in fields variable.
End While
End Using

Recursively Iterate through a folder

Set fs = WScript.CreateObject ("Scripting.FileSystemObject")

Sub ShowSubFolders(Folder)
For Each Subfolder In Folder.SubFolders
Set files = SubFolder.Files
For Each file In files
WScript.Echo file.Name, file.Size
Next
ShowSubFolders Subfolder
Next
End Sub

ShowSubFolders fs.GetFolder("C:\your\path\here")

Check for a text in a string

Dim find As String = "Ted"
Dim strg As String = "My name is Ted Unnikumaran"
If InStr(obj, find) = 1 Then
MsgBox("found it")
Else
MsgBox("not found")
End If

Monday, March 9, 2009

Visual Basic 2005: A Developer's Notebook/Windows Applications

A great  number of tips and tricks that are in visual basic 2005.


The one that I didn't know about that I used immediately was that now DoubleBuffered is a property that you can set for a UserControl.  PReviously, I would set it in the Sub New.

http://commons.oreilly.com/wiki/index.php/Visual_Basic_2005:_A_Developer%27s_Notebook/Windows_Applications



DoEvents vs Sleep

Sometimes when you want to delay things you might call a DoEvents.  However I've ocassionally come across situations where this can really slow down an application.  Sleep is actually called by DoEvents, so it might be better to call Sleep instead.  Call DoEvents if you have a long running routine or loop and you don't want to tie up the operating system.  Thats my take on it. 

This forum has more on the topic.
http://www.vbforums.com/showthread.php?t=344709

And it has an interesting API call where you check the status to see if there are items waiting in the Queue

  1. Option Explicit
  2.  
  3. Private Const QS_HOTKEY = &H80
  4. Private Const QS_KEY = &H1
  5. Private Const QS_MOUSEBUTTON = &H4
  6. Private Const QS_MOUSEMOVE = &H2
  7. Private Const QS_PAINT = &H20
  8. Private Const QS_POSTMESSAGE = &H8
  9. Private Const QS_SENDMESSAGE = &H40
  10. Private Const QS_TIMER = &H10
  11. Private Const QS_ALLINPUT = (QS_SENDMESSAGE Or QS_PAINT Or QS_TIMER Or QS_POSTMESSAGE Or QS_MOUSEBUTTON Or QS_MOUSEMOVE Or QS_HOTKEY Or QS_KEY)
  12.  
  13. Private Declare Function GetQueueStatus Lib "user32" (ByVal fuFlags As Long) As Long
  14.  
  15. Private Sub Form_Click()
  16.     Dim lngDoEventCnt As Long
  17.     Dim lngLoopCount As Long
  18.    
  19.     Do
  20.         If GetQueueStatus(QS_ALLINPUT) <> 0 Then
  21.             lngDoEventCnt = lngDoEventCnt + 1
  22.             DoEvents
  23.         End If
  24.        
  25.         lngLoopCount = lngLoopCount + 1
  26.     Loop Until lngLoopCount = 2000000
  27.    
  28.     Debug.Print lngDoEventCnt
  29. End Sub


Another post from the forum is below:

HOWTO: Determine the Differences Between DoEvents and Sleep
Q158175


SUMMARY
This article explains the differences between the Visual Basic DoEvents function and
the Sleep() Windows API function.

MORE INFORMATION
DoEvents is a Visual Basic function that yields execution so the operating system can
process other events. This function cleans out the message loop and executes any other
pending business in the Visual Basic runtime. Upon completing the pending business
execution, the function calls the Sleep function with zero (0) as the argument so that
the remaining time slice can be used to check the queue.

The Sleep 32-bit API function is a subset of the DoEvents function. The Visual Basic
program calling the function and the Visual Basic runtime executable and interactions
with Windows are immediately put to sleep by this function. The programs remain inactive
for the time in milliseconds specified in the Sleep argument.

The Sleep function allows you to specify the amount of time your applications are
inactive. The DoEvents function returns control to the Visual Basic program after
the operating system has finished processing the events in its queue and all keys in
the SendKeys queue have been sent.

The information in this article applies to:
Microsoft Visual Basic Learning Edition for Windows 5.0
Microsoft Visual Basic Learning Edition for Windows 6.0
Microsoft Visual Basic Professional Edition for Windows 5.0
Microsoft Visual Basic Professional Edition for Windows 6.0
Microsoft Visual Basic Enterprise Edition for Windows 5.0
Microsoft Visual Basic Enterprise Edition for Windows 6.0
Microsoft Visual Basic Standard Edition, 32-bit, for Windows 4.0
Microsoft Visual Basic Professional Edition, 32-bit, for Windows 4.0
Microsoft Visual Basic Enterprise Edition, 32-bit, for Windows 4.0
Last Reviewed: 5/13/2003 (3.0)
Keywords: kbhowto KB158175