【发布时间】:2023-03-15 23:52:01
【问题描述】:
【问题讨论】:
标签: vb.net loops combobox picturebox
【问题讨论】:
标签: vb.net loops combobox picturebox
使用来自资源的图像填充组合框:
For Each dicEntry As DictionaryEntry In resourceSet.OfType(Of Object)()
If TypeOf (dicEntry.Value) Is Drawing.Image Then
ComboBox1.Items.Add(dicEntry.Key.ToString())
End If
Next
选择图像并将其设置为 PictureBox:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim comboBox As ComboBox = CType(sender, ComboBox)
Dim sSelectedItem = CType(comboBox.SelectedItem, String)
Dim img As Image = CType(My.Resources.ResourceManager.GetObject(sSelectedItem), Image)
PictureBox1.BackgroundImage = img
End Sub
【讨论】:
这里有几件事要解决。最重要的是,Image 对象没有名称,因此无需列出任何内容。也许您的意思是创建 Image 对象的文件的名称,但这是不同的。除非您设置ImageLocation 属性来加载文件,否则您将无法从PictureBox 控件中获取这些文件。假设您已经这样做了,您可以像这样从PictureBoxes 获取每个文件的名称:
Dim fileNames = Controls.OfType(Of PictureBox)().
Select(Function(pb) IO.Path.GetFileName(pb.ImageLocation))
尽管如此,这仍然没有任何意义。似乎在加载Images 之前获取文件名会更有意义。不过,您的解释还不够充分,无法提供可靠的解决方案。
【讨论】:
Images 之间的关系是什么。你没有解决我在回答中提出的任何问题。