Sub Search() ' ค้นหา
If TbxSearch.Text = "" Then
MessageBox.Show("กรุณาระบุรหัสพนักงานก่อน !!! ", "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
Dim sql As String
Dim dr As OleDbDataReader
Dim ID As String
ID = TbxSearch.Text.Trim
sql = "select * from Employee where IDEmployee='" & ID & "'"
Dim cmd As New OleDbCommand
cmd.CommandType = CommandType.Text
cmd.CommandText = sql
cmd.Connection = connect
dr = cmd.ExecuteReader
If dr.HasRows Then
dr.Read()
TbxIDEmployee2.Text = dr.Item("IDEmployee").ToString
CMBTitleName2.Text = dr.Item("TitleName").ToString
CMBSex2.Text = dr.Item("Sex").ToString
TbxFullName2.Text = dr.Item("FullName").ToString
MIDCard2.Text = dr.Item("IDCard").ToString
CmbDay2.Text = dr.Item("a").ToString
CmbMonth2.Text = dr.Item("b").ToString
CmbYear2.Text = dr.Item("c").ToString
Dim old, old2 As Integer ' โชว์อายุหลังค้นหา
old = DateTime.Now.Year + 543
old2 = old - CmbYear2.Text
Tbxold2.Text = old2
TbxAddress2.Text = dr.Item("Address").ToString
TbxZipCode2.Text = dr.Item("ZipCode").ToString
MPhoneNumber2.Text = dr.Item("PhoneNumber").ToString
DateTimePicker2.Text = dr.Item("Start").ToString
TbxAppointment2.Text = dr.Item("Appointment").ToString
TbxSalary2.Text = dr.Item("Salary").ToString
CMBEducation2.Text = dr.Item("Education").ToString
TbxFaculty2.Text = dr.Item("Faculty").ToString
TbxBranch2.Text = dr.Item("Branch").ToString
Dim imgByte() As Byte = dataset.Tables("Employee").Rows((Val((Mid(ID, 2, 4)) - 1)))("Picture") ' test อยู่
Dim imgstremam As New MemoryStream(imgByte)
Picture2.Image = Image.FromStream(imgstremam)
Dim index As Integer = (Val((Mid(ID, 2, 4)) - 1))
ListBox1.SelectedIndex = index
updateStatus()
'TbxSearch.Text = ""
Else
MessageBox.Show("ไม่พบพนักงานในระบบ", "ค้นหา", MessageBoxButtons.OK, MessageBoxIcon.Error)
TbxSearch.SelectAll()
End If
End Sub
นี่เป็นโค๊ต Listbox ครับ Code (VB.NET)
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim index As Integer = 0
index = ListBox1.SelectedIndex ' Check Index ของ List box ถ้าอยู่ใน Index ไหนให้ส่งค่าไปยังโปรแกรมย่อย
Me.BindingContext(dataset, "Employee").Position = index
If index = -1 Then
Return
Else
Showpicture(index) ' รับค่า Index จาก Listbox
_position = index '
updateStatus()
End If
If dataset.Tables("Employee").Rows(index)("c") Is DBNull.Value Then ' การคำนวณอายุ
Tbxold2.Text = ""
Return
Else
Dim old As Integer
old = (DateTime.Now.Year + 543)
Tbxold2.Text = (old - CmbYear2.Text)
End If
End Sub
Sub BindingsShowE() ' โปรแกรมย่อยใช้โชว์ข้อมูลพนักงานหน้า Tab1
Dim sql As String = "Select * from Employee"
cmd = New OleDbCommand(sql, connect)
adapter = New OleDbDataAdapter(cmd)
dataset = New DataSet
adapter.Fill(dataset, "Employee")
Dim name As String
ListBox1.Items.Clear()
For i = 0 To dataset.Tables("Employee").Rows.Count - 1
name = dataset.Tables("Employee").Rows(i)("TitleName")
name &= (" ") & dataset.Tables("Employee").Rows(i)("FullName")
ListBox1.Items.Add(name)
Next
TbxIDEmployee2.DataBindings.Add("text", dataset, "Employee.IDEmployee")
CMBTitleName2.DataBindings.Add("text", dataset, "Employee.TitleName")
CMBSex2.DataBindings.Add("text", dataset, "Employee.Sex")
TbxFullName2.DataBindings.Add("text", dataset, "Employee.FullName")
MIDCard2.DataBindings.Add("text", dataset, "Employee.IDCard")
CmbDay2.DataBindings.Add("text", dataset, "Employee.a")
CmbMonth2.DataBindings.Add("text", dataset, "Employee.b")
CmbYear2.DataBindings.Add("text", dataset, "Employee.c")
Dim old, old2 As Integer ' โชว์อายุตอน เข้าไปเก็บ
old = DateTime.Now.Year + 543
old2 = old - CmbYear2.Text
Tbxold2.Text = old2
DateTimePicker2.DataBindings.Add(New Binding("Value", dataset, "Employee.Start"))
TbxAddress2.DataBindings.Add("text", dataset, "Employee.Address")
TbxZipCode2.DataBindings.Add("text", dataset, "Employee.ZipCode")
MPhoneNumber2.DataBindings.Add("text", dataset, "Employee.PhoneNumber")
TbxAppointment2.DataBindings.Add("text", dataset, "Employee.Appointment")
TbxSalary2.DataBindings.Add("text", dataset, "Employee.Salary")
CMBEducation2.DataBindings.Add("text", dataset, "Employee.Education")
TbxFaculty2.DataBindings.Add("text", dataset, "Employee.Faculty")
TbxBranch2.DataBindings.Add("text", dataset, "Employee.Branch")
If dataset.Tables("Employee").Rows(0)("Picture") IsNot DBNull.Value Then
Dim imgByte() As Byte = dataset.Tables("Employee").Rows(0)("Picture")
Dim imgstremam As New MemoryStream(imgByte)
Picture2.Image = Image.FromStream(imgstremam)
ElseIf dataset.Tables("Employee").Rows(0)("Picture") Is DBNull.Value Then
Picture2.Image = Nothing
End If
Me.BindingContext(dataset, "Employee").Position = 0
updateStatus()
End Sub