กรณีที่เราไม่ได้ทำการเชื่อมต่อกับอุปกรณ์ที่มีอยู่จริงให้เลือก USA Windows Mobile 5.0 Pocket PC R2 Emulator หรือ กรณีที่ได้เชื่อมต่อแล้วก็ให้เลือก Windows Mobile 5.0 Packet PC Device R2 ได้เลย
Public Class frmMain
' By https://www.thaicreate.com (mr.win)'
Private Sub btnHome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHome.Click
Dim f As New frmHome
f.Show()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
If MessageBox.Show("Are you sure to exit?", "Confirm.", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
Application.Exit()
End If
End Sub
Private ReadOnly VK_OFF As Byte = &HDF
Private ReadOnly KEYEVENTF_KEYUP As Byte = &H2
<System.Runtime.InteropServices.DllImport("coredll.dll", SetLastError:=True)> _
Public Shared Sub keybd_event(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
End Sub
Private Sub btnShutdown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShutdown.Click
If MessageBox.Show("Are you sure to shutdown?", "Confirm.", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
Call keybd_event(VK_OFF, 0, 0, 0)
Call keybd_event(VK_OFF, 0, KEYEVENTF_KEYUP, 0)
End If
End Sub
End Class
frmHome
Imports System.Data
Imports System.Data.SqlServerCe
Imports System.Data.SqlTypes
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Public Class frmHome
' By https://www.thaicreate.com (mr.win)'
Private Sub frmHome_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BindDataGrid()
End Sub
Private Sub BindDataGrid()
Dim myConnection As SqlCeConnection
Dim dt As New DataTable
Dim Adapter As SqlCeDataAdapter
myConnection = New SqlCeConnection("Data Source =" _
+ (System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly.GetName.CodeBase ) + "\AppDatabase1.sdf;"))
myConnection.Open()
Dim myCommand As SqlCeCommand = myConnection.CreateCommand()
myCommand.CommandText = "SELECT [id], [name], [email] FROM [mytable]"
myCommand.CommandType = CommandType.Text
Adapter = New SqlCeDataAdapter(myCommand)
Adapter.Fill(dt)
myConnection.Close()
Dim tableStyle As New DataGridTableStyle()
tableStyle.MappingName = dt.TableName
Dim column As New DataGridTextBoxColumn()
column.MappingName = "id"
column.HeaderText = "ID"
column.Width = 30
tableStyle.GridColumnStyles.Add(column)
column = New DataGridTextBoxColumn()
column.MappingName = "name"
column.HeaderText = "Name"
column.Width = 40
tableStyle.GridColumnStyles.Add(column)
column = New DataGridTextBoxColumn()
column.Width = 70
column.MappingName = "email"
column.HeaderText = "Email"
tableStyle.GridColumnStyles.Add(column)
Me.dgName.DataSource = dt
Me.dgName.TableStyles.Clear()
Me.dgName.TableStyles.Add(tableStyle)
dt = Nothing
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Dim f As New frmAdd
f.Show()
End Sub
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
Dim f As New frmEdit
f._strID = Me.dgName(Me.dgName.CurrentRowIndex, 0)
f.Show()
End Sub
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDel.Click
If MessageBox.Show("Are you sure to delete?", "Confirm.", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
Dim strID As String = Me.dgName(Me.dgName.CurrentRowIndex, 0)
Dim myConnection As SqlCeConnection
myConnection = New SqlCeConnection("Data Source =" _
+ (System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly.GetName.CodeBase ) + "\AppDatabase1.sdf;"))
myConnection.Open()
Dim myCommand As SqlCeCommand = myConnection.CreateCommand()
myCommand.CommandText = "DELETE FROM [mytable] WHERE id = '" & strID & "'"
myCommand.CommandType = CommandType.Text
myCommand.ExecuteNonQuery()
myConnection.Close()
MessageBox.Show("Delete Successfully")
BindDataGrid()
End If
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Dim f As New frmMain
f.Show()
End Sub
End Class
frmAdd
Imports System.Data
Imports System.Data.SqlServerCe
Imports System.Data.SqlTypes
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Public Class frmAdd
' By https://www.thaicreate.com (mr.win)'
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If Me.txtName.Text = "" Then
MessageBox.Show("Please input (Name)")
Me.txtName.Focus()
Exit Sub
End If
If Me.txtEmail.Text = "" Then
MessageBox.Show("Please input (Email)")
Me.txtEmail.Focus()
Exit Sub
End If
Dim myConnection As SqlCeConnection
myConnection = New SqlCeConnection("Data Source =" _
+ (System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly.GetName.CodeBase ) + "\AppDatabase1.sdf;"))
myConnection.Open()
Dim myCommand As SqlCeCommand = myConnection.CreateCommand()
myCommand.CommandText = "INSERT INTO [mytable] ([name], [email]) VALUES " & _
" ('" & Me.txtName.Text & "','" & Me.txtEmail.Text & "' ) "
myCommand.CommandType = CommandType.Text
myCommand.ExecuteNonQuery()
myConnection.Close()
MessageBox.Show("Save Successfully")
Dim f As New frmHome
f.Show()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Dim f As New frmHome
f.Show()
End Sub
Private Sub txtName_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtName.GotFocus
Me.InputPanel1.Enabled = True
End Sub
Private Sub txtEmail_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEmail.GotFocus
Me.InputPanel1.Enabled = True
End Sub
Private Sub txtName_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtName.LostFocus
Me.InputPanel1.Enabled = False
End Sub
Private Sub txtEmail_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEmail.LostFocus
Me.InputPanel1.Enabled = False
End Sub
End Class
frmEdit
Imports System.Data
Imports System.Data.SqlServerCe
Imports System.Data.SqlTypes
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Public Class frmEdit
' By https://www.thaicreate.com (mr.win)'
Dim strID As String = ""
Public Property _strID() As String
Get
Return strID
End Get
Set(ByVal value As String)
strID = value
End Set
End Property
Private Sub frmEdit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myConnection As SqlCeConnection
Dim dt As New DataTable
Dim Adapter As SqlCeDataAdapter
myConnection = New SqlCeConnection("Data Source =" _
+ (System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly.GetName.CodeBase ) + "\AppDatabase1.sdf;"))
myConnection.Open()
Dim myCommand As SqlCeCommand = myConnection.CreateCommand()
myCommand.CommandText = "SELECT [id], [name], [email] FROM [mytable] WHERE id = '" & strID & "' "
myCommand.CommandType = CommandType.Text
Adapter = New SqlCeDataAdapter(myCommand)
Adapter.Fill(dt)
myConnection.Close()
If dt.Rows.Count > 0 Then
Me.txtName.Text = dt.Rows(0)("name")
Me.txtEmail.Text = dt.Rows(0)("email")
End If
dt = Nothing
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
If Me.txtName.Text = "" Then
MessageBox.Show("Please input (Name)")
Me.txtName.Focus()
Exit Sub
End If
If Me.txtEmail.Text = "" Then
MessageBox.Show("Please input (Email)")
Me.txtEmail.Focus()
Exit Sub
End If
Dim myConnection As SqlCeConnection
myConnection = New SqlCeConnection("Data Source =" _
+ (System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly.GetName.CodeBase ) + "\AppDatabase1.sdf;"))
myConnection.Open()
Dim myCommand As SqlCeCommand = myConnection.CreateCommand()
myCommand.CommandText = "UPDATE [mytable] SET " & _
" [name] = '" & Me.txtName.Text & "', [email] = '" & Me.txtEmail.Text & "' " & _
" WHERE id = '" & strID & "' "
myCommand.CommandType = CommandType.Text
myCommand.ExecuteNonQuery()
myConnection.Close()
MessageBox.Show("Update Successfully")
Dim f As New frmHome
f.Show()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Dim f As New frmHome
f.Show()
End Sub
Private Sub txtName_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtName.GotFocus
Me.InputPanel1.Enabled = True
End Sub
Private Sub txtEmail_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEmail.GotFocus
Me.InputPanel1.Enabled = True
End Sub
Private Sub txtName_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtName.LostFocus
Me.InputPanel1.Enabled = False
End Sub
Private Sub txtEmail_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEmail.LostFocus
Me.InputPanel1.Enabled = False
End Sub
End Class