我如何保持int变量的旧值数,并根据行数改变form1的大小

本文关键字:改变 form1 变量 int 何保持 | 更新日期: 2023-09-27 18:06:31

这是为我工作的代码:

第一个在表单的顶部1:

int firsttime;
private const int ROW_SIZE = 22;
private int NumberOfRows;
Image ima;
private System.Timers.Timer _refreshTimer;
private int _thisProcess;

在构造函数中:

firsttime = 0;
dataGridView1.BackgroundColor = System.Drawing.Color.White;
dataGridView1.Columns["Column2"].DefaultCellStyle.BackColor = System.Drawing.Color.LightGoldenrodYellow;
dataGridView1.Columns["Column3"].DefaultCellStyle.BackColor = System.Drawing.Color.LightGoldenrodYellow;
dataGridView1.AllowUserToAddRows = false;

在form1 load事件中:

private void Form1_Load(object sender, EventArgs e)
        {
            _thisProcess = Process.GetCurrentProcess().Id;
            InitializeRefreshTimer();
            PopulateApplications();
        }

然后timer init方法使用timer来更新进程列表和dataGridView1。

void InitializeRefreshTimer()
        {
            _refreshTimer = new System.Timers.Timer(5000);
            _refreshTimer.SynchronizingObject = this;
            _refreshTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimerToUpdate_Elapsed);
            _refreshTimer.Start();
        }

计时器经过的事件:

void TimerToUpdate_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            PopulateApplications();
        }
PopulateApplications方法:
void PopulateApplications()
        {
            dataGridView1.Rows.Clear();            
            foreach (Process p in Process.GetProcesses())
            {
                if (p.MainWindowTitle.Length > 1)
                {
                    var icon = Icon.ExtractAssociatedIcon(p.MainModule.FileName);
                    ima = icon.ToBitmap();
                    ima = resizeImage(ima, new Size(25, 25));
                    ima.Save(@"c:'temp'ima.jpg");
                    String status = p.Responding ? "Running" : "Not Responding";
                    dataGridView1.Rows.Add(ima,p.ProcessName, status);
                }
            }
            firsttime += 1;
            if (firsttime == 1)
            {
                NumberOfRows = dataGridView1.Rows.Count;
            }
            if (NumberOfRows != dataGridView1.Rows.Count)
            {
                int diff = dataGridView1.Rows.Count - NumberOfRows;
                this.Height = this.Height + (ROW_SIZE * diff);
                NumberOfRows = dataGridView1.Rows.Count;
            }
        }

图片大小调整方法:

public static Image resizeImage(Image imgToResize, Size size)
        {
            return (Image)(new Bitmap(imgToResize, size));
        }

在form1设计器中,我添加了9列的dataGridView1只是为了测试。在设计中,form1的尺寸为:652,358dataGridView1的大小为:624,479

现在每次添加新行时,form1 Height自动调整大小+= 22。如果一行被删除,这意味着如果我关闭任何应用程序/进程,它会自动更新dataGridView1和删除一行,form1也会自动调整大小。

用画图程序检查。打开它,关闭它几次,它工作。

我如何保持int变量的旧值数,并根据行数改变form1的大小

EDIT:

我已经用下面的代码替换了我原来的答案,因为它更好。您不需要同时使用lastValuecurrentValue变量。相反,我使用了一个叫做NumberOfRows的变量。我还添加了2个常量ROW_SIZE(每行添加到表单高度的量)和BaseFormHeight(没有行时表单的高度)。

private const int ROW_SIZE = 22;
private int NumberOfRows;
private int BaseFormHeight
void PopulateApplications()
{
    dataGridView1.Rows.Clear();
    foreach (Process p in Process.GetProcesses())
    {
        if (p.MainWindowTitle.Length > 1)
        {
            ... add a row to the data grid ...
        }
    }
    if(NumberOfRows != dataGridView1.Rows.Count)
    {
        NumberOfRows = dataGridView1.Rows.Count;
        this.Height = BaseFormHeight + (ROW_SIZE * NumberOfRows);
    }
}

Load事件处理程序中,您应该记录表单的初始高度和初始行数。

private void Form1_Load(object sender, EventArgs e)
{
    NumberOfRows = dataGridView1.Rows.Count;
    BaseFormHeight = this.Height - (NumberOfRows * ROW_SIZE);
    PopulateApplications();
}

这是我的工作代码:

第一个在表单的顶部1:

int firsttime;
private const int ROW_SIZE = 22;
private int NumberOfRows;
Image ima;
private System.Timers.Timer _refreshTimer;
private int _thisProcess;

在构造函数中:

firsttime = 0;
dataGridView1.BackgroundColor = System.Drawing.Color.White;
dataGridView1.Columns["Column2"].DefaultCellStyle.BackColor = System.Drawing.Color.LightGoldenrodYellow;
dataGridView1.Columns["Column3"].DefaultCellStyle.BackColor = System.Drawing.Color.LightGoldenrodYellow;
dataGridView1.AllowUserToAddRows = false;
In the form1 load event:
private void Form1_Load(object sender, EventArgs e)
        {
            _thisProcess = Process.GetCurrentProcess().Id;
            InitializeRefreshTimer();
            PopulateApplications();
        }

然后timer init方法使用timer来更新进程列表和dataGridView1。

void InitializeRefreshTimer()
        {
            _refreshTimer = new System.Timers.Timer(5000);
            _refreshTimer.SynchronizingObject = this;
            _refreshTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimerToUpdate_Elapsed);
            _refreshTimer.Start();
        }

计时器经过的事件:

void TimerToUpdate_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            PopulateApplications();
        }
PopulateApplications方法:
void PopulateApplications()
        {
            dataGridView1.Rows.Clear();            
            foreach (Process p in Process.GetProcesses())
            {
                if (p.MainWindowTitle.Length > 1)
                {
                    var icon = Icon.ExtractAssociatedIcon(p.MainModule.FileName);
                    ima = icon.ToBitmap();
                    ima = resizeImage(ima, new Size(25, 25));
                    ima.Save(@"c:'temp'ima.jpg");
                    String status = p.Responding ? "Running" : "Not Responding";
                    dataGridView1.Rows.Add(ima,p.ProcessName, status);
                }
            }
            firsttime += 1;
            if (firsttime == 1)
            {
                NumberOfRows = dataGridView1.Rows.Count;
            }
            if (NumberOfRows != dataGridView1.Rows.Count)
            {
                int diff = dataGridView1.Rows.Count - NumberOfRows;
                this.Height = this.Height + (ROW_SIZE * diff);
                NumberOfRows = dataGridView1.Rows.Count;
            }
        }

图片大小调整方法:

public static Image resizeImage(Image imgToResize, Size size)
        {
            return (Image)(new Bitmap(imgToResize, size));
        }

在form1设计器中,我添加了9列的dataGridView1只是为了测试。在设计器中,form1的大小为:652,358 dataGridView1的大小为:624,479

现在每次添加新行时,form1 Height自动调整大小+= 22。如果一行被删除,这意味着如果我关闭任何应用程序/进程,它会自动更新dataGridView1和删除一行,form1也会自动调整大小。

用画图程序检查。打开它,关闭它几次,它工作。