这段代码运行没有问题,就是在给文本框中输入值后,单击按钮,Label1就显示空白了。
我是以,20,56,30,带进去的,各位帮忙看下。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace again
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {            time Atime = new time();
            Atime.Sethour(Int32.Parse(textBox1.Text));
            Atime.Setminute(Int32.Parse(textBox2.Text));
            Atime.Setsecond(Int32.Parse(textBox3.Text));
            string s = Atime.Gettimes();
            label1.Text = s;
        }
    }
    public class time
    {
        private int Hour;
        private int Minute;
        private int Second;
        private string times;
        public int Gethour()
        {
            return Hour;
        }
        public void Sethour(int Newhour)
        {
            Hour = (Hour > 0 && Hour < 24) ? Hour : -1;
        }
        public int Getminute()
        {
            return Minute;
        }
        public void Setminute(int Newminute)
        {
            Minute = (Minute >= 0 && Minute < 60) ? Minute : -1;
        }
        public int Getsecond()
        {
            return Second;
        }
        public void Setsecond(int Newsecond)
        {
            Second = (Second >= 0 && Second < 60) ? Second : -1;
        }
        public string Gettimes()
        {
            return times;
        }
        public void Settimes(int Newtimes)
        {
            if (Hour != -1 && Minute != -1 && Second != -1)
            {
                 times =String.Format("{0}:{1:D2}:{2:D2} {3}",
                    (Hour == 12 || Hour == 0) ? 12 : Hour % 12, Minute,Second,
                    Hour < 12 ? "AM" : "PM");
            }
            else
                MessageBox.Show("你输入的时间不正确,请重新输入");
        }
    }
}

解决方案 »

  1.   

    因为label1.Text = s; S的值为NULL, 所有显示空白。
     代码有问题:问题一:
    Atime.Sethour(Int32.Parse(textBox1.Text));
    Atime.Setminute(Int32.Parse(textBox2.Text));
    Atime.Setsecond(Int32.Parse(textBox3.Text));
    这三行代码传的参数实际上都没有用到
    问题二: 
    string s = Atime.Gettimes();  这个函数返回times,但是times一直没有赋过值  
      

  2.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace again
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                  InitializeComponent();
             }        private void button1_Click(object sender, EventArgs e)
            {
                 time Atime = new time();
                 Atime.Sethour(Int32.Parse(textBox1.Text));
                 Atime.Setminute(Int32.Parse(textBox2.Text));
                 Atime.Setsecond(Int32.Parse(textBox3.Text));
                 //实例化一个类后,你要给你的times赋值啊
                 Atime.Settimes();  //你缺少的是这句话
                 string s = Atime.Gettimes();
                 label1.Text = s;
               }
          }
      
          public class time
         {
                 private int Hour;
                 private int Minute;
                 private int Second;
                 private string times;
                 public int Gethour()
                {
                     return Hour;
                }
                public void Sethour(int Newhour)
                {
                       Hour = (Hour > 0 && Hour < 24) ? Hour : -1;
                 }
     
                 public int Getminute()
                {
                       return Minute;
                 }
     
                 public void Setminute(int Newminute)
                {
                        Minute = (Minute >= 0 && Minute < 60) ? Minute : -1;
                }           public int Getsecond()
               {
                     return Second;
               }           public void Setsecond(int Newsecond)
               {
                         Second = (Second >= 0 && Second < 60) ? Second : -1;
                }
     
              public string Gettimes()
             {
                  return times;
              }         public void Settimes()
             {
                   if (Hour != -1 && Minute != -1 && Second != -1)
                   {
                          times =String.Format("{0}:{1:D2}:{2:D2} {3}",
                             (Hour == 12 || Hour == 0) ? 12 : Hour % 12, Minute,Second,
                                  Hour < 12 ? "AM" : "PM");
                   }
                  else
                   {
                                MessageBox.Show("你输入的时间不正确,请重新输入");
                   }
             }
        }
    }
      

  3.   

    public string Gettimes()
      {
    if (Hour != -1 && Minute != -1 && Second != -1)
      {
      times =String.Format("{0}:{1:D2}:{2:D2} {3}",
      (Hour == 12 || Hour == 0) ? 12 : Hour % 12, Minute,Second,
      Hour < 12 ? "AM" : "PM");
      }
      else
      MessageBox.Show("你输入的时间不正确,请重新输入");
      
      return times;
      }
      
      

  4.   

    补充了您这句代码,还是不行,错误提示:“Settimes”方法没有任何重载采用“0”个参数
      

  5.   

    您这种用公共的我做好了,现在是用SET,GET,来做。
      

  6.   

            //将你的这个方法中的参数去掉,你有没有用到干嘛呢??
             public void Settimes()
             {
                   if (Hour != -1 && Minute != -1 && Second != -1)
                   {
                          times =String.Format("{0}:{1:D2}:{2:D2} {3}",
                             (Hour == 12 || Hour == 0) ? 12 : Hour % 12, Minute,Second,
                                  Hour < 12 ? "AM" : "PM");
                   }
                  else
                   {
                                MessageBox.Show("你输入的时间不正确,请重新输入");
                   }
             }
      

  7.   

    现在输入值后,点击按钮,显示的是Messagebox里面的内容,输入时间不正确。
      

  8.   

    帮你仔细看了一下,你是在设置时分秒的时候,没使用函数里的参数进行判断,而是用类中本身的属性来判断所以你的那个Hour、Minute和Seconde都是-1,所以报输入错误。
    已帮你改正,将如下代码拷贝过去覆盖下试试吧。
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace again
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                  InitializeComponent();
             }        private void button1_Click(object sender, EventArgs e)
            {
                 time Atime = new time();
                 Atime.Sethour(Int32.Parse(textBox1.Text));
                 Atime.Setminute(Int32.Parse(textBox2.Text));
                 Atime.Setsecond(Int32.Parse(textBox3.Text));
                 //实例化一个类后,你要给你的times赋值啊
                 Atime.Settimes();  //你缺少的是这句话
                 string s = Atime.Gettimes();
                 label1.Text = s;
               }
          }
      
          public class time
         {
                 private int Hour;
                 private int Minute;
                 private int Second;
                 private string times;
                 public int Gethour()
                {
                     return Hour;
                }
                public void Sethour(int Newhour)
                {
                       Hour = (Newhour> 0 && Newhour< 24) ? Newhour: -1;
                 }
     
                 public int Getminute()
                {
                       return Minute;
                 }
     
                 public void Setminute(int Newminute)
                {
                        Minute = (Newminute>= 0 && Newminute< 60) ? Newminute: -1;
                }           public int Getsecond()
               {
                     return Second;
               }           public void Setsecond(int Newsecond)
               {
                         Second = (Newsecond>= 0 && Newsecond< 60) ? Newsecond: -1;
                }
     
              public string Gettimes()
             {
                  return times;
              }         public void Settimes()
             {
                   if (Hour != -1 && Minute != -1 && Second != -1)
                   {
                          times =String.Format("{0}:{1:D2}:{2:D2} {3}",
                             (Hour == 12 || Hour == 0) ? 12 : Hour % 12, Minute,Second,
                                  Hour < 12 ? "AM" : "PM");
                   }
                  else
                   {
                                MessageBox.Show("你输入的时间不正确,请重新输入");
                   }
             }
        }
    }