2014-01-11 21 views
-1

這就是我在用戶控制代碼中所做的: 我在我的項目中添加了一個用戶控件。我如何創建一個pictureBox用戶控件?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Drawing.Drawing2D; 

namespace Find_Distance 
{ 
    public partial class pictureBox1Control : UserControl 
    { 
     public pictureBox1Control() 
     { 
      InitializeComponent(); 

      SetStyle(
      ControlStyles.AllPaintingInWmPaint | 
      ControlStyles.OptimizedDoubleBuffer | 
      ControlStyles.UserPaint | 
      ControlStyles.ResizeRedraw, true); 

     } 

     private readonly List<Ellipse> _clouds = new List<Ellipse>(); 
     public List<Ellipse> Clouds 
     { 
      get { return _clouds; } 
     } 

     protected override void OnPaint(PaintEventArgs e) 
     { 
      e.Graphics.CompositingQuality = CompositingQuality.HighQuality; 
      e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 
      e.Graphics.SmoothingMode = SmoothingMode.HighQuality; 

      foreach (var cloud in _clouds) 
      { 
       e.Graphics.FillEllipse(
        cloud.Brush, cloud.Center.X, cloud.Center.Y, 
        cloud.Diameter, cloud.Diameter); 
      } 

      base.OnPaint(e); 
     } 


     private void pictureBox1Control_Load(object sender, EventArgs e) 
     { 

     } 
    } 
} 

但是,當IM在Form1使用它例如:

pictureBox1Control.Image 

Image屬性是不存在的。 我需要使用此控件作爲常規pictureBox1和其他東西。

編輯**

新增漆事件在PictureBox:

pictureBox1 = new pictureBox1Control(); 
pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint); 

但它從來沒有得到油漆事件:

private void pictureBox1_Paint(object sender, PaintEventArgs e) 
{ 
    e.Graphics.CompositingQuality = CompositingQuality.HighQuality; 
    e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; 
    e.Graphics.SmoothingMode = SmoothingMode.HighQuality; 
    e.Graphics.Clear(Color.White); 
    e.Graphics.DrawImage(pictureBox1.Image, movingPoint); 
    CloudEnteringAlert.Paint(e.Graphics, currentfactor, distance); 
} 

爲什麼事件永遠不會火?

+0

您的班級沒有圖片屬性。所以*當然*你無法在屬性窗口或IntelliSense中找到它。添加圖片屬性。 –

回答

1

然後,你應該繼承自PictureBox類。聲明你的類如下:

public partial class MyPictureBox : PictureBox 

然後您可以創建這個類的一個實例,並使用圖像屬性(或使用旨在增加在窗體上的PictureBox):

MyPictureBox pictureBox1Control = new MyPictureBox(); 
pictureBox1Control.Image... 
+0

Cosmin我在哪裏聲明MyPictureBox公共部分類?我在pictureBox1Control代碼中聲明它? – user3163653

+0

在你寫的代碼中,你需要改變這一行:public partial class pictureBox1Control:UserControl而不是UserControl你有PictureBox – Cosmin

+0

Cosmin ok我做到了。請看看我之前做過的編輯。 – user3163653

0

你是繼承自UserControl,它不具有Image屬性。要解決這個問題,你可以從PictureBox繼承或者自己推出。

相關問題