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

本文关键字:服务 Web EventArgs 返回 结果 PaintEventArgs 何调用 调用 | 更新日期: 2023-09-27 18:10:22

我有这样的代码:

    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));
            }
}

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

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

您可以将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();
    }    
}

好的,我想我找到了一个临时的解决方案。

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的建议!