Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/35.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表单-动态创建大量按钮/面板/图片盒的速度很慢。如何加速?_C#_.net_Winforms - Fatal编程技术网

C# C表单-动态创建大量按钮/面板/图片盒的速度很慢。如何加速?

C# C表单-动态创建大量按钮/面板/图片盒的速度很慢。如何加速?,c#,.net,winforms,C#,.net,Winforms,由于我不是使用C表单的专家,我希望我能找到正确的方向,因为我的某段代码运行缓慢,我不知道如何加快速度 按照我现在的方式,我让用户选择图像和瓷砖大小 根据图像的尺寸和选定的瓷砖大小,我在主图像上方绘制了图像。这些图像是具有填充主图像尺寸的数字的平铺尺寸。当我点击它们时,我希望它们像按钮一样。迄今为止,这些图像都是作为面板制作的 如果每次用户更改图像或瓷砖大小时,我不必重新计算需要多少面板,那就不会太糟糕了。但就目前情况而言,它会发出大约半秒钟的嘎嘎声,尤其是在瓷砖尺寸较低的情况下 换句话说,由于我

由于我不是使用C表单的专家,我希望我能找到正确的方向,因为我的某段代码运行缓慢,我不知道如何加快速度

按照我现在的方式,我让用户选择图像和瓷砖大小

根据图像的尺寸和选定的瓷砖大小,我在主图像上方绘制了图像。这些图像是具有填充主图像尺寸的数字的平铺尺寸。当我点击它们时,我希望它们像按钮一样。迄今为止,这些图像都是作为面板制作的

如果每次用户更改图像或瓷砖大小时,我不必重新计算需要多少面板,那就不会太糟糕了。但就目前情况而言,它会发出大约半秒钟的嘎嘎声,尤其是在瓷砖尺寸较低的情况下

换句话说,由于我必须制作大量图像/控件并动态显示它们,您会建议我做些什么来提高效率

到目前为止我所拥有的一切

    pictureBox.Controls.Clear();

    //Create our large list of buttons
    for (int y = 0; y < tileCountHeight; y++)
    {
        for (int x = 0; x < tileCountWidth; x++)
        {
            Panel newPicture = new Panel();
            int locX = x * tileSize;
            int locY = y * tileSize;
            newPicture.Location = new Point(locX, locY);

            //Determine the starting image
            int dataIndex = x + (y * tileCountWidth);

            if (passabilityList.Count >= dataIndex)
            {
                passabilityList.Add(Passability.Passable);
            }

            switch (passabilityList[dataIndex])
                {
                    case Passability.Blocked:
                        newPicture.BackgroundImage = SquareImage;
                        break;

                    case Passability.HideBehind:
                        newPicture.BackgroundImage = TriangleImage;
                        break;

                    case Passability.Passable:
                        newPicture.BackgroundImage = CircleImage;
                        break;
                }

                newPicture.Width = tileSize;
                newPicture.Height = tileSize;
                newPicture.Visible = true;
                newPicture.BackgroundImageLayout = ImageLayout.Center;
                newPicture.Name = "picOptionX" + x + "Y" + y;

                pictureBox.Controls.Add(newPicture);                            
                }
            } 

你得把这个扔掉。ListView with View=Tile是一种显示大量图像的好方法。通过C窗体,我想您是在谈论Winforms吧?我建议您研究一下WPF,因为使用名为DataTemplates的概念动态创建x个按钮/控件/视图非常容易,然后您可以将其应用于ListView并绑定到对象列表。这取决于你是否愿意从头开始你的界面,但我会推荐它用于将来的开发。@Paul-这是正确的,我知道WPF,但我更了解表单,因为我不认为这会是一个如此复杂的应用程序,我和你一起去的it@Hans-我能否确定是否在ListView中单击了图像?我想我可以,但我想确定一下。使用x*y控件执行此操作会产生巨大的开销。作为一种折衷,您可以拥有绘画活动中的内容,本网站上有,涉及DrawImage方法。然后你需要自己做命中测试,但是只有矩形的数学模型很容易。