2012-03-02 8 views
0

スレッドの継続:C# Invalidate troubles。クラスを作成しましたが、今はエラー2があります。埋め込みステートメントは、宣言またはラベル付きステートメントにすることはできません。そして、私は車で "車"を作成しようとしています。acar =新しい車(50,100)。それは私がエラーを受けているところです。 提案していただきありがとうございます。C#クラスとグラフィックス

class Car 
    { 
    private Pen pen1 = new Pen(Color.Blue, 2F); 
    private Pen pen2 = new Pen(Color.Green, 2F); 
    int cost = 0; 
     int x, y; 
     Graphics g; 

public Car(int x, int y) 
{ 
    this.x = x; 
    this.y = y; 
} 

public void printCar() 
{ 

    g.DrawEllipse(pen1, x, y, 30, 30); 
    g.DrawEllipse(pen1, x + 100, y, 30, 30); 
    g.DrawRectangle(pen2, x - 5, y + 50, 140, 50); 
    g.DrawLine(pen2, x + 15, y + 50, x + 30, y + 90); 
    g.DrawLine(pen2, x + 30, y + 90, x + 90, y + 90); 
    g.DrawLine(pen2, x + 90, y + 90, x + 110, y + 50); 
    // Create string to draw. 
    String drawString = "Price: " + (cost).ToString("C"); 
    // Create font and brush. 
    Font drawFont = new Font("Arial", 16); 
    SolidBrush drawBrush = new SolidBrush(Color.Black); 
    // Create point for upper-left corner of drawing. 
    PointF drawPoint = new PointF(50, 95); 
    // Draw string to screen. 
    g.DrawString(drawString, drawFont, drawBrush, drawPoint); 


} 

公共部分クラスをForm1:フォーム {

private Pen pen1 = new Pen(Color.Blue, 2F); 
    private Pen pen2 = new Pen(Color.Green, 2F); 

    private double cost ; 
    private int days = 0; 
    private double air; 
    private double automatic; 
    int count; 
    int m = 50; 
    Car aCar = new Car(50, 60); 

    public Form1() 
    { 

     InitializeComponent(); 

     days = int.Parse(textBox1.Text); 

    } 

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (comboBox1.SelectedItem.ToString() == "Van") 
     { 
      cost = 110; 
      label1.Text = "The Cost of van per day" + (cost).ToString("C"); 
      textBox1.Text = "1"; 
      textBox1.Focus(); 
     } 
     else if (comboBox1.SelectedItem.ToString() == "Car") 
     { 

      cost = 85.20; 
      label1.Text = "The Cost of car per day" + (cost).ToString("C"); 
      textBox1.Text = "1"; 
      textBox1.Focus(); 
     } 
     else 
     { 
      cost = 135; 
      label1.Text = "Van" + (cost).ToString("C"); 
      textBox1.Text = "1"; 
      textBox1.Focus(); 
     } 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     count++; 
     button1.Text = "Move"; 
     pictureBox1.Invalidate(); 
     if(count == 2) 
      button1.Text = "Reset"; 
     if (count == 3) 
     { 
      textBox1.Text = "0"; 
      count = 0; 
      comboBox1.Text = "select type of vehical"; 
      checkBox1.Checked = false; 
      checkBox2.Checked = false; 

     } 
    } 
    private void pictureBox1_Paint(object sender, PaintEventArgs e) 
    { 
     e.Graphics.Clear(Color.White); 

     if (count == 1) 

     aCar.printCar(); 

     if (count == 2) 

}

private void Form1_Load(object sender, EventArgs e) 
    { 

    } 
+0

Public void printCar(Graphic g) { // etc, etc } 

だから、あなたがそれを呼び出すときことあなたのサンプルに行が存在しないことを除いて、 'Car aCar = new Car(50,100) 'で起こっています。あなたのコードのその部分を見せてください。 –

+0

参考までに、これはディスカッションフォーラムではありません。あなたは別の質問に関連する新しい質問をしています。あなたはスレッドを続行していません。 –

答えて

0

を使用すると、Paintイベントから取得しているオブジェクト:あなたは、エラーを言う

aCar.printCar(e.Graphics); 
1

はあなたのpicturebox1_Paint方法の終わりには、if文を開きますが、実際に体が含まれていません。それは違法です:あなたは、グラフィックを渡す必要が

private void pictureBox1_Paint(object sender, PaintEventArgs e) 
{ 
    e.Graphics.Clear(Color.White); 

    if (count == 1) 

    aCar.printCar(); 

    if (count == 2) // <-- This is illegal since the if statement is just dangling 
} 
関連する問題