2016-05-12 72 views
0

我一直在試圖算出這個斷斷續續,持續數週。動態變化圖像圖片框中不能正常工作

在我的VB 2010窗體應用程序中,我有一些圖片框使用拖放方法從其他圖片框中填充圖片。這沒問題,它工作正常。圖片框全部放在分組盒中。

問題是試圖在拖和拖放操作交換兩個圖片框之間的圖片。換句話說,pBox1具有image.x,pBox2具有image.y。將圖像從pBox2拖到pBox1,然後放下它; pBox1將從pBox2獲得image.y,pBox2將從pBox1獲得image.x。

有了這個例子,這裏是我到目前爲止的代碼:

Private Sub pBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pBox1.MouseDown 
    strImageSource = "pBox2" 'strImageSource is a global string variable 
    [other stuff] 
end sub 

^這節省了源圖片框的名字命名的全局字符串。

Private Sub pBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles pBox1.DragDrop 
    For Each Control as PictureBox in GroupBox1.Controls.OfType(of PictureBox)() 
    if Control.Name = strImageSource then 
     Control.Image = pBox1.Image 
    end if 
    next 

    dim imgTarget as Image = CType((e.Data.GetData(DataFormats.Bitmap)), Bitmap) 
    pBox1.image = imgTarget 
End Sub 

^這個搜索的命名由strImageSource(「pBox2」)的圖片框和副本pBox1的內容進去,然後下降,這是在pBox2到pBox1圖像。

我希望這是有道理的。

這正確地將這個圖象從pBox2到pBox1,但它並不像從pBox1切換到pBox2。 pBox2只是空白。但是,調試顯示pBox2中的圖像不是什麼;它確實包含一個位圖。它只是不可見。

現在,只是作爲一個測試,我添加了一行到每個部分,將改變圖片框的背景色:

For Each Control as PictureBox in GroupBox1.Controls.OfType(of PictureBox)() 
    if Control.Name = strImageSource then 
    Control.Image = pBox1.Image 
    Control.BackColor = color.red 
    end if 
next 

和背面的顏色是變化的。這告訴我,For Each部分正在工作 - 它找到控件並更改背景顏色。它只是沒有顯示圖像。

是否有我忽視的東西?

謝謝!

+0

我們不知道你在做什麼'DoDragDrop'。它看起來像您使用該全局字符串推出了自己的方法。 – Plutonix

回答

0

您應該調用PictureBox控件上的Control.Refresh()以更新圖像。

+0

這不提供問題的答案。一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你將能夠[評論任何職位](http://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/12335118) – isedev

+0

@isedev不,它確實回答了這個問題。不確定技術的準確性,但它符合問題描述。 – CherryDT

0

而不是使用strImageSource的,使用定義爲一個PictureBox

Private tmpPictureBox As PictureBox

一個全局變量,然後存儲PictureBox的引用,以便您可以的DragDrop設置它的圖像

Private Sub pBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pBox1.MouseDown 
    'store the picturebox reference 
    tmpPictureBox = sender 
    [other stuff] 
end sub 

Private Sub pBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles pBox1.DragDrop 
    'set the first image to the second 
    tmpPictureBox.Image = sender.image 

    'set the second image to the first 
    pBox1.image = CType((e.Data.GetData(DataFormats.Bitmap)), Bitmap) 
End Sub 
+0

謝謝,Chase。我也曾想過這個,試過了,但沒有奏效。實際上,我設置了一個臨時的圖片框,可以看到圖像正在顯示。它只是不會顯示在目標框中。 –

0

OK,這是笨。

我正在做一切正確的事情,一個真正的頭骨異常。在另一部分代碼中,莫名其妙地,我在圖像被替換後清除了內容的圖片框。它可能是我試圖做的與這個問題無關的事情的遺留物,我從來沒有糾正過它。

我對此表示歉意,並感謝所有回覆。