ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 2809|ตอบกลับ: 1

[VB.NET] การนำภาพมาใส่ลงในหลักของตารางกริดได้แบบหลายแถว

[คัดลอกลิงก์]

308

กระทู้

498

โพสต์

5971

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
5971



อันนี้เป็นคำถามมาจากชาวต่างชาติ แอดมินคิดว่าโจทย์มันน่าสนใจดี เพราะเอาไปใช้งานได้หลายอย่างมาก เช่น แผนที่ภาษี แบบแปลนบ้าน ภาพประกอบข้อมูลรถ และอื่นๆอีกเยอะแยะ สำหรับในตอนนี้แอดมินจะให้โค้ดสำหรับการเพิ่มแถว ลบแถว และเบราซ์ไฟล์ภาพมาเก็บไว้ในหลักของตารางกริดได้แบบหลายๆแถว โดยจะเลือกภาพมาใส่ไว้ในแถวที่ถูกโฟกัสอยู่ ส่วนการจัดเก็บข้อมูลค่อยว่ากันอีกที เพราะกรณีนี้เราไม่รู้ว่าจำนวนภาพที่จะเก็บมีจำนวนเท่าไหร่ ดังนั้นในเรื่องของฐานข้อมูล เราจะต้องแยกตารางการจัดเก็บภาพไปไว้อีกตารางหนึ่ง ...

