简体   繁体   English

如何检查和禁用 C# windows 表单中的 flowLayoutPanel 控件?

[英]How to check and disable flowLayoutPanel controls in C# windows form?

I just want to make sure that the burst time input is greater than 0 and i wanna disable the waiting time control since its only for output.我只想确保突发时间输入大于 0,并且我想禁用等待时间控制,因为它仅适用于 output。

在此处输入图像描述

I'm a complete newbie, please advise how do I implement this on the code below?我是一个完整的新手,请告诉我如何在下面的代码中实现它? thanks a lot!多谢!

Here is the code from the form above:这是上面表格中的代码:

namespace CPU_Scheduling
{
public partial class FCFS : Form
{
   
    public FCFS()
    {
        InitializeComponent();
        
        picBusy.Hide();
        picWaiting.Show();
       
    }
   
   
    public bool ran = false;
    public int Numpro;

    public int Max;

    public int Min;
   
    private void label3_Click(object sender, EventArgs e)
    {

    }

    private void label12_Click(object sender, EventArgs e)
    {

    }
    Random rand = new Random();
    private int Normal(double mean, double stdDev, int max, int min)
    {
        int k = max + 1;
        while (k > max || k < min)
        {
            double u1 = 1.0 - rand.NextDouble(); //uniform(0,1] random doubles
            double u2 = 1.0 - rand.NextDouble();
            double randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) *
                         Math.Sin(2.0 * Math.PI * u2); //random normal(0,1)


            double randNormal = mean + stdDev * randStdNormal; //random normal(mean,stdDev^2)
            k = (int)Math.Floor(randNormal);

        }

        return k;

    }
    Process[] prolist;
    Process[] dosched;
    public void populate()
    {   prolist = new Process[Numpro];           
        double mean = (double)(Max + Min)/(double) 2;
        double stdDev = (double)(Max - Min) / (double)6;
        
        for (int i = 0; i < Numpro; i++)

        {   
            prolist[i] = new Process();
            prolist[i].Num = i;
            if (ran == true)
            {
                prolist[i].Arrival = Normal(mean, stdDev, Max, Min);
                prolist[i].Burst = Normal(mean, stdDev, Max, Min+1);
            }
            else
            {
                prolist[i].Arrival = 0;
                prolist[i].Burst = 1;
            }
            flowLayoutPanel1.Controls.Add(prolist[i]);
        }
        
    }
    int totalwait=0;
    int totalturnAround=0;
    public void Sched()
    {
        dosched = new Process[Numpro];
        dosched = prolist;
        dosched.Count();
        for (int k = 0; k < Numpro; k++)
        {
            for (int i = k + 1; i < Numpro; i++)
            {
                if (dosched[k].Arrival > dosched[i].Arrival ||((dosched[i].Arrival==dosched[k].Arrival) && (dosched[k].Num>dosched[i].Num )))
                {
                    Process temp = new Process();
                    temp = dosched[i];
                    dosched[i] = dosched[k];
                    dosched[k] = temp;
                }
            }
        }

        int clock = 0;
        totalwait = 0; totalturnAround = 0;
        for (int i = 0; i < Numpro; i++)
        {
            if (dosched[i].Arrival >= clock)
            {
                dosched[i].Start = dosched[i].Arrival;
                clock += dosched[i].Start;
                clock += dosched[i].Burst;

            }
            else
            {
                if (i > 0)
                    dosched[i].Start = dosched[i - 1].End;
                
                clock += dosched[i].Burst;
            }
            if (dosched[i].Start > dosched[i].Arrival)
                dosched[i].WaitT = dosched[i].Start - dosched[i].Arrival;
            else dosched[i].WaitT = 0;
            dosched[i].End = dosched[i].Start + dosched[i].Burst;
            dosched[i].Turnaround = dosched[i].WaitT + dosched[i].Burst;
            totalwait += dosched[i].WaitT;
            totalturnAround += dosched[i].Turnaround;
        }
        
        for(int i=0;i<Numpro;i++)
        {   for(int k=0;k<Numpro;k++)
                if (dosched[i].Num == prolist[k].Num)
                {
                    prolist[k].WaitT = dosched[i].WaitT;
                }
        }
        
    }
    
    private void LoadBar(Process p1)
    {   
        Process k = new Process();
        k = p1;
        Label k1 = new Label();
        k1.AutoSize = true;
        Label k2 = new Label();
        k2.AutoSize = true;
        k1.Text = "P" + p1.Num.ToString();
        k2.Text = p1.Start.ToString();
        LoadBar newload = new LoadBar();
        newload.Max = k.Burst;
        int i = tableLayoutPanel1.ColumnCount++;
        newload.load();
       
        tableLayoutPanel1.Controls.Add(k1, i - 1, 0);
        tableLayoutPanel1.Controls.Add(newload.b1, i - 1, 1);
        tableLayoutPanel1.Controls.Add(k2, i - 1, 2);
        
        picBusy.Show();
        picWaiting.Hide();
        k.loadprogress();
        
    }
   
    int orderpro=0;
    int counttime=0;
    int endtime = 0;
    bool stop;
    private void btnStart_Click(object sender, EventArgs e)
    {
        flowLayoutPanel1.Enabled = false;
        timer1.Tick -= timer1_Tick;
        Simulate();
    }
    private void Simulate()
    {
        Sched();
        
        counttime = 0;
        for (int i = 0; i < Numpro; i++)
        {
            if (endtime < prolist[i].End) endtime = prolist[i].End;
        }
        timer1.Tick += timer1_Tick;
        timer1.Interval = 1000;
       
        timer1.Start();
    }
    

    
    int wait = 0;
    private void timer1_Tick(object sender, EventArgs e)
    {

        
        string waitpro = "";

            lbClock.Text = (counttime).ToString();
            for (int i = 0; i < Numpro; i++)
            {
                if (counttime == prolist[i].Start)
                {
                    wait = prolist[i].End;
                    prolist[i].setWait(prolist[i].WaitT);
                    LoadBar(prolist[i]);
                    lbCurrent.Text = "P" + prolist[i].Num.ToString();

                }
            }
            int numqueue = 0;
            for (int k = 0; k < Numpro; k++)
                if ((counttime > dosched[k].Arrival) && (counttime < dosched[k].Start))
                { waitpro += "P" + dosched[k].Num.ToString() + "|"; numqueue++; }
            lbWait.Text = waitpro; lbQueue.Text = numqueue.ToString();
            if (counttime < wait ) { lbStatus.Text = "Busy"; picBusy.Show(); picWaiting.Hide(); }
            else if(counttime<endtime) { lbStatus.Text = "Idle"; picBusy.Hide(); picWaiting.Show(); }


            if (counttime == endtime+1) { timer1.Stop(); lbStatus.Text = "Idle"; picBusy.Hide(); picWaiting.Show();lbCurrent.Text = "non";
                lbWaitT.Text = Math.Round((double)totalwait / (double)Numpro, 2).ToString();
                lbTurn.Text = Math.Round((double)totalturnAround / (double)Numpro, 2).ToString();
            }
            counttime++;
        
       
    }
    
    private void btnRestart_Click(object sender, EventArgs e)
    {
        flowLayoutPanel1.Enabled = true;
        timer1.Tick -= timer1_Tick;
        picBusy.Hide();picWaiting.Show();
        lbClock.Text = "0";
        lbQueue.Text = "0";
        lbStatus.Text = "Idle";
        lbCurrent.Text = "P";
        lbTurn.Text = "0";
        lbWaitT.Text = "0";

        flowLayoutPanel1.Controls.Clear();
        for (int i = tableLayoutPanel1.Controls.Count - 1; i >= 0; --i)
            tableLayoutPanel1.Controls[i].Dispose();

        tableLayoutPanel1.Controls.Clear();
        tableLayoutPanel1.ColumnStyles.Clear();
        tableLayoutPanel1.ColumnCount = 1;

      
        for (int i = 0; i < Numpro; i++)

        {
            prolist[i].stop();
            
            flowLayoutPanel1.Controls.Add(prolist[i]);
        }
        
        //flowLayoutPanel1.Controls.Clear();



    }

    private void FCFS_Load(object sender, EventArgs e)
    {
        this.FormClosed += new FormClosedEventHandler(formclose);

    }
    private void formclose(object sender, EventArgs e)
    {
        EntryForm k = new EntryForm();
        k.Show();
    }
}
}

The complete C# program from GitHub: link来自 GitHub 的完整 C# 程序:链接

at the start button click, I want it to check if the burst time inputs is greater than 0 and if not, stop the method execution.在开始按钮单击时,我希望它检查突发时间输入是否大于 0,如果不是,则停止方法执行。 I know how to check this on datagridview but the creator used flowlayoutpanel.我知道如何在 datagridview 上检查这一点,但创建者使用了 flowlayoutpanel。

Well, you already have an array, Process[] prolist;好吧,你已经有了一个数组, Process[] prolist; , that holds all the Process instances, just iterate over that. ,它包含所有 Process 实例,只需对其进行迭代。 The fact that they are displayed in a FlowLayoutPanel is irrelevant.它们显示在 FlowLayoutPanel 中的事实是无关紧要的。

For example:例如:

private void btnStart_Click(object sender, EventArgs e)
{
    for(int i=0; i<prolist.Length; i++)
    {
        if (prolist[i] != null && prolist[i].Burst <= 0)
        {
            MessageBox.Show("All burst times must be greater than zero");
            return; // exit the method
        }
    }     

    flowLayoutPanel1.Enabled = false;
    timer1.Tick -= timer1_Tick;
    Simulate();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM