2011-02-02 2 views
12
으로 표시

PictureBox으로 C#으로 이미지를 표시하고 싶습니다. 나는 contians가 pictureBox 인 타이머와 타이머를 만들었습니다. 하지만 그 때 아무것도에서 표시 개체를 만들 수 있습니다.이미지를 C#

어떻게해야합니까?

나는 정확하게 timer1을 사용합니까? Timer가 활성화되어 있는지

public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     c1 c = new c1(); 
     c.create_move(1); 
    } 

} 

class c1 { 

    PictureBox p = new PictureBox(); 
    Timer timer1 = new Timer(); 

    public void create_move(int i){ 

     p.ImageLocation = "1.png"; 
     p.Location = new Point(50, 50 + (i - 1) * 50); 

     timer1.Start(); 
     timer1.Interval = 15; 
     timer1.Tick += new EventHandler(timer_Tick); 
    } 


    private int k = 0; 
    void timer_Tick(object sender, EventArgs e) 
    { 
     // some code. this part work outside the class c1 properly. 
     ... 

    } 

답변

11

그림 상자를 Form에 추가해야합니다. Form.Controls.Add() 방법을 확인하십시오.

2

확인 :

여기 내 코드입니다. Start() 메서드를 호출하기 전에 timer1.Enabled = true;을해야 할 수도 있습니다.

5

귀하의 picturebox가 현재 양식에 추가되지 않았기 때문입니다.

Form.Controls 속성이 있으며 Add() 메서드가 있습니다.

+0

어디로 넣어야합니까? move_create() 메서드에서 Form1.Controls.Add (p)를 작성했지만 오류가 발생했습니다. –

+0

양식에 대한 참조가 필요합니다. 클래스의 생성자에서 속성을 전달하십시오. – Shimrod

2

우선, 양식에 pictureBox를 추가해야합니다 (표시하려는 경우). 어쨌든 - 나는 userControl을 만들도록/권하고 싶습니다. 새 컨트롤에 PictureBox을 추가하고 TimerControl을 추가하십시오.

public partial class MovieControl : UserControl 
{ 
    // PictureBox and Timer added in designer! 

    public MovieControl() 
    { 
     InitializeComponent(); 
    } 

    public void CreateMovie(int i) 
    { 
     pictureBox1.ImageLocation = "1.png"; 
     pictureBox1.Location = new Point(50, 50 + (i - 1) * 50); 

     // set Interval BEFORE starting timer! 
     timer1.Interval = 15; 
     timer1.Start(); 
     timer1.Tick += new EventHandler(timer1_Tick); 
    } 

    void timer1_Tick(object sender, EventArgs e) 
    { 
     // some code. this part work outside 
    } 
} 

이 새 컨트롤을 forms.controls 컬렉션에 추가하면됩니다.

class Form1 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     MovieControl mc = new MovieControl(); 
     mc.CreateMovie(1); 
     this.Controls.Add(mc); /// VITAL!! 
    } 
} 
+1

+1 이것은 상황에 맞는 좋은 방법이며 타이머가있는 컨트롤에 필수적인 메소드가 없습니다. – honibis

관련 문제