มาดูโค้ดฉบับบเต็มกันเถอะ ...
  1. Imports System.IO

  2. Public Class frmImage2DataGrid
  3.     Dim strPath As String = MyPath(Application.StartupPath)

  4.     Private Sub frmImage2DataGrid_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
  5.         '// Initialize DataGridView Control
  6.         With DataGridView1
  7.             .AllowUserToAddRows = False
  8.             .AllowUserToDeleteRows = False
  9.             .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  10.             .AutoResizeColumns()
  11.             .AllowUserToResizeColumns = True
  12.             .AllowUserToResizeRows = True
  13.         End With
  14.         DataGridView1.RowTemplate.Height = 120
  15.         '// Declare columns type.
  16.         Dim Column1 As New DataGridViewTextBoxColumn()
  17.         Dim Column2 As New DataGridViewTextBoxColumn()
  18.         '// Add new Columns
  19.         DataGridView1.Columns.AddRange(New DataGridViewColumn() { _
  20.                 Column1, Column2 _
  21.                 })
  22.         With DataGridView1
  23.             .Columns(0).Name = "Product ID"
  24.             .Columns(1).Name = "Product Name"
  25.         End With
  26.         Dim row As String() = New String() {"1", "Product 1"}
  27.         DataGridView1.Rows.Add(row)
  28.         '// Add 3th column (Index = 2), It's Image.
  29.         Dim imgCol As New DataGridViewImageColumn()
  30.         Dim img As Image = Image.FromFile(strPath & "Blank.gif")
  31.         With imgCol
  32.             .Image = img
  33.             .HeaderText = "Image"
  34.             .Name = "img"
  35.             .ImageLayout = DataGridViewImageCellLayout.Stretch
  36.             .Width = 120
  37.         End With
  38.         DataGridView1.Columns.Add(imgCol)
  39.         '//
  40.         '// Add 4th column (Index = 3), Show image path.
  41.         Dim PicturePath As New DataGridViewTextBoxColumn()
  42.         DataGridView1.Columns.Add(PicturePath)
  43.         With PicturePath
  44.             .HeaderText = "Image Path"
  45.             .ReadOnly = True
  46.             '// Normally have to hide this columns.
  47.             .Visible = True
  48.         End With
  49.         Me.DataGridView1.Focus()
  50.     End Sub

  51.     Private Sub btnBrowse_Click(sender As System.Object, e As System.EventArgs) Handles btnBrowse.Click
  52.         If Me.DataGridView1.Rows.Count = 0 Then Return
  53.         Call BrowseImage()
  54.     End Sub

  55.     Private Sub btnAddRow_Click(sender As System.Object, e As System.EventArgs) Handles btnAddRow.Click
  56.         Call AddRow()
  57.     End Sub

  58.     Private Sub btnRemoveRow_Click(sender As System.Object, e As System.EventArgs) Handles btnRemoveRow.Click
  59.         Call RemoveRow()
  60.     End Sub

  61.     Private Sub AddRow()
  62.         Dim row As String()
  63.         If Me.DataGridView1.RowCount = 0 Then
  64.             row = New String() {"1", "Product 1"}
  65.             DataGridView1.Rows.Add(row)
  66.         Else
  67.             Dim CellValue As String = DataGridView1.Rows(DataGridView1.RowCount - 1).Cells(0).Value + 1
  68.             row = New String() {CellValue, "Product " & CellValue}
  69.             DataGridView1.Rows.Add(row)
  70.         End If
  71.         Me.DataGridView1.Focus()
  72.         SendKeys.Send("^{HOME}")
  73.         SendKeys.Send("^{DOWN}")
  74.     End Sub

  75.     Private Sub RemoveRow()
  76.         '// Delete current row from DataGridView1
  77.         DataGridView1.Rows.Remove(DataGridView1.CurrentRow)
  78.     End Sub

  79.     Private Sub BrowseImage()
  80.         Dim dlgImage As OpenFileDialog = New OpenFileDialog()
  81.         ' / Open File Dialog
  82.         With dlgImage
  83.             '.InitialDirectory = strPath
  84.             .Title = "Select images"
  85.             .Filter = "Images types (*.jpg;*.png;*.gif;*.bmp)|*.jpg;*.png;*.gif;*.bmp"
  86.             .FilterIndex = 1
  87.             .RestoreDirectory = True
  88.         End With
  89.         ' Select OK after Browse ...
  90.         If dlgImage.ShowDialog() = DialogResult.OK Then
  91.             Using FS As IO.FileStream = File.Open(dlgImage.FileName, FileMode.Open)
  92.                 Dim bitmap As Bitmap = New Bitmap(FS)
  93.                 Dim currentPicture As Image = CType(bitmap, Image)
  94.                 Me.DataGridView1.CurrentRow.Cells(2).Value = currentPicture
  95.             End Using
  96.             '// Keep Image Path.
  97.             Me.DataGridView1.CurrentRow.Cells(3).Value = dlgImage.FileName
  98.         End If

  99.     End Sub

  100.     ' / --------------------------------------------------------------------------------
  101.     ' / Get my project path
  102.     ' / AppPath = C:\My Project\bin\debug
  103.     ' / Replace "\bin\debug" with ""
  104.     ' / Return : C:\My Project\
  105.     Function MyPath(ByVal AppPath As String) As String
  106.         '/ MessageBox.Show(AppPath);
  107.         AppPath = AppPath.ToLower()
  108.         '/ Return Value
  109.         MyPath = AppPath.Replace("\bin\debug", "").Replace("\bin\release", "").Replace("\bin\x86\debug", "").Replace("\bin\x86\release", "")
  110.         '// If not found folder then put the \ (BackSlash ASCII Code = 92) at the end.
  111.         If Microsoft.VisualBasic.Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
  112.     End Function

  113.     Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
  114.         Me.Close()
  115.     End Sub

  116.     Private Sub frmImage2DataGrid_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  117.         Me.Dispose()
  118.         Application.Exit()
  119.     End Sub

  120. End Class
คัดลอกไปที่คลิปบอร์ด

ดาวน์โหลดโค้ดต้นฉบับ VB.NET (2010) ได้ที่นี่ ...


ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด

0

กระทู้

4

โพสต์

142

เครดิต

Member

Rank: 2

เครดิต
142
โพสต์ 2019-11-12 13:20:10 | ดูโพสต์ทั้งหมด

ขอบคุณครับ
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-3-28 21:33 , Processed in 0.189862 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้