如何更改表单类中的类(对象)的字段值

时间:2017-01-27 10:37:32

标签: c# winforms class object

我设计了一个时间等级

` class Time_Class
    {
        private int minute;
        private int second;
        private int hour;

        //Default constructor
        public Time_Class()
        {
            this.minute = 0;
            this.second = 0;
            this.hour = 0;
        }

        //Constructor overload
        public Time_Class(int mm, int ss, int hh)
        {
            this.minute = mm;
            this.second = ss;
            this.hour = hh;
        }

        //Method for setting time
        public void Set_Time(int mm,int ss,int hh)
        {
            this.minute = mm;
            this.second = ss;
            this.hour = hh;      
        }

        //Method of Getting 24 Hour time
        public string[] Get24Time()
        {
            int[] time = new int[3];
            time[0] = this.minute;
            time[1] = this.second;
            time[2] = this.hour;

            if (time[1] > 59)
            {
                time[1] = 0;
                time[0] += 1;         
            }
            if (time[0] > 59)
            {
                time[0] = 0;
                time[2] += 1;
            }
            if (time[2] > 24)
            {
                time[0] = 0;
                time[1] = 0;
                time[2] = 0;
            }

            string[] ret = new string[3];
            ret[0] = time[0].ToString();
            ret[1] = time[1].ToString();
            ret[2] = time[2].ToString();

            return ret;         
        }

        //Method of Getting 12 Houur time
        public string[] Get12Time()
        {
            string ampm = "AM";
            int[] time = new int[2];
            time[0] = this.minute;
            time[1] = this.second;
            time[2] = this.hour;

            if (time[1] > 59)
            {
                time[1] = 0;
                time[0] += 1;
            }
            if (time[0] > 59)
            {
                time[0] = 0;
                time[2] += 1;
            }
            if (time[2] > 12)
            {
                time[0] = 0;
                time[1] = 0;
                time[2] = 0;
                if (ampm == "PM")
                {
                    ampm = "AM";
                    goto b;
                }
                if (ampm=="AM")
                {
                    ampm = "PM";
                }

            }
        b:
            string[] ret = new string[3];
            ret[0] = time[0].ToString();
            ret[1] = time[1].ToString();
            ret[2] = time[2].ToString();
            ret[3] = ampm;

            return ret;         
        }

`

我想在winform(Form类)中创建此类的对象,并希望在timer tick事件中更改class(object)字段的值。

我喜欢这个。

 Time_Class t1 = new Time_Class();

        private void timer1_Tick(object sender, EventArgs e)
        {

            //code
        }

但是在计时器滴答声中,这个类的对象没有调用,我定义了计时器滴答的一面。

我如何在计时器刻度中调用此类的对象并更改该类(对象)的字段值。

1 个答案:

答案 0 :(得分:3)

你必须公开字段:

 public int minute {get;set;}
 public int second {get;set;}
 public int hour {get;set;}