Developers Archive for the 'Visual Basic' Category

Convert Decimal Integer Values to Binary String in VB6

Convert Decimal Integer Values to Binary String in VB6 Tuesday, February 27th, 2007

Public Function DecToBin(DeciValue As Long, Optional NoOfBits As Integer) _As String
 Dim i As Integer
 NoOfBits = 8
‘make sure there are enough bits to contain the number
 Do While DeciValue > (2 ^ NoOfBits) - 1 
 NoOfBits = NoOfBits + 8
 Loop
 DecToBin = vbNullString
‘build the string
For i = 0 To (NoOfBits - 1)    
DecToBin = CStr((DeciValue And 2 ^ i) / 2 ^ i) & DecToBin
Next i
End Function

VB Express Color Object to Hex String

VB Express Color Object to Hex String Monday, February 26th, 2007

This function takes a color object and returns the Hex value with a preceeding #.

Private Function ColorHex( ByVal color As Color) As String
        Dim r As String = Hex(FColor.R), G As String = Hex(FColor.G), B As String = Hex(FColor.B)
        If r.Length = 1 Then r = 0 & r : If G.Length = 1 Then G = 0 & G : If B.Length = 1 Then B = 0 & B
        ColorHex = “#” & r & G & B
End Function

Counting lines in a multi-line textbox

Counting lines in a multi-line textbox Friday, February 23rd, 2007

Often, you may need to know how many lines of text there are in a multi-line textbox. While Visual Basic makes it easy to determine how many paragraphs the textbox contains, using code like so:

Private Sub Command1_Click()
    Dim myParas As Variant
    myParas = Split(Text1, vbNewLine)
    MsgBox UBound(myParas) + 1
End Sub

This code, as you can see, only parses out hard carriage returns; whereas you want to know how many lines of text the control contains.

To accomplish this task, you’ll need to resort to the SendMessageAsLong() API function. This function conforms to the following syntax:

Private Declare Function SendMessageAsLong Lib “user32″ _
     Alias “SendMessageA” (ByVal hWnd As Long, ByVal wMsg As Long, _
     ByVal wParam As Long, ByVal lParam As Long) As Long

To obtain the number of lines, you pass in the EM_GETLINECOUNT constant in the wMsg parameter. Declare this constant like this:

Const EM_GETLINECOUNT = 186

Finally, to use the function, pass in the handle to the textbox from which you want to retrieve the line count, as well as the wMsg we mentioned earlier. Example code might look like so:

Private Sub Command2_Click()
    Dim lCount As Long
    lCount = SendMessageAsLong(Text1.hWnd, EM_GETLINECOUNT, 0, 0)
    MsgBox lCount
End Sub


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.