2009年5月19日 星期二

vb list\Public Class Form

Public Class Form1



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'TODO: 這行程式碼會將資料載入'ToysDataSet.toy' 資料表。您可以視需要進行移動或移除。

Me.ToyTableAdapter.Fill(Me.ToysDataSet.toy)

ShowRecord(0)

Button4.Enabled = False : Button5.Enabled = False

End Sub

Sub ShowRecord(ByVal pos As Integer)

Dim objRow As DataRow

If pos >= 0 Then

' 取得DataRow物件的記錄

objRow = Me.ToysDataSet.toy.Rows(pos)

' 顯示記錄資料

TextBox1.Text = objRow("Number")

TextBox2.Text = objRow("Name")

TextBox3.Text = objRow("Price")

TextBox4.Text = objRow("quantity")

TextBox5.Text = objRow("stockdate")



End If

End Sub



Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

ShowRecord(ListBox1.SelectedIndex) ' 顯示記錄

End Sub



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

' 設定狀態

TextBox1.ReadOnly = False : Button2.Enabled = False

Button3.Enabled = False : Button4.Enabled = True

Button5.Enabled = True

TextBox1.Text = "" : TextBox2.Text = ""

TextBox3.Text = "" : TextBox4.Text = ""

TextBox5.Text = ""

End Sub



Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim strSQL As String

If MsgBox("確定刪除記錄:" & TextBox1.Text, _

MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then

' 建立SQL刪除記錄資料

strSQL = "DELETE FROM toy WHERE Number ='"

strSQL &= TextBox1.Text & "'"

ExecuteSQL(strSQL) ' 執行SQL

Me.ToyTableAdapter.GetData()

Me.ToyTableAdapter.Fill(Me.ToysDataSet.toy)

End If

End Sub



Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim strSQL As String

' 建立SQL敘述更新資料庫記錄



strSQL = "update toy set Name='" & TextBox2.Text & "',"

strSQL &= "Price='" & TextBox3.Text & "',"

strSQL &= "quantity=" & TextBox4.Text & ","

strSQL &= "stockdate=#" & TextBox5.Text & "#"

strSQL &= " where Number='" & TextBox1.Text & "'"





'strSQL = "UPDATE toy SET "

'strSQL &= "Name='" & TextBox2.Text & "',"

'strSQL &= "quantity='" & TextBox3.Text & "',"

'strSQL &= "Price=" & TextBox4.Text & ","

'strSQL &= "Stock date=#" & TextBox5.Text & "#"

'strSQL &= " WHERE Number='" & TextBox1.Text & "'"

ExecuteSQL(strSQL) ' 執行SQL

Me.ToyTableAdapter.GetData()

Me.ToyTableAdapter.Fill(Me.ToysDataSet.toy)

End Sub



Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

Dim strSQL As String

' 建立SQL敘述新增一筆資料表記錄

strSQL = "INSERT INTO toy ([Number], Name" & _

",Price, quantity , stockdate) "



strSQL &= "VALUES ('" & TextBox1.Text & "','"

strSQL &= TextBox2.Text & "',"

strSQL &= TextBox3.Text & ","

strSQL &= TextBox4.Text & ","

'strSQL &= TextBox4.Text & ")"

strSQL &= "#" & TextBox5.Text & "#)"

ExecuteSQL(strSQL) ' 執行SQL

TextBox1.ReadOnly = True ' 重設狀態

Button4.Enabled = False : Button5.Enabled = False

Me.ToyTableAdapter.GetData()

Me.ToyTableAdapter.Fill(Me.ToysDataSet.toy)

End Sub



Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

TextBox1.ReadOnly = True ' 重設狀態

Button4.Enabled = False : Button5.Enabled = False

ShowRecord(ListBox1.SelectedIndex)

End Sub

End Class