本人初学c#,感觉自己写的代码很怪,请大家指出我的不足,谢谢using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
using System.Text;namespace Start_App
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            timer1.Enabled = false;
            inspeed = 10;
            outspeed = 10;
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            state = 0;
            timer1.Enabled = true;
            this.Opacity = 0;            try
            {
                if (!File.Exists(peizhilujing))
                {
                    File.AppendAllText(peizhilujing, "", Encoding.Default);
                }
                string quanbuneirong = File.ReadAllText(peizhilujing);
                string[] quan = quanbuneirong.Split('*');
                foreach (string peizhixiangmu in quan)
                {
                    if ((peizhixiangmu != null) && (peizhixiangmu != ""))
                    {
                        listBox1.Items.Add(peizhixiangmu);
                        listBox1.SetSelected(0, true);
                    }
                }
            }
            catch { }
        }        private int state;//淡入\淡出状态
        private double inspeed;
        private double outspeed;        public double InSpeed //淡入速度属性(0---100)
        {
            get{ return inspeed; }
            set{ inspeed = value;}
        }        public double OutSpeed //淡出速度属性(0---100)
        {
            get {return outspeed;}
            set { outspeed = value;}
        }        public void FormClose()//窗体关闭时,调用此函数实现淡出效果
        {
            state = -1;
            timer1.Enabled = true;
        }        private void timer1_Tick_1(object sender, EventArgs e)
        {
            if (state >= 0)
            {
                this.Opacity += inspeed / 200;
                if (this.Opacity == 1)
                {
                    timer1.Enabled = false;
                }
            }
            else if (state < 0)
            {
                this.Opacity -= outspeed / 200;
                if (this.Opacity == 0)
                {
                    this.Close();
                    timer1.Enabled = false;
                }
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            FormClose();
        }        private void addapp(object sender, EventArgs e)
        {
            openFileDialog1.Title = "打开秋秋要定时检查的程序";
            openFileDialog1.Filter = "可执行文件 (*.exe)|*.exe";
            openFileDialog1.InitialDirectory = @"F:\program Files\";
            openFileDialog1.FileName = "";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                listBox1.Items.Add(openFileDialog1.FileName);
                int suoyin = listBox1.Items.IndexOf(openFileDialog1.FileName);
                listBox1.SetSelected(suoyin, true);
            }
            string gangdakai = openFileDialog1.FileName;
            xiepeizhi(gangdakai);
        }        string peizhilujing = Directory.GetCurrentDirectory() + @"\q.txt";
        private void xiepeizhi(string gangdakai)
        {
            try
            {
                bool panduan = true;
                string quanbuneirong = File.ReadAllText(peizhilujing);
                string[] quan = quanbuneirong.Split('*');
                foreach (string peizhixiangmu in quan)
                {
                    if (peizhixiangmu == gangdakai) { panduan = false; }
                }
                if (panduan == true)
                {
                    File.AppendAllText(peizhilujing, gangdakai + "*");
                }
            }
            catch { MessageBox.Show("加条目写文件失败了!"); }
        }        static System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
        private void dishi(object sender, EventArgs e)
        {
            if (label3.Text == "打开")
            {
                myTimer.Enabled = true;
                int jiange = Convert.ToInt32(textBox1.Text) * 1000;
                myTimer.Interval = jiange;
                myTimer.Tick += new EventHandler(kong);
                string app = Convert.ToString(listBox1.SelectedItem);
                string appname = Path.GetFileNameWithoutExtension(app);
                int ProgressCount = 0;//判断进程是否运行的标识
                Process[] prc = Process.GetProcesses();
                foreach (Process pr in prc) //遍历整个进程
                {
                    string pname = pr.ProcessName;
                    if (appname == pname) //如果进程存在
                    {
                        ProgressCount += 1; //计数器清空
                    }
                }
                if (ProgressCount == 0)
                { try { Process.Start(app); }
                catch { MessageBox.Show("进程相关的出问题了!"); }
                }
            }
        }        private void kong(object sender, EventArgs e)
        {
        }        public void qidong(Object sender, EventArgs e)
        {
            if (listBox1.Items.Count == 0)
            {
                MessageBox.Show("请先添加文件!");
            }
            if (listBox1.Items.Count > 0)
            {
                myTimer.Tick += new EventHandler(dishi);
                int jiange = Convert.ToInt32(textBox1.Text) * 1000;
                myTimer.Interval = jiange;
                myTimer.Start();
                label3.Text = "打开";
            }
        }        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = e.KeyChar < 48 || e.KeyChar > 57;
            if (e.KeyChar == 8)
            {
                e.Handled = false;
            }
        }        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.Items.Count == 0)
            {
                MessageBox.Show("请先添加文件!");
            }
            if (listBox1.Items.Count > 0)
            {
                label3.Text = "关闭";
            }
        }        private void listBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                int index = listBox1.IndexFromPoint(e.X, e.Y);                if (index != -1)
                {
                    listBox1.SelectedIndex = index;
                    contextMenuStrip1.Show(Control.MousePosition.X, 
                        Control.MousePosition.Y);
                }
            }
        }        private void deleteItemToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                try 
                {
                    string itemzhi = listBox1.SelectedItem.ToString();
                    StreamReader sr = new StreamReader(peizhilujing, Encoding.Default);
                    string context = sr.ReadToEnd();
                    sr.Close();
                    context = context.Replace(itemzhi + "*", "");
                    StreamWriter sw = new StreamWriter(peizhilujing);
                    sw.Write(context);
                    sw.Close();
                    
                    listBox1.Items.RemoveAt(listBox1.SelectedIndex);
                }
                catch { MessageBox.Show("删除启动条目失败!"); }
            }
        }        private void checkBox1_Click(object sender, EventArgs e)
        {
            if (checkBox1.CheckState == CheckState.Checked)
            {
                this.TopMost = true;
            }
            else
            {
                this.TopMost = false;
            }
        }        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("www.vfxcg.com");
        }    }
}

解决方案 »

  1.   

    可以用vs自带的格式化工具  格式化下你的代码
    Ctrl+E,D
      

  2.   

    1,代码不够美观,给人杂乱的感觉2,在控件命名及方法命名上要多注意些,比如button2这样的可以这么写 btn_add 。让阅读代码的人大体知道你一个控件是做什么用的3,变量定义不要写到哪想到了就定义在哪4,适当的注释还是要的
    以上提的不好的大家别丢砖啊!~~呵呵
      

  3.   

    private int state;//淡入\淡出状态
            private double inspeed;
            private double outspeed;        public double InSpeed //淡入速度属性(0---100)
            {
                get{ return inspeed; }
                set{ inspeed = value;}
            }        public double OutSpeed //淡出速度属性(0---100)
            {
                get {return outspeed;}
                set { outspeed = value;}
            }        public void FormClose()//窗体关闭时,调用此函数实现淡出效果
            {
                state = -1;
                timer1.Enabled = true;
            }
          
           private double inspeed;        public double InSpeed //淡入速度属性(0---100)
            {
                get{ return inspeed; }
                set{ inspeed = value;}
            }
                    private double outspeed;
            public double OutSpeed //淡出速度属性(0---100)
            {
                get {return outspeed;}
                set { outspeed = value;}
            }
               private int state;//淡入\淡出状态        public void FormClose()//窗体关闭时,调用此函数实现淡出效果
            {
                state = -1;
                timer1.Enabled = true;
            }
    不知道你能不能看明白我的意思。另外,楼上几位所说的都很值得去学习。
      

  4.   

    private void xiepeizhi(string gangdakai)    //???
    {
    }我的建议是要么写英文,要么写中文,拼音很难形象化:
    private void 写配置(string 配置)
    {
    }private void WriteConfig(string config)
    {
    }另一个建议就是空的catch几乎一定是错的。现在不一定能说服你,不过等你有不错的水品后应该就能体会了。
      

  5.   

            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                e.Handled = e.KeyChar < 48 || e.KeyChar > 57;
                if (e.KeyChar == 8)
                {
                    e.Handled = false;
                }
            }
    尽量不要用魔术数字,换成:        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                const int digit0 = 48;
                const int digit9 = 57;            e.Handled = e.KeyChar < digit0 || e.KeyChar > digit9;
            }
            // 也可以直接用 (char)Keys.D0和(char)Keys.D9
      

  6.   

    顺着14楼的话题,你实现只能输入数字的TextBox还比较简明易懂。
    不过还有其他一些更普遍方法,比如包装一个继承TextBox的类:    class NumericalTextBox : TextBox
        {
            protected override CreateParams CreateParams
            {
                get
                {
                    CreateParams cp = base.CreateParams;
                    const int ES_NUMBER = 0x2000;
                    cp.Style = cp.Style | ES_NUMBER;
                    return cp;
                }
            }
        }
    编译后从工具栏就可以拖到设计窗口了,而且你可以发现它能应付剪贴-拷贝(你的方法不能)。
      

  7.   

    简单看了一下:每个控件最好发其业务含义命名。如:lb_UserName
    方法变量命名最好使用英文
    注释最好使用
            /// <summary>
            /// 产生ProjectCostingReport
            /// </summary>
            /// <param name="templateFile">模板 这里传绝对对路径</param>
            /// <param name="currency">货币</param>
            /// <param name="asOfDate"></param>
            /// <param name="condition">条件</param>
            /// <param name="orderBy">排序字串,只含order by后面部分</param>
            /// <returns>返回生成报表名称</returns>