从另一种方法中抽出并不起作用

时间:2017-09-30 21:02:50

标签: c# winforms

在此代码中,我在Form1类中创建一个网格,并通过键值,从箭头键,我可以移动符号" @"在网格上,它工作得很好。

如果我正在调用ComputerPlayer类,则x和y坐标会像预期的那样发生变化(录制到visual studio调试器),但图形不会发生变化。

我错过了什么?

public class Form1 : Form
{

    public Form1()
    {
        Text = "Form1";
        Size = new Size(400, 400);
        Paint += new PaintEventHandler(Print);
        CenterToScreen();
    }

    public void Print(object sender, PaintEventArgs e)
    {
        Graphics graphics = e.Graphics;
        int i = 0, j;
        DoubleBuffered = true;
        Paint += new PaintEventHandler(Cursor);

        ...
    }    

    public void Cursor(object sender, PaintEventArgs e)
    {
        Font arialBold = new Font("Arial", 14.0F);
        {
            TextRenderer.DrawText(e.Graphics, ("@"), arialBold,
            new Point(x, y), Color.Red);
        }
    }
}

public class ComputerPlayer : Form1
{
    public void InitiateComputerCursor()
    {        
        if (count-- != 0)
        {
            Paint += new PaintEventHandler(ComputerCursor);
            this.Invalidate();
        }
    }

    private void ComputerCursor(object sender, PaintEventArgs e)
    {
        DoubleBuffered = true;
        Font arialBold = new Font("Arial", 14.0F);
        TextRenderer.DrawText(e.Graphics, ("@"), arialBold,
        new Point(x, y), Color.Red);
    }
}

1 个答案:

答案 0 :(得分:0)

可能是我完全错了,但看着你的代码似乎你继承自Form1,这是创建两个不同的Form1实例,但只有一个显示在屏幕上,所以对ComputerPlayer的更改将没有任何视觉效果。

可能您可以按如下方式更改代码:

cur.executescript('''
    DROP TABLE IF EXISTS New;

    CREATE TABLE New (
        "index"  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
        date   TEXT,
        location TEXT,
        location_code INTEGER)
''')

然后,无论您在Form1中初始化ComputerPlayer,都会将引用传递给构造函数:

public class ComputerPlayer
{
    private Form1 _form1;

    public ComputerPlayer(Form1 form) 
    {
         _form1 = form;
    }

    public void InitiateComputerCursor()
    {        
        if (count-- != 0)
        {
            _form.Paint += new PaintEventHandler(ComputerCursor);
            _form.Invalidate();
        }
    }

    private void ComputerCursor(object sender, PaintEventArgs e)
    {
        DoubleBuffered = true;
        Font arialBold = new Font("Arial", 14.0F);
         //You might need to replace the following line for a method in form1 that have access to TextRenderer
        _form1.TextRenderer.DrawText(e.Graphics, ("@"), arialBold,
        new Point(x, y), Color.Red);
    }
}