C# 中的文本框未更新

发布于 2024-10-06 06:23:33 字数 2699 浏览 4 评论 0原文

具体看Customer类中的arrive方法。我正在使用 for 循环创建客户类的实例,当我尝试将它们的到达时间写入文本框(仅用于测试目的)时,文本框不会更新。这是为什么呢?

这只是我的计算机课程的一个小型模拟项目。它还处于早期阶段,很多地方可能都是错误的!

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;
using System.Threading;

namespace QueueSimulation
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("The form has loaded");
        }

        public void goButton_Click(object sender, EventArgs e)
        {
            Initialisers init = new Initialisers();

            Customer customer = new Customer();

            customer.Arrive();
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            // put code here to break out of the program
        }
    }

    public class Customer : Initialisers
    {
        int waitingTime;
        int arrivalTime;
        int arrivalInterval;

        Initialisers init = new Initialisers();


        public void Arrive()
        {
            Customer[] customer = new Customer[1000];
            int counter = 0;
            for (int i = 1; i <= 10; i++)
            {
                customer[i] = new Customer();
                customer[i].TimeArrived();
                displayArrival.Text = displayArrival.Text + customer[i].TimeArrived().ToString();
                // Implement something to either show the time in the queue if needed
                Thread.Sleep(init.CustomerArriveTime*100);
            }
            MessageBox.Show("All of the customers have arrived");
        }

        public string TimeArrived()
        {
            return Convert.ToString(DateTime.Now);
        }

        public void Leave()
        {

        }

        public void GetServed()
        {

        }
    }

    public class Server
    {
        bool servingStatus;
        int servingTime;

        public void Serve()
        {

        }
    }

    public class Initialisers : Form1
    {
        private int cust_no = 3;

        public int CustomerArriveTime
        {
            get
            {
                return cust_no;
            }
            set
            {
                cust_no = value;
            }
        }

        private int s_time = 4;

        public int serveTime
        {
            get
            {
                return s_time;
            }
            set
            {
                s_time = value;
            }
        }
    }
}

Specifically looking at the arrive method in the Customer class. I am using a for loop to create instances of the customer class, and when I try to write out their arrival times to a textBox (Just for testing purposes) the text box does not update. Why is this?

This is just a small simulation project for my Computing class. It is in its early stages, and is probably wrong in a lot of places!

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;
using System.Threading;

namespace QueueSimulation
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            MessageBox.Show("The form has loaded");
        }

        public void goButton_Click(object sender, EventArgs e)
        {
            Initialisers init = new Initialisers();

            Customer customer = new Customer();

            customer.Arrive();
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            // put code here to break out of the program
        }
    }

    public class Customer : Initialisers
    {
        int waitingTime;
        int arrivalTime;
        int arrivalInterval;

        Initialisers init = new Initialisers();


        public void Arrive()
        {
            Customer[] customer = new Customer[1000];
            int counter = 0;
            for (int i = 1; i <= 10; i++)
            {
                customer[i] = new Customer();
                customer[i].TimeArrived();
                displayArrival.Text = displayArrival.Text + customer[i].TimeArrived().ToString();
                // Implement something to either show the time in the queue if needed
                Thread.Sleep(init.CustomerArriveTime*100);
            }
            MessageBox.Show("All of the customers have arrived");
        }

        public string TimeArrived()
        {
            return Convert.ToString(DateTime.Now);
        }

        public void Leave()
        {

        }

        public void GetServed()
        {

        }
    }

    public class Server
    {
        bool servingStatus;
        int servingTime;

        public void Serve()
        {

        }
    }

    public class Initialisers : Form1
    {
        private int cust_no = 3;

        public int CustomerArriveTime
        {
            get
            {
                return cust_no;
            }
            set
            {
                cust_no = value;
            }
        }

        private int s_time = 4;

        public int serveTime
        {
            get
            {
                return s_time;
            }
            set
            {
                s_time = value;
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌若浮生 2024-10-13 06:23:33

将在 Form1 上创建的文本框对象的实例传递给 Arrive。

public void Arrive(TextBox displayArrival)

为什么要在Initialiserz中继承Form1?在这种情况下,最好将引用传递给 Form1 而不是继承。

Pass to the Arrive the instance of the textbox object created on your Form1.

public void Arrive(TextBox displayArrival)

Why are you inheriting the Form1 in Initialiserz? It's better to pass the reference to Form1 instead of inheritance in this case.

一身仙ぐ女味 2024-10-13 06:23:33

这似乎过于复杂。尝试对现实世界进行建模。什么是Initialisers,为什么有继承树:Customer >初始化器>表格1?

您的客户正在写入自己的文本框,而不是您正在查看的文本框(表单中可见的文本框)。

为什么不使用 Arrive 方法将私有字段设置为 DateTime.Now。然后,询问客户其 TimeArrived,这会返回此字段。在您的表单中,根据循环中的需要调用这些方法。

这也将命令(Arrive)与查询(TimeArrived)分开+使您的继承更加合乎逻辑。

您甚至可能不再需要初始化程序。并且不要让 Customer 继承 Form,因为 Customer 不是 Form。

This seems overly complex. Try to model the real world. What is Initialisers, and why do you have an inheritance tree: Customer > Initialisers > Form1?

You're customer is writing to its own TextBox, instead of the TextBox you're looking at (the one from the Form that is visible).

Why not have a method Arrive that sets a private field to DateTime.Now. Then, ask the Customer its TimeArrived, which returns this field. In your Form, call these methods as much as needed in your loop.

This also seperaties command (Arrive) from query (TimeArrived) + keeps your inheritance more logical.

You might not even need Initialisers anymore. And don't let Customer inherit from Form, because a Customer isn't a Form.

在你怀里撒娇 2024-10-13 06:23:33

我认为这里有更多的设计问题,您正在客户内部创建客户实例。

您的客户到达方法可能应该是另一个类中的函数,如下所示,客户应该只定义客户是什么。处理它们应该由不同的类来处理。

class Customer
{
    int waitingTime;         
    int arrivalTime;         
    int arrivalInterval; 

    // etc...
}

class ProcessCustomers
{
    pubic void Arrive()
    {
        // etc...
    }
}

public void goButton_Click(object sender, EventArgs e)            
{                
     Initialisers init = new Initialisers();                    
     ProcessCustomers CustomerQueue = new ProcessCustomers();                    
     CustomerQueue .Arrive();            
}  

但是对于文本框问题,您必须在表单类中公开一个属性并进行设置,

string ArrivalTime
{    
    get     
    {       
       return textBox1.Text;    
    }    
    set    
    {       
       textBox1.Text = value;    
    } 
} 

I think there is more of a design issue here, you are creating instances of customer inside customer.

Your customer Arrive method should probably be a function inside the another class, like below, customer should just define what a customer is. Processing them should be handled by a different class.

class Customer
{
    int waitingTime;         
    int arrivalTime;         
    int arrivalInterval; 

    // etc...
}

class ProcessCustomers
{
    pubic void Arrive()
    {
        // etc...
    }
}

public void goButton_Click(object sender, EventArgs e)            
{                
     Initialisers init = new Initialisers();                    
     ProcessCustomers CustomerQueue = new ProcessCustomers();                    
     CustomerQueue .Arrive();            
}  

But for the text box issue you will have to expose a property in the form class and set it like that,

string ArrivalTime
{    
    get     
    {       
       return textBox1.Text;    
    }    
    set    
    {       
       textBox1.Text = value;    
    } 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文