Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 矩形没有';调用方法时,不能向上移动_C#_Winforms_Graphics - Fatal编程技术网

C# 矩形没有';调用方法时,不能向上移动

C# 矩形没有';调用方法时,不能向上移动,c#,winforms,graphics,C#,Winforms,Graphics,我现在对图形感兴趣,我正在尝试创建一个随着关键事件移动的矩形。 代码如下: 类来创建矩形: using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Snake { class RectangleClass { // private Rectangle

我现在对图形感兴趣,我正在尝试创建一个随着关键事件移动的矩形。 代码如下:

类来创建矩形:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Snake
{
class RectangleClass
{
   // private Rectangle _rectangle;
    private int _positionX;
    private int _positionY;
    private int _height;
    private int _width;



    public RectangleClass(int positionX, int positionY, int height, int width)
    {
        _positionX = positionX;
        _positionY = positionY;
        _height = height;
        _width = width;
    }
    public int PositionX
    {
        get
        {
            return _positionX;
        }
        set
        {
            _positionX = value;
        }
    }

    public int PositionY
    {
        get
        {
            return _positionY;
        }
        set
        {
            _positionY = value;
        }
    }

    public int Height
    {
        get
        {
            return _height;
        }
        set
        {
            _height = value;
        }
    }

    public int Width
    {
        get
        {
            return _width;
        }
        set
        {
            _width = value;
        }
    }


    public Rectangle CreateRectangle()
    {
         Rectangle rectangle = new Rectangle(PositionX, PositionY, Width, Height);
            return rectangle;
    }



    public int MoveYUpWard()
    {
        return PositionY += 10;
    }

    public int MoveYDownWard()
    {
        return PositionY -= 10;
    }


  }
}
及表格:

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


    namespace Snake
    {
        public partial class Form1 : Form
        {
            RectangleClass rc;

            public Form1()
            {



                InitializeComponent();
                rc = new RectangleClass(30, 90, 50, 50);

            }

            private Graphics g;



            private void Form1_Paint(object sender, PaintEventArgs e)
            {




                if (g == null)
                {
                    g = this.CreateGraphics();
                }
                Pen pen = new Pen(Color.Blue, 10);
                g.DrawRectangle(pen, rc.CreateRectangle());

            }



            private void Form1_KeyUp(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Up)
                {
                    MessageBox.Show("UP");
                    //Sets position y to 100
                    rc.MoveYUpWard();




                }
            }
我已经调试了代码,当按下箭头键时,MoveYUpWard()方法确实会更改矩形的位置,但矩形的位置是相同的


我将非常感谢您的帮助。

问题可能在这里。试试这个代码

public int MoveYUpWard()
{
    PositionY -= 10;
    return PositionY;
}

public int MoveYDownWard()
{
    PositionY += 10;
    return PositionY;
}


private void Form1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Up)
    {
        MessageBox.Show("UP");
        //Sets position y to 100
        rc.MoveYUpWard();
        this.Invalidate();
    }
}

不要存储
图形
实例。相反,请使用
e.Graphics
。是否确定正在触发绘制事件?表单不知道如何重新绘制-在其上调用Invalidate。好的,我将尝试。它成功了,非常感谢。:)@SLaks为什么我不能那样做?有什么原因吗?我已经在上面实现了singleton。是的,我在执行程序后意识到了这一点。但这并不是主要问题。无论如何谢谢你的帮助