请问我现在利用组合框创建了序号、名称查询窗体(图1),现在打算把文件夹里的图片(图2)绑定到组合框,现在打算把文件夹里的图片(图2)绑定到组合框

img

img


请问我现在利用组合框创建了序号、名称查询窗体(图1),现在打算把文件夹里的图片(图2)绑定到组合框,一 一 对应,请问怎么处理?

你得说清楚用的什么编程语言,什么数据库,你现在代码写到哪里了。什么都没有,建议找一个例子学习下。

以下内容部分参考ChatGPT模型:


首先,需要将文件夹里的图片读入到一个数组中。可以使用VBA的Dir函数来实现。例如,下面的代码会读取文件夹路径为C:\Images\中的所有jpg文件,并将它们的完整路径存储在一个数组中。

Dim imageFiles() As String
Dim path As String
Dim fileName As String
Dim i As Integer

path = "C:\Images\"
fileName = Dir(path & "*.jpg")

i = 0
Do While fileName <> ""
    ReDim Preserve imageFiles(i)
    imageFiles(i) = path & fileName
    i = i + 1
    fileName = Dir()
Loop

然后,可以将这个数组中的图片文件路径绑定到组合框中。可以使用组合框的RowSource属性来实现。例如,下面的代码会将上面创建的数组中的每个元素都添加到组合框的RowSource属性中。

Dim imagePath As String
For i = 0 To UBound(imageFiles)
    imagePath = imageFiles(i)
    ComboBox1.AddItem imagePath
Next i

最后,需要在组合框的选项改变事件中显示选中图片。可以使用一个图像框来显示图片。例如,下面的代码会在组合框的选项改变事件中将选中的图片显示在名为Image1的图像框中。

Private Sub ComboBox1_Change()
    Dim imagePath As String
    Dim selectedImagePath As String
    
    selectedImagePath = ComboBox1.Value
    
    If selectedImagePath <> "" Then
        Image1.Picture = LoadPicture(selectedImagePath)
    End If
End Sub

综合起来,下面是完整的示例代码:

Private Sub UserForm_Initialize()
    Dim imageFiles() As String
    Dim path As String
    Dim fileName As String
    Dim i As Integer
    
    path = "C:\Images\"
    fileName = Dir(path & "*.jpg")
    
    i = 0
    Do While fileName <> ""
        ReDim Preserve imageFiles(i)
        imageFiles(i) = path & fileName
        i = i + 1
        fileName = Dir()
    Loop
    
    Dim imagePath As String
    For i = 0 To UBound(imageFiles)
        imagePath = imageFiles(i)
        ComboBox1.AddItem imagePath
    Next i
End Sub

Private Sub ComboBox1_Change()
    Dim imagePath As String
    Dim selectedImagePath As String
    
    selectedImagePath = ComboBox1.Value
    
    If selectedImagePath <> "" Then
        Image1.Picture = LoadPicture(selectedImagePath)
    End If
End Sub

如果我的建议对您有帮助、请点击采纳、祝您生活愉快