我写了个月份选择器按<按钮从后往前,按>按钮从前往后,不管按那个按钮,都会在txt文本框中显示月份,一共12个月份,我刚开始写的代码是如果文本框的Text是1,那鼠标单击>则变成2,依次往后,如果是12那单击<则变成11依次往前,虽然功能是实现了,但觉得这样写有代码有点多,我想写个for循环,代码如下
替换代码:
        private void button1_Click(object sender, EventArgs e)
        {
            //if (textBox1.Text=="12")
            //{
            //    this.textBox1.Text = "11";
            //}            //else if(textBox1.Text=="11")
            //{
            //    this.textBox1.Text = "10";
            //}            //else if(textBox1.Text=="10")
            //{
            //    this.textBox1.Text = "9";
            //}            //else if(textBox1.Text=="9")
            //{
            //    this.textBox1.Text = "8";
            //}            //else if(textBox1.Text=="8")
            //{
            //    this.textBox1.Text = "7";
            //}            //else if(textBox1.Text=="7")
            //{
            //    this.textBox1.Text = "5";
            //}            //else if(textBox1.Text=="6")
            //{
            //    this.textBox1.Text = "5";
            //}            //else if (textBox1.Text == "5")
            //{
            //    this.textBox1.Text = "4";
            //}            //else if (textBox1.Text == "4")
            //{
            //    this.textBox1.Text = "3";
            //}            //else if (textBox1.Text == "3")
            //{
            //    this.textBox1.Text = "2";
            //}            //else if (textBox1.Text == "2")
            //{
            //    this.textBox1.Text = "1";
            //}
            //else
            //{
            //    MessageBox.Show("您输入的月份不正确!");
            //}
        }
不好意思注释未消我写的循环代码:
            int i;
            int n = 0;
            if (textBox1.Text != "")
            {
                
                for (i = 0; i < 12 ; i++ ) 
                {
                    n=int.Parse(textBox1.Text)+1;
                }
            }
            textBox1.Text = Convert.ToString(n);
这个思路是让它循环12次,每循环一次就加1,应为文本框中是string类型,所以要转换成int然后每次加1,没循环一次就显示在文本框中本次结果是多少,这个倒是实现了,不过出现了一点意外就是没有限制的循环,我按>按钮加到12次后,后面还能加,而且一直在加,我规定那个<12根本没意义了!到底是那个地方出错也找不出来!

解决方案 »

  1.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication19
    {
        public enum genders//定义性别枚举
        {
            female = 0,
            male
        }    public class person//定义人类
        {
            private String _name;//人的名称
            /// <summary>
            /// 获取或设置人的名称
            /// </summary>
            public String name
            {
                get
                {
                    return this._name;
                }
                set
                {
                    //去掉名字前后的空格
                    this._name = value.Trim();
                }
            }
            private genders _gender;//人的性别
            /// <summary>
            /// 获取或设置人的性别
            /// </summary>
            public genders gender
            {
                get
                {
                    return this._gender;
                }
                set
                {
                    this._gender = value;// 这里不是说不改变,Value是一个内置的参数,表示接受传入的值。
                }
            }
        }
        class Program
        {
            static void Main(string[] args)//入口函数,程序是从这里开始执行的
            {
                person aperson = new person();//实例化得到一个人的对象aperson
                aperson.name = "zhangsan";//设置这个人的名字为zhangsan
                aperson.gender = genders.male;//设置这个人的性别
                printperson(aperson);//将APERSON送入PRINTPERSON方法,这里printperson是用来输出aperson中的信息的。            aperson.name = " lisi";// 设置人的名字为lisi
                aperson.gender = genders.male;//设置这个人的性别
                printperson(aperson);//输出这个人的信息            aperson.name = " wanger";// 设置人的名字为wanger
                aperson.gender = genders.male;//设置这个人的性别
                printperson(aperson);//输出这个人的信息
            }        /// <summary>
            /// 输出指定的人的信息
            /// </summary>
            /// <param name="aperson">指定要进行信息输出的人</param>
            static void printperson(person aperson)
            {
                System.Console.WriteLine("name:{0},gender:{1}", aperson.name, aperson.gender);//输出
            }
        }
    }