2012-06-12 40 views
1

在Form1我已經LABEL2在設計師和我加了碼:爲什麼Form1中的label2.Text沒有從新類更新?

public void lbl2(string text) 
     { 
      label2.Text = text; 
     } 

在新類的頂部i補充說:

private static AnimationEditor.Form1 fr1 = new AnimationEditor.Form1(); 

,並在新類的事件即時更新label2.Text像這樣:

int ISampleGrabberCB.BufferCB(double sampleTime, IntPtr pBuffer, int bufferLen) 
     { 
      using (var bitmap = new Bitmap(_width, _height, _width * 3, PixelFormat.Format24bppRgb, pBuffer)) 
      { 
       bitmap.RotateFlip(RotateFlipType.Rotate180FlipX); 
       if (SaveToDisc) 
       { 
        String tempFile = _outFolder + _frameId + ".bmp"; 
        if (File.Exists(tempFile)) 
        { 
         fr1.lbl2(_frameId.ToString(); 
        } 
        else 
        { 
         bitmap.Save(Path.Combine(_outFolder, _frameId + ".bmp")); 
        } 
        _frameId++; 
       } 
       else 
       { 
        if (Images == null) 
         Images = new List<Bitmap>(); 
        Images.Add((Bitmap)bitmap.Clone()); 
       } 
      } 
      return 0; 
     } 

THEL INE在執行該更新是:

fr1.lbl2(_frameId.ToString(); 

現在我在新類中的這一行上使用了斷點,並且在公共函數的label2.Text中的Form1上使用了斷點,並且我看到label2文本首先是0,然後是1,然後是2等等。

但實際上在實時時,即時通訊應用程序的運行LABEL2 dosent改變其所有的時間文本SATY作爲LABEL2

這是當我點擊它,它做新的類代碼Form1的按鈕單擊事件:

private void button5_Click(object sender, EventArgs e) 
     { 
      wmv = new Polkan.DataSource.WmvAdapter(@"d:\VIDEO0040.3gp", sf); 
      wmv.SaveToDisc = true; 
      wmv.Start(); 
      wmv.WaitUntilDone(); 
     } 
+0

你是什麼意思UI被鎖定?沒有在Form1即時更新其他標籤,如在picturebox1鼠標移動事件我更新在一個lable1鼠標移動的座標和它的工作。我還嘗試在運行時代碼中將label2文本更改爲「hi」以進行測試,並且工作正常。所以這裏有些東西是別的東西。 – user1434011

+0

不'fr1'需要成爲您正在使用的表單的實際參考?我看到'fr1 = new AnimationEditor.Form1();',它可能與您使用的表單不​​同。 – LarsTech

+0

LarsTech AnimationEditor是命名空間。我在行label2上使用了一個斷點= text;在Form1中的lbl2函數中,並看到文本已更改。變量文本正在改變0,1,2 .... – user1434011

回答

1

我認爲快速的答案是在標籤的類的引用傳遞:

private Label lbl; 

public WmvAdapter(string file, string outFolder, Label label) { 
    // yada-yada-yada 
    lbl = label; 
} 

你例程將改變爲:

if (File.Exists(tempFile)) 
{ 
    lbl.Text = _frameId.ToString(); 
} 

點擊事件:

private void button5_Click(object sender, EventArgs e) 
{ 
    wmv = new Polkan.DataSource.WmvAdapter(@"d:\VIDEO0040.3gp", sf, this.Label2); 
    wmv.SaveToDisc = true; 
    wmv.Start(); 
    wmv.WaitUntilDone(); 
} 

較長的答案是讓你的類引發一個事件,有你,收聽它。

讓你的班級意識到這種形式並不是最好的編碼習慣。

+0

拉爾斯,但在新班級我不能讓私人Form1 fr1;它劑量給我的選擇。像Form1 dosent存在。 – user1434011

+0

我編輯並將我的Form1代碼放在我的問題帖子中的Form1代碼的開頭。 – user1434011

+0

@ user1434011更新答案只是傳遞標籤而不是表單。 – LarsTech

相關問題