你如何编码在代码中创建的对象?

时间:2012-02-23 16:08:35

标签: vb.net winforms dynamic-controls

我正在使用vb.net,我无法弄清楚如何编写我在代码中创建的图片框。

2 个答案:

答案 0 :(得分:3)

创建PictureBox Class的实例并根据您的要求设置属性和事件。

按照此SO主题:C#, dynamically creating picture boxes?和Picture类文档链接来了解属性和事件。

例如:

Dim WithEvents PictureBox1 As PictureBox

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

    PictureBox1.Location = New Point(16, 48)
    PictureBox1.Text = "RadioButton3"
    PictureBox1.Size = New Size(120, 20)
    PictureBox1.Image = _
        Image.FromFile("c:\vbnet\ch07\pictureboxes\image.jpg")
    Me.Controls.Add(PictureBox1)  
End Sub

答案 1 :(得分:0)

试试这个:

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim pic As New PictureBox
    With pic
        .Name = "pic1"
        .Size = New Size(250, 250)
        .Location = New Point(50, 50)
        .SizeMode = PictureBoxSizeMode.StretchImage
        .Image = Image.FromFile("C:\flower.jpg")
    End With
    Me.Controls.Add(pic)
End Sub