2017-01-27 77 views
0

我設計一個時間類如何在窗體類改變類(對象)的字段的值

` 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類)並且想要在計時器滴答事件中更改類(對象)字段的值。

我喜歡。

Time_Class t1 = new Time_Class(); 

     private void timer1_Tick(object sender, EventArgs e) 
     { 

      //code 
     } 

但是在計時器滴答聲中,這個類的對象沒有調用,我定義了計時器滴答聲的一面。

我如何在計時器滴答中調用這個類的對象並更改該類(對象)的字段值。

+0

請擡頭看'else'和'其他if'控制流語句,並刪除'goto' – pinkfloydx33

+0

是的,它是Windows窗體計時器和enabled屬性是假,但我開始計時器加載泡沫事件像'timer1.Start();' –

+0

@ pinkfloydx33是的,我會做到這一點,但沒有關係的要求。 –

回答

3

你必須讓公衆領域:

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

是的。現在對象正在調用計時器打勾。 –

相關問題