开发者

TextBox not updating in C#

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 Leav开发者_高级运维e()
        {

        }

        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;
            }
        }
    }
}


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.


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.


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;    
    } 
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