如何从Web服务的EventArgs返回的结果调用PaintEventArgs

时间:2015-08-30 17:25:57

标签: c# .net winforms web-services

我有这段代码:

    public partial class Form1 : Form
{
    private int size;
    public Form1()
    {
        InitializeComponent();
        size = 10;
    }

    private void runAutomat_Click(object sender, EventArgs e)
    {

        var myMatrix = new int[size][];

        for (int i = 0; i < size; i++)
        {
            myMatrix[i] = new int[size];
            for (int j = 0; j < size; j++)
                myMatrix[i][j] = 0;
        }

        var cw = new MyWebService();

        var result = cw.FillMatrix(myMatrix, size);

    }
}

接下来我想为结果绘制网格,但我不知道如何使用PaintEventArgs将其发送到方法。例如:

private void PB_Paint(object sender, PaintEventArgs e)
{
    int cellSize = 2;

      for (int x = 0; x < size; ++x)
            for (int y = 0; y < size; ++y)
            {
                if (result [y, x].state == 1)
                    e.Graphics.FillRectangle(new System.Drawing.SolidBrush(Color.Cyan), new Rectangle(y * cellSize, x * cellSize, cellSize, cellSize));
                else if (result [y, x].state == 2)
                    e.Graphics.FillRectangle(new System.Drawing.SolidBrush(Color.Yellow), new Rectangle(y * cellSize, x * cellSize, cellSize, cellSize));

            }

}

我知道这是不正确的,我需要更好的解决方案。

2 个答案:

答案 0 :(得分:0)

您可以将result的值存储为表单级变量,然后调用this.Refresh()以重新绘制表单。

public partial class Form1 : Form
{
    //Guessing what the data type is:
    private int[,] _result;

    private void runAutomat_Click(object sender, EventArgs e)
    {
        //snip
        _result = cw.FillMatrix(myMatrix, size);
        this.Refresh();
    }    
}

答案 1 :(得分:0)

好的,我认为我找到了临时解决方案。

using WFConsume.localhost;

public partial class Form1 : Form
{
    private int size;
    private localhost.Cell [][]cells;
    public Form1()
    {
        InitializeComponent();
        size = 10;
    }
}

    private void runAutomat_Click(object sender, EventArgs e)
    {

        var myMatrix = new int[size][];

        for (int i = 0; i < size; i++)
        {
            myMatrix[i] = new int[size];
            for (int j = 0; j < size; j++)
                myMatrix[i][j] = 0;
        }

        MyWebService cw = new MyWebService();

        cells = cw.FillMatrix(myMatrix, size);

    }

它在表单级别工作。感谢DavidG的提示!