Lowprice低贵 2021-10-31 15:11 采纳率: 70%
浏览 34
已结题

C# 不使用refresh()进行窗体动态变化

描述一下我的问题:
我的winform窗体有52个picturebox,这些picturebox里的图片依次发生变化时(例如用一张图片替换当前图片),窗体不会立刻显示这一变化,而是在所有picturebox的图片都更替完毕后,显示最后的效果,而我想要的是,在每一次变化时picturebox就及时做出变化,我最先在每一步更替操作后加上refresh()刷新窗体,确实可以实现这一要求,但是问题来了,这个操作是刷新整个winform窗体,在大量图片更替时,refresh一直被执行,导致窗体卡住无法操作。请问还有哪些方法可以解决这些问题?

附上代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;



namespace poke_text
{

    public partial class Form1 : Form
    {
        PictureBox[] pb;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int Num = 0;
            Num = 53;
            pb = new PictureBox[Num];
            for (int i = 1; i < Num; i++)
            {
                pb[i] = new System.Windows.Forms.PictureBox();
                pb[i].BorderStyle = BorderStyle.FixedSingle;
                pb[i].Height = 100;
                pb[i].Width = 50;
                pb[i].Image = Image.FromFile(@"C:\Users\Administrator\source\repos\poke_text\poke_text\bin\Resources\" + i + ".JPG");
                pb[i].SizeMode = PictureBoxSizeMode.StretchImage;
                flowLayoutPanel1.Controls.Add(pb[i]);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int a = 2;

            int count = 52;

            while (a < 53)
            {
                int[] b = new int[52];
                for (int i = 0; i < 52; i += 1)
                {
                    b[i] = 0;
                }
                for (a = 2; a < 53; a += 1)
                {
                    for (int temp = a; temp < 53; temp += 1)
                    {
                        if (temp % a == 0)
                        {
                            if (b[temp - 1] == 0)
                            {
                                pb[temp].Image = Image.FromFile(@"C:\Users\Administrator\source\repos\poke_text\poke_text\bin\Resources\back.JPG");
                                count--;
                                refresh();
                                //这里的刷新窗体
                                b[temp - 1] = 1;
                            }
                            else
                            {
                                pb[temp].Image = Image.FromFile(@"C:\Users\Administrator\source\repos\poke_text\poke_text\bin\Resources\" + temp + ".JPG");
                                count++;
                                b[temp - 1] = 0;
                            }
                        }
                    }
                }
                string s1 = count.ToString();
                string s2 = Convert.ToString(count);
                textBox1.Text = s2;
            }
        }
    }
}

  • 写回答

1条回答 默认 最新

  • 真的小白啊 2021-10-31 15:52
    关注

    导致窗体卡住无法操作

    那就新开线程

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月8日
  • 已采纳回答 10月31日
  • 创建了问题 10月31日

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探