2014-11-01 49 views
0

我有一個如下所述的函數。如果用戶在面板上放置一個小圖像,我需要添加N個圖片框。 使用下面的代碼(在窗體上的拖放事件),我可以添加一個動態的圖片框。但我面臨兩個問題。如何將圖片添加到動態創建的Picturebox上,拖放事件

  1. 它只增加1個圖片框。如果我拖放一些東西,它只會替換Picturebox中的Picture。

  2. 我需要的行和列,以添加新的PictureBox的智慧,取決於面板


Public Function addPic(ByVal pic As Image, ByVal pName As String) 
    Dim PB As New PictureBox 
    With PB 
     .Name = pName 
     .SizeMode = PictureBoxSizeMode.CenterImage 
     .Location = New System.Drawing.Point(5, 5) 
     .Size = New Size(50, 50) 
     .BackgroundImageLayout = ImageLayout.Center 
     .Image = pic 
     ' Note you can set more of the PicBox's Properties here 
    End With 

    thePanel.Controls.Add(PB) 
    RichTextBox1.Controls.Add(PB) 
    ' This is the line that sometimes catches people out! 

    'Me.Controls.Add(PB) 
    PB.BringToFront() 

    ' Wire this control up to an appropriate event handler 
    ' AddHandler PB.Click, AddressOf MyPicClicked 

    AddHandler PB.MouseDown, AddressOf PictureBox_MouseHover 
    AddHandler PB.MouseLeave, AddressOf PictureBox_MouseLeave 
    ' AddHandler PB.Paint, AddressOf PictureBox_Paint 
    Return True 
End Function 

回答

0

你需要計算有多少圖像可以適應在面板的寬度:

numImagesWide = thePanel.ActualWidth/image.Width 
numImagesHigh = thePanel.ActualHeight/ image.Height 

你可以安全地使用整數算術來做到這一點,因爲你只需要整個數量的圖像,將fi噸。

然後,您可以在numImagesWidenumImagesHigh之間循環,每次在循環中添加一個新的PictureBox。當前方法中的所有代碼都希望進入循環 - 所以編寫一個循環並調用當前方法的包裝方法可能會更清晰。

相關問題