Vb.net 将图像添加到下一个可用的picturebox中
我正在通过卡片数据库执行一个卡片生成器项目,到目前为止,当我单击一行(使用datagridview)时,“image\u url”列中包含的值被打印到一个不可见的文本框中,然后用于下载该图像并在picturebox中显示 现在所有的工作都很好,但是卡片组最多有60张卡片,所以我将使用60个图片盒来打印用户选择的卡片。我一直想做的是设置一个picturecount,当他们选择一列时,数字会增加一个,如下所示:Vb.net 将图像添加到下一个可用的picturebox中,vb.net,Vb.net,我正在通过卡片数据库执行一个卡片生成器项目,到目前为止,当我单击一行(使用datagridview)时,“image\u url”列中包含的值被打印到一个不可见的文本框中,然后用于下载该图像并在picturebox中显示 现在所有的工作都很好,但是卡片组最多有60张卡片,所以我将使用60个图片盒来打印用户选择的卡片。我一直想做的是设置一个picturecount,当他们选择一列时,数字会增加一个,如下所示: picturebox(picturecount) = textbox4.text 但是
picturebox(picturecount) = textbox4.text
但是我遇到了太多的错误。您知道如何在下一个可用的图片盒中显示用户选择的卡吗?例如,如果他们选择“黑暗魔术师”三次,那么“黑暗魔术师”的图像将打印在第一个可用的三个图片框中
VB.NET:
专用异步子PictureLoader()
将imageURL设置为字符串
如果TextBox4.Text=”“,则
imageURL=dataSet.Tables(“YGO卡”)。行(行计数)。项(7)
其他的
imageURL=TextBox4.Text
如果结束
作为Net.WebClient的Dim客户端=新的Net.WebClient()
Dim ms As MemoryStream=新的MemoryStream(等待客户端。下载DataTaskAsync(新Uri(imageURL)))
使用image As image=image.FromStream(毫秒)
PictureBox1.Image?.Dispose()
PictureBox1.Image=DirectCast(Image.Clone(),Image)
终端使用
Dispose女士()
client.Dispose()
端接头
这是在datagrid中选择列时发生的事件
Dim index As Integer
index = e.RowIndex
Dim selectedrow As DataGridViewRow
selectedrow = DataGridView1.Rows(index)
' selectedrow.Cells(1) is the image_Url column
TextBox4.Text = selectedrow.Cells(1).Value.ToString()
If TextBox4.Text = "" Then
PictureBox1.Image = Nothing
' imageURL = dataSet.Tables("YGO cards").Rows(row_count).Item(7)
Else
PictureLoader()
End If
暂时忽略您特定问题的细节,并将其分解为一个通用语句。你的意思是你有一个集合,集合的大小可以根据用户的输入而增减。这是一种理想的情况,您可以通过指定列表将包含的项的数据类型来声明列表,然后根据需要添加项。因为您正在存储卡的URL,所以您将创建一个新列表(字符串): 现在,每当需要向列表中添加URL时,如果是单个URL,则调用add方法;如果是多个URL,则调用AddRange方法:
cards.Add(TextBox1.Text)
'Or
cards.AddRange({TextBox1.Text, TextBox2.Text, TextBox3.Text})
至于在PictureBox中显示图像,考虑到PictureBox类具有and(看起来您需要异步功能)方法,实际上不需要创建MemoryStream和克隆图像。但是,如果要为集合中的每个项目创建PictureBox,则需要遍历集合,创建新的PictureBox,在当前迭代的URL上调用Load或Load Async方法,然后将其添加到表单(通常是容器)中。这可以使用传统的For/Each循环完成:
'Create a placeholder variable
Dim cardPictureBox As PictureBox
'Loop through every selected card URL
For Each url As String In Cards
'Create a new PictureBox
cardPictureBox = New PictureBox() With {
.Size = New Size(100, 100)
.SizeMode = PictureBoxSizeMode.CenterImage
.WaitOnLoad = False
}
'Add the PictureBox to the Form
Me.Controls.Add(cardPictureBox)
'Load the image asynchronously
cardPictureBox.LoadAsync(url)
Next
感谢堆积大卫它的工作与更少的代码比我的!!!!!!现在我只需更改cardpicturebox代码,这样每次创建时,位置都会向右移动5!我建议将控件添加到
FlowLayoutPanel
或TableLayoutPanel
。在这种情况下,您无需设置位置
,因为容器将为您处理该问题。我喜欢下面David的动态方法。但是,如果您希望使用静态数量的控件,则可以使用controls.Find()
获取每个picturebox的引用。请看一个例子。谢谢你的建议和之前的回答。我会看看我更喜欢哪一个!
'Create a placeholder variable
Dim cardPictureBox As PictureBox
'Loop through every selected card URL
For Each url As String In Cards
'Create a new PictureBox
cardPictureBox = New PictureBox() With {
.Size = New Size(100, 100)
.SizeMode = PictureBoxSizeMode.CenterImage
.WaitOnLoad = False
}
'Add the PictureBox to the Form
Me.Controls.Add(cardPictureBox)
'Load the image asynchronously
cardPictureBox.LoadAsync(url)
Next