Option Explicit
Private mImageID As Variant
Private mImageCtl As Control
Private mImageFolderName As String
Public Property Let ImageID(vID As Variant)
mImageID = vID
End Property
Public Property Get ImageID() As Variant
ImageID = mImageID
End Property
Public Property Set ImageControl(ctl As Control)
Set mImageCtl = ctl
End Property
Public Property Get ImageControl() As Control
ImageControl = mImageCtl
End Property
Public Property Let ImageFolderName(sName As String)
mImageFolderName = sName
End Property
Public Property Get ImageFolderName() As String
ImageFolderName = mImageFolderName
End Property
Public Sub synImage()
On Error GoTo EH
Dim defaultImage As String, savedImagePath As String
defaultImage = ThisWorkbook.Path & "\" & mImageFolderName & "\" & "placeholder.bmp"
savedImagePath = GetImagePath
If checkFileExist(savedImagePath) = False Then
mImageCtl.Picture = LoadPicture(defaultImage)
Else
mImageCtl.Picture = LoadPicture(savedImagePath)
End If
Exit Sub
EH:
Select Case Err.Number
Case 53, 76
'MsgBox "Không tìm thay file anh", vbExclamation, AppName
Case Else
MsgBox "Err: " & Err.Number & vbCrLf & "Err content: " & Err.Description, vbCritical, AppName & " - SynImage"
End Select
End Sub
Public Function GetImagePath() As String 'Dung variant vì imageID có the là Number, Text
GetImagePath = ThisWorkbook.Path & "\" & mImageFolderName & "\" & mImageID & ".bmp"
End Function
Public Sub addImage()
Dim strNewPicDest As String, sourceImagePath As String
sourceImagePath = openImageFile
If sourceImagePath = "" Then Exit Sub
strNewPicDest = ThisWorkbook.Path & "\" & mImageFolderName & "\" & mImageID & ".bmp"
FSO_FileCopy sourceImagePath, strNewPicDest
synImage
End Sub
Public Function deleteImage()
If checkFileExist(GetImagePath) = False Then Exit Function
If MsgBox("Ban co chac muon xoa hình [" & ImageID & "]?", vbCritical + vbYesNo) = vbYes Then
Kill GetImagePath
synImage
End If
End Function
Function checkCreateFolder(sFolderPath As String) As Boolean
Dim FSO As Object
On Error GoTo HandleError
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(sFolderPath) Then
checkCreateFolder = True
Else
FSO.CreateFolder (sFolderPath)
'MsgBox "It has been created.", vbInformation, "Create folder"
checkCreateFolder = True
End If
HandleExit:
Exit Function
HandleError:
checkCreateFolder = False
MsgBox "Ma loi: " & Err.Number & vbCrLf & "Noi dung: " & Err.Description, vbCritical, "Check and create folder"
Resume Next
End Function
Public Function FSO_FileCopy(ByVal sSource As String, ByVal sDest As String) As Boolean
'On Error GoTo Error_Handler
Dim oFSO As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
Call oFSO.CopyFile(sSource, sDest, True)
FSO_FileCopy = True
Error_Handler_Exit:
On Error Resume Next
If Not oFSO Is Nothing Then Set oFSO = Nothing
Exit Function
Error_Handler:
MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
"Error Number: " & Err.Number & vbCrLf & _
"Error Source: FSO_FileCopy" & vbCrLf & _
"Error Description: " & Err.Description & _
Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
, vbOKOnly + vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit
End Function
Function openImageFile() As String
With Application.FileDialog(msoFileDialogFilePicker)
.Filters.Clear
.Filters.Add "Image Files", "*.bmp", 1
.Title = "Choose an Image (.BMP) file:"
.AllowMultiSelect = False
.InitialFileName = ThisWorkbook.Path & "\" & ImageFolderName
If .Show = True Then
openImageFile = .SelectedItems(1)
End If
End With
End Function
Private Sub Class_Terminate()
On Error Resume Next
'...
End Sub