.NET Smart Device เขียนโปรแกรมบน Smartphone, Pocket PC , Windows CE , Window Mobile 5-6, Hand Held,... |
|
|
|
.NET Smart Device Project เป็น Application ใช้สำหรับเขียนโปรแกรมเพื่อทำงานบน Smartphone, Pocket PC , Windows CE , Window Mobile 5-6, Hand Held,... ใน Visual Studio สามารถสร้าง Application ของ Smart Device นั้น สามารถทำได้อย่างง่ายได้ รูปแบบการเขียนหรือแม้แต่ Event ต่าง ๆ ก็ไม่ต่างอะไรกับ Windows Form Applciation และทั้งยังสามารถพัฒนาใช้งานได้กับหลาย ๆ Target platform เช่น
Framework : 1,2,3,4
- Pocket PC 2003
- Windows CE
- Windows Mobile 5.0 Pockget PC SDK
- Windows Mobile 5.0 Smartphone PC SDK
ในการเขียน Application ประเภท Smart Device สามารถทำการเชื่อมต่อกับอุปกรณ์ จริง หรือจะทำการใช้ตัว Emulator ที่อยู่ใน .NET Framework เพื่อทำการจำลองและใช้สำหรับ Debug โปรแกรม แต่ถ้าหากทำการเชื่อมต่อกับอุปกรณ์จริง ๆ จะต้องทำการติดตั้งตัวโปรแกรม Microsoft ActiveSync หรือใน Windows 7,Vista ขึ้นไปจะใช้ Windows Mobile Device Center สำหรับเชื่อมต่อกับอุปกรณ์
สำหรับบทความนี้มีทั้งที่เป็นภาษา VB.NET และ C# ครับ สามารถเลือกอ่านได้ตามความถนัดได้เลย
Language Code : VB.NET || C#
ในตัวอย่างนี้ผมได้ใช้ Tool ของ Visual Studio 2008 บน .NET Framework 3.5
ให้เปิด Tool ขึ้นมาพร้อมกับ New Project

Project type เลือกเป็น VB.NET หรือ C# ก็แล้วแต่ความถนัด

เลือกเป็น Device Application และเราจะต้องทดสอบบน platform ของ Windows Mobile 5.0 Pockget PC SDK

หน้าจอของ Form ที่พร้อมจเขียนโปรแกรม มีตัวอย่างเป็นหน้าจอบน Pocket PC ให้ดูเป็นตัวอย่าง

อันนี้เป็น Form Default ที่สร้างมาให้ หากไม่ต้องการก็ลบทิ้งได้เลย

สามารถเพิ่ม Form ขึ้นมาใหม่โดยคลิกขวาที่ Project -> Add -> New Item....

เลือก Windows Form และกำหนดชื่อ Form

ได้ Form ขึ้นมาใหม่แล้ว

กรณีที่เป็น VB.NET สามารถกำหนด Start Up โปรเจคได้ที่ คลิกขวาที่ Project เลือก Properties

และตรง Startup Project : เลือก form หลักที่ต้องการให้รันแรกสุดเมื่อมีการรันโปรแกรม

ทดสอบการรัน Application โดยคลิกที่ปุ่ม Start Debugging

กรณีที่เราไม่ได้ทำการเชื่อมต่อกับอุปกรณ์ที่มีอยู่จริงให้เลือก USA Windows Mobile 5.0 Pocket PC R2 Emulator หรือ กรณีที่ได้เชื่อมต่อแล้วก็ให้เลือก Windows Mobile 5.0 Packet PC Device R2 ได้เลย

ในตัวอย่างได้เลือกเป็นแบบ Emulator ซึ่งก็จะได้ผลดังรูป ซึ่งตอนนี้เป็นหน้าว่างเปล่า เพราะยังไม่มีการใส่ข้อความหรือเขียนฟังก์ชั่นเข้าไป
ในตัวอย่างนี้มีการเรียกใช้งาน Database ของ SqlServerCe ด้วย

ให้คลิกขวที่ Project -> Add -> New Item...

เลือก Database File พร้อมกับกำหนดชื่อ Database ด้วย

Database ถุกสร้างขึ้นมาแล้ว

ทดสอบการสร้าง Table โดยไปที่ Server Explorer (อยู่ด้านซ้าย) คลิกที่ Database ที่เราสร้างขึ้นมา

และภายใต้ Table ให้คลิกขวาเลือก Create Table ให้สร้างตารางชื่อ mytable โดยมีฟิว์ id,name,email (id เป็น primary key และ IdentityIncrement) และ save ถือเป็นเสร็จสิ้นการสร้าง Table
ในตัวอย่างนี้จะประกอบด้วย 4 Form คือ
1. frmMain เป็นหน้าแรกของ Application
2. frmHome เป็นหน้าหลักของโปรแกรมใช้ DataGrid สำหรับเรียกข้อมูลมาแสดง (เพื่อลิงค์ไปยังการ เพิ่ม/แก้ไข/ลบ ข้อมูล)
3. frmAdd เป็นหน้าสำหรับเพิ่มข้อมูล
4. frmEdit เป็นหน้าสำหรับแก้ไขข้อมูล
frmMain

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
ทดสอบการรันโปรแกรมโดยคิกที่ Start Debugging

เข้าสู่หน้า Main

หน้า Home

หน้า Add ข้อมูล

หร้า Edit ข้อมูล

สำหรับลบข้อมูล
สำหรับตัวอย่างทั้งหมดนี้สามารถดาวน์โหลด Code ได้จากข้างล่าง โดยมีทั้งภาษา VB.NET และ C# และบทความนี้เป็นเพียงการสอนเบื้องต้นเท่านั้น เพื่อเป็นพื้นฐานในการต่อยอดในการพัฒนาโปรแกรมที่มีความซับซ้อนและการใช้งานที่หลากหลาย
บทความที่เกี่ยวข้อง
Go to : Shared Folder or Map Drive in Emulator Smart Device Mobile Application
Go to : Smart Device Mobile Application Read and Write Text File
|
|
|
|
 |
|
|
|
|
|
|
|
|
|
|
|
By : |
TC Admin
|
|
Score Rating : |
- |
|
Create Date : |
2010-09-02 21:19:09 |
|
Download : |
(1.50 MB) |
|
|
|
|
|
|
|