VB.NET – Native VC++ DLL

15 2018

VC++ Data Type – VB.NET Implementation :

1. LPBYTE – ByRef results As Byte,
2. INT – ByVal a as Int32
3. DWORD – ByVal statusSize As Int32
4. LPDWORD – ByRef len As Long

Sample 1:

'EXTERN_C_DLL_EXPORT INT Function1(BOOL bool_value, LPBYTE longpointer_byte_value, DWORD dword_value, LPDWORD longpointer_dword_value);
<DllImport(DLL_FILENAME, CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Function Function1 _
(ByVal bool_valueAs Boolean,
ByRef longpointer_byte_valueAs Byte,
ByVal dword_valueAs Int32,
ByRef longpointer_dword_valueAs Int32) As Int32
End Function

Sample 2:

VC++

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
<Serializable()> Public Structure ErrorLog
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=2, ArraySubType:=UnmanagedType.U1)> _
Public ucErrorID() As Byte ‘ 2 Byte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=5, ArraySubType:=UnmanagedType.U1)> _
Public ucTime() As Byte ‘ 5 Byte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=8, ArraySubType:=UnmanagedType.U1)> _
Public ucErrorCode() As Byte ‘ 8 Byte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=2, ArraySubType:=UnmanagedType.U1)> _
Public ucStatusSts() As Byte ‘ 2 Byte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=22, ArraySubType:=UnmanagedType.U1)> _
Public ucSensorSts() As Byte ‘ 22 Byte
Public ucCmdNo As Byte

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=28, ArraySubType:=UnmanagedType.U1)> _
Public dummy() As Byte ‘ 28 Byte
End Structure

typedef struct _fullLog {
ERRORLOG errLog[5];
} FULLLOG;
VB.NET
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
<Serializable()> Public Structure fulllog
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=512, ArraySubType:=UnmanagedType.AsAny)> _
Public Error_Log() As ErrorLog
End Structure

‘EXTERN_C_DLL_EXPORT INT GetMylLog(FULLLOG *fullLog);
<DllImport(DLL_FILENAME, CallingConvention:=CallingConvention.Cdecl)> _
Public Shared Function GetMylLog(ByRef FullLog1 As ErrorLog, ByVal dwLen As UInt32, ByRef lpLen As UInt32, <MarshalAs(UnmanagedType.LPWStr)> Optional lpPathName As String = “”) As Int32
End Function

 

Slide Animation in Tab Control VB.NET

03 2015

1. Create a New VB.NET Project with one form named as “Form1”.
2. Create a TabControl named as “TabControl1”.
3. Create 2 Timers named as “Timer1” and “Timer2” with Interval 10ms.
4. Create 2 Buttons named as “Button1” and “Button2” with text “Next” and “Back”.
5. Copy and paste the following code and Run to test.


Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Enabled = True
End Sub

Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer2.Enabled = True
End Sub

Dim tabPageOffset As Integer
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
tabPageOffset -= 100 ' Tweak to change speed
If tabPageOffset <= 0 Then tabPageOffset = 0 Timer1.Enabled = False End If TabControl1.SelectedTab.Controls(0).Left = tabPageOffset End Sub Private Sub TabControl1_Selecting(sender As Object, e As TabControlCancelEventArgs) Handles TabControl1.Selecting Dim CurrentTabSelected As Integer = TabControl1.SelectedIndex If TabControl1.SelectedIndex = 1 Then tabPageOffset = e.TabPage.ClientSize.Width ' Timer1.Enabled = True Else tabPageOffset_right_anim = 0 - e.TabPage.ClientSize.Width ' Timer2.Enabled = True End If End Sub Dim tabPageOffset_right_anim As Integer Private Sub Timer2_Tick(sender As System.Object, e As System.EventArgs) Handles Timer2.Tick tabPageOffset_right_anim += 100 ' Tweak to change speed If tabPageOffset_right_anim >= 0 Then
tabPageOffset_right_anim = 0
Timer2.Enabled = False
End If
TabControl1.SelectedTab.Controls(0).Left = tabPageOffset_right_anim
End Sub

Disable Ctrl+Tab in TabControl VB.NET

03 2015

Paste below code in your Form.

Protected Overrides Function ProcessCmdKey(ByRef msg As Message, _
ByVal keyData As Keys) As Boolean
If TypeOf ActiveControl Is TabControl Then
If CBool(keyData And Keys.Control Or Keys.Tab) Then
Return True
End If
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function

IP Address Control in VB.NET

01 2015

1. Create a VB.NET UserControl Project.
2. Create 4 Text boxes in the UserControl Design Windows.
Control Name : TextBox1 , TextBox2 , TextBox3 , TextBox4
3. Copy the following code to your code window.
4. Build the output as dll.
5. Import the usercontrol in another VB.NET Project.

Code
Imports System.ComponentModel
Public Class IPAddressControl
Inherits System.Windows.Forms.UserControl

Private IPAddress As String ' = "192.168.1.1"
Public ERR_INVALID_DATA As String = "Invalid IP Address"
Dim IP1, IP2, IP3, IP4 As String
Dim AllowCharacter As Boolean = True
Property IP_Address() As String
Get
Return IPAddress
End Get
' Sets the method for setting the value of your property.
Set(ByVal Value As String)
If Value <> "" Then
IPAddress = Value ' IP1 + IP2 + IP3 + IP4

Dim ip1_pos = IPAddress.IndexOf(".")
Dim ip2_pos = IPAddress.IndexOf(".", ip1_pos + 1)
Dim ip3_pos = IPAddress.IndexOf(".", ip2_pos + 1)
IP1 = IPAddress.Substring(0, ip1_pos)
IP2 = IPAddress.Substring(ip1_pos + 1, ip2_pos - (ip1_pos + 1))
IP3 = IPAddress.Substring(ip2_pos + 1, ip3_pos - (ip2_pos + 1))
IP4 = IPAddress.Substring(ip3_pos + 1, IPAddress.Length() - (ip3_pos + 1))

TextBox1.Text = IP1
TextBox2.Text = IP2
TextBox3.Text = IP3
TextBox4.Text = IP4
Else
IPAddress = "192.168.1.1"
End If
End Set
End Property

Public Function GetIPAddress() As String
Try
Return IPAddress
Catch oExcept As Exception
' Throws the exception
Throw

End Try
End Function
Public Overrides Property Text() As String
Get
Return IPAddress
End Get
' Sets the method for setting the value of your property.
Set(ByVal Value As String)
IPAddress = Value ' IP1 + IP2 + IP3 + IP4

Dim ip1_pos = IPAddress.IndexOf(".")
Dim ip2_pos = IPAddress.IndexOf(".", ip1_pos + 1)
Dim ip3_pos = IPAddress.IndexOf(".", ip2_pos + 1)
IP1 = IPAddress.Substring(0, ip1_pos)
IP2 = IPAddress.Substring(ip1_pos + 1, ip2_pos - (ip1_pos + 1))
IP3 = IPAddress.Substring(ip2_pos + 1, ip3_pos - (ip2_pos + 1))
IP4 = IPAddress.Substring(ip3_pos + 1, IPAddress.Length() - (ip3_pos + 1))

TextBox1.Text = IP1
TextBox2.Text = IP2
TextBox3.Text = IP3
TextBox4.Text = IP4
End Set
End Property

Private Sub TextBox1_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox1.GotFocus
' TextBox1.SelectAll()
End Sub

Private Sub TextBox2_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox2.GotFocus
' TextBox2.SelectAll()
End Sub

Private Sub TextBox3_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox3.GotFocus
' TextBox3.SelectAll()
End Sub

Private Sub TextBox4_GotFocus(sender As Object, e As System.EventArgs) Handles TextBox4.GotFocus
' TextBox4.SelectAll()
End Sub

Private Sub TextBox1_LostFocus(sender As Object, e As System.EventArgs) Handles TextBox1.LostFocus
Dim value As Integer
If TextBox1.Text <> "" Then
value = Convert.ToInt32(TextBox1.Text)
If value > 255 Then
MsgBox(ERR_INVALID_DATA)
TextBox1.Focus()
TextBox1.SelectAll()
End If
End If
End Sub

Private Sub TextBox2_LostFocus(sender As Object, e As System.EventArgs) Handles TextBox2.LostFocus
Dim value As Integer
If TextBox2.Text <> "" Then
value = Convert.ToInt32(TextBox2.Text)
If value > 255 Then
MsgBox(ERR_INVALID_DATA)
TextBox2.Focus()
TextBox2.SelectAll()
End If
End If
End Sub

Private Sub TextBox3_LostFocus(sender As Object, e As System.EventArgs) Handles TextBox3.LostFocus
Dim value As Integer
If TextBox3.Text <> "" Then
value = Convert.ToInt32(TextBox3.Text)
If value > 255 Then
MsgBox(ERR_INVALID_DATA)
TextBox3.Focus()
TextBox3.SelectAll()
End If
End If
End Sub

Private Sub TextBox4_LostFocus(sender As Object, e As System.EventArgs) Handles TextBox4.LostFocus
Dim value As Integer
If TextBox4.Text <> "" Then
value = Convert.ToInt32(TextBox4.Text)
If value > 255 Then
MsgBox(ERR_INVALID_DATA)
TextBox4.Focus()
TextBox4.SelectAll()
End If
End If
End Sub

Private Sub TextBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
KeyPressEvent(TextBox1, e)
If e.KeyChar = "." Then
TextBox2.Focus()
End If
End Sub

Private Sub TextBox2_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
KeyPressEvent(TextBox2, e)
If e.KeyChar = "." Then
TextBox3.Focus()
End If
End Sub

Private Sub TextBox3_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress
KeyPressEvent(TextBox3, e)
If e.KeyChar = "." Then
TextBox4.Focus()
End If
End Sub

Private Sub TextBox4_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
KeyPressEvent(TextBox4, e)
End Sub

Sub KeyPressEvent(ByVal control As TextBox, e As System.Windows.Forms.KeyPressEventArgs)
If e.KeyChar = "." Then
e.Handled = True
End If
If AllowCharacter = False Then
e.Handled = True
End If
AllowCharacter = True
End Sub

Private Sub TextBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
NumberValidate(TextBox1, e)
End Sub
Private Sub TextBox2_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
NumberValidate(TextBox2, e)
End Sub
Private Sub TextBox3_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox3.KeyDown
NumberValidate(TextBox3, e)
End Sub
Private Sub TextBox4_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox4.KeyDown
NumberValidate(TextBox4, e)
End Sub

Sub NumberValidate(ByVal control As TextBox, e As System.Windows.Forms.KeyEventArgs)
If e.KeyData = Keys.OemPeriod Or e.KeyData = 110 Then
control.Focus()
End If
AllowCharacter = False
If e.KeyCode >= Keys.D0 And e.KeyCode <= Keys.D9 Then AllowCharacter = True End If If e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9 Then AllowCharacter = True End If If e.KeyCode = Keys.Back Then AllowCharacter = True End If If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Then AllowCharacter = True End If End Sub Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged If TextBox1.TextLength > 2 Then
TextBox2.Focus()
End If
UpdateIPAddress()
End Sub

Private Sub TextBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox2.TextChanged
If TextBox2.TextLength > 2 Then
TextBox3.Focus()
End If
UpdateIPAddress()
End Sub

Private Sub TextBox3_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox3.TextChanged
If TextBox3.TextLength > 2 Then
TextBox4.Focus()
End If
UpdateIPAddress()
End Sub
Private Sub TextBox4_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox4.TextChanged
UpdateIPAddress()
End Sub

Private Sub IPAddressControl_Leave(sender As Object, e As System.EventArgs) Handles Me.Leave
UpdateIPAddress()
End Sub

Sub UpdateIPAddress()
IPAddress = TextBox1.Text & "." & TextBox2.Text & "." & TextBox3.Text & "." & TextBox4.Text
End Sub
End Class

A Simple Thread in VB.NET

01 2015

1. Create the thread.
Private WinThread As Thread

2. Call the thread in an event.
WinThread = New Thread(AddressOf ThreadFunction)
WinThread.IsBackground = True
WinThread.Start()

3. Definition of Thread Function

Public Sub ThreadFunction()
MsgBox("Hi")
End Sub