11.07.2015 Views

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>101</strong> TECH TIPSFor <strong>Visual</strong> <strong>Studio</strong> DevelopersVB4/32, VB5, VB6Level: IntermediateAdd Multicharacter Search Capability to ListboxesUsers often complain that listboxes don’t have the same multiplekeypress search capabilities as treeviews and other objects. Butyou can simulate this behavior by adding code to a form with atimer and a listbox whose Sorted property is set to True.For this test, Form_Load adds some data and sets the defaultinterval between keystrokes. You can type in “AL” to get to Allaninstead of the first instance of an entry with an “a” in the list. Thiscan be extremely helpful in long lists. You can also convert thiscode easily for use within a custom control:Option ExplicitPrivate Declare Function SendMessage Lib "user32" _Alias "SendMessageA" (ByVal hwnd As Long, _ByVal wMsg As Long, ByVal wParam As Long, _lParam As Any) As LongPrivate Const LB_FINDSTRING = &H18FPrivate Const LB_ERR = (-1)Private sSearchstring As StringPrivate Sub Form_Load()With List1.AddItem "Adam".AddItem "Allan".AddItem "Arty".AddItem "Aslan".AddItem "Barney".AddItem "Bob"End WithTimer1.Interval = 2000End SubPrivate Sub List1_KeyPress(KeyAscii As Integer)Dim nResult As LongTimer1.Enabled = TruesSearchstring = sSearchstring & Chr$(KeyAscii)With List1nResult = SendMessage(.hWnd, LB_FINDSTRING, _.ListIndex, ByVal sSearchstring)If nResult LB_ERR Then.ListIndex = nResultKeyAscii = 0End IfEnd WithEnd SubPrivate Sub Timer1_Timer()sSearchstring = ""Timer1.Enabled = FalseEnd SubVS.NETLevel: Intermediate—Joseph L. Scally, Stamford, Conn.Use Locals to Speed Up CodeWhen working with an object’s fields repetitively in VS.NET, youcan improve performance two-fold by storing the object as a localvariable rather than a field. In VB.NET, when you use the WithmyObject ... End With syntax, a local variable is created formyObject. In C#, you must declare the local variable and set it tothe object.—Bill McCarthy, Barongarook, Victoria, AustraliaVB4/32, VB5, VB6, SQL Server 7.0Level: AdvancedExecute a SQL Server DTS Package RemotelyYou can easily execute a SQL Server 7.0 Data TransformationServices (DTS) package from VB remotely:1. Create a DTS package. It can be an import from Excel into SQLServer.2. Set a reference to Microsoft DTS Package Object Library in anyVB project. You might need to load SQL Server on the developmentmachine.3. Use the LoadFromSQLServer method on the package object:Private Sub cmdRefreshCustomers_Click()Dim oPackage As New DTS.PackageOn Error GoTo eh'Load the package that we created previously' ("Customer_List").'Use the global variables for SQL Server name, UserID,'and Password.oPackage.LoadFromSQLServer sServername, sUid, sPwd, _DTSSQLStgFlag_Default, _"", "", "", "Customer_List", 0'Execute the PackageoPackage.ExecuteMsgBox oPackage.Description, vbInformation, _"Re-import Excel sheet."'Clean up.Set oPackage = NothingExit Subeh:MsgBox Err.Description, vbCritical, _"Error refreshing Customer List"'For more sophisticated sample VB code with DTS, go'to the SQL Server 7 CD and browse these folders:'devtools\samples\dts\dtsempl1 or 2 or 3.End SubThis is a simple, powerful way to take advantage of any DTSpackage.VB3, VB4, VB5, VB6Level: Beginning—Steve Simon, Palisades Park, N.J.Embed Quotation MarksYou use quotation marks in VB to define strings, but how do youinclude them in your output? Use whichever of these methodsworks the best for you:Dim strUseChr As StringDim strUseVar As StringDim strUseDbl As StringConst Quote As String = """"strUseChr = "Hello " & Chr$(34) & "VB" & _Chr$(34) & " World!"strUseVar = "Hello " & Quote & "VB" & _Quote & " World!"strUseDbl = "Hello ""VB"" World!"Debug.Print strUseChrDebug.Print strUseVarDebug.Print strUseDblEach one prints:Hello "VB" World!—Dave Keighan, Victoria, British Columbia22 Supplement to <strong>Visual</strong> <strong>Studio</strong> <strong>Magazine</strong> SEPTEMBER 2001

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!