首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为EventArgs从Web返回的结果调用EventArgs

如何为EventArgs从Web返回的结果调用EventArgs
EN

Stack Overflow用户
提问于 2015-08-30 17:25:56
回答 2查看 72关注 0票数 1

我有这样的代码:

代码语言:javascript
复制
    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将其发送给方法。例如,如下所示:

代码语言:javascript
复制
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));

            }

}

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-30 17:34:50

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

代码语言:javascript
复制
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();
    }    
}
票数 0
EN

Stack Overflow用户

发布于 2015-08-30 18:18:03

好吧,我想我找到了暂时的解决办法。

代码语言:javascript
复制
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的提示!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32299289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档