VS 2010 C#程序总结

1.TEXTBOX大小随控件大小改变控件上添加设置


this.TextBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));


2. console.read()在控制台上输出结果停留

 

3.设置秒表计时器,只用一个button控件,一个label控件,一个timer控件

 public partial class Form1 : Form


    {
        public Form1()
        {
            InitializeComponent();
        }
        System.Diagnostics.Stopwatch sw;


        bool state = false;
        private void button1_Click(object sender, EventArgs e)
        {
            if (!state)
            {
                sw = new System.Diagnostics.Stopwatch();

                sw.Start();

                timer1.Start();

                state = true;
                button1.Text = "停止";
            }
            else
            {
                sw.Stop();

                TimeSpan ts = sw.Elapsed;

                label1.Text = String.Format("{0}小时{1}分{2}秒", ts.Hours, ts.Minutes, ts.Seconds);
                state = false;
                button1.Text = "开始";
           

            }

 


        private void timer1_Tick(object sender, EventArgs e)
        {
            TimeSpan ts = sw.Elapsed;
            label1.Text = String.Format("{0}小时{1}分{2}秒", ts.Hours, ts.Minutes, ts.Seconds);


        }
    }




4.System.Windows.Forms.Timer用法:


System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();//实例化一个timer


myTimer.Tick += new EventHandler(函数名); //给timer挂起事件


myTimer.Enabled = true;//使timer可用


myTimer.Interval = n; //设置时间间隔,以毫秒为单位


myTimer.Stop(); //如果要暂停计时则使用Stop()方法


myTimer.Enabled = false;//若要停止使用timer,则使之不可用


5.chart控件(http://download.csdn.net/detail/cherry123678/9847641)

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        const int chartInitiMaxSizeX = 3500;
        const int chartInitiMinSizeX = 0;
        const int chartMaxSizeY = 200;
        const int chartMinSizeY = 0;
        const int chartVerRange = 2000;
        const int chartInitiVerScrollMin = 0;
        const int chartInitiHorMin = 0;
        const int chartIntervalX = 500;
        const int chartIntervalY = 10;
        const int chartInitializeVerMax = 200;
        const int chartInitializeVerMin = 0;
        Random rd = new Random();
        double indexTime = 0;
        double offsetNumber = 0;//偏移量
        double count = 0;//计数
        double newY;
        int maxX = 0;
        // System.Diagnostics.Stopwatch sw;


        private void Form1_Load(object sender, EventArgs e)
        {


            timer1.Enabled = false;//禁止计时器运行
            timer1.Interval = 70;
            InitChart();


        }
        //初始化chart的一般属性,以及静态显示坐标
        private void InitChart()
        {
            chart.Series[0].ChartType = SeriesChartType.Line;
            chart.ChartAreas[0].AxisY.Minimum = chartMinSizeY;
            chart.ChartAreas[0].AxisY.Maximum = chartMaxSizeY;
            chart.ChartAreas[0].AxisY.Interval = chartIntervalY;
            chart.ChartAreas[0].AxisX.Minimum = chartInitiMinSizeX;
            chart.ChartAreas[0].AxisX.Maximum = chartInitiMaxSizeX;
            chart.ChartAreas[0].AxisX.Interval = chartIntervalX;




            //初始化x,y坐标轴
            //points数据为:(0,0),(500,0),(1000,0)...(3500,0)
            //其points的索引是0-7
            for (double i = 0; i <= 3500; i += 500)
            {
                chart.Series[0].Points.AddXY(i, 0);
                chart.Series[0].Points[chart.Series[0].Points.Count - 1].IsEmpty = true;


            }


            hScrollBar.Maximum = chartInitiMaxSizeX;
            hScrollBar.Minimum = chartInitiMinSizeX;
            hScrollBar.Value = chartInitiMaxSizeX;
            vScrollBar.Maximum = chartVerRange - chartInitializeVerMax;
            vScrollBar.Minimum = chartInitializeVerMin;
            vScrollBar.Value = chartVerRange - chartInitializeVerMax;




        }
        //重置x轴属性
        private void SetChartX(double pianyi)
        {


            chart.ChartAreas[0].AxisX.Minimum = chartInitiMinSizeX + offsetNumber * pianyi;
            chart.ChartAreas[0].AxisX.Maximum = chartInitiMaxSizeX + offsetNumber * pianyi;
            chart.ChartAreas[0].AxisX.Interval = chartIntervalX;
            hScrollBar.Maximum = (int)(chartInitiMaxSizeX + offsetNumber * pianyi);
            hScrollBar.Minimum = 0;
            hScrollBar.Value = hScrollBar.Maximum;


            int cnt = chart.Series[0].Points.Count;
            double tmp = 0;


            for (int i = 0; i < 8; i++)
            {
                tmp = chart.Series[0].Points[i].XValue + pianyi;
                chart.Series[0].Points[i].XValue = tmp;


            }


        }
       
        private void startBtn_Click(object sender, EventArgs e)
        {


            chart.Series[0].Points.Clear();
            InitChart();
          
            timer1.Start();


            indexTime = 0;
            offsetNumber = 0;
            hScrollBar.Enabled = false;
            count = 0;


        }
     
        private void timer1_Tick(object sender, EventArgs e)
        {
            
            int seconds = (int)(indexTime / 1000);
            int MillSencinds = (int)(indexTime % 1000);
            int minute = seconds / 60;
            int seconds2 = seconds % 60;
            timecount.Text = String.Format("{0}分{1}秒{2}毫秒", minute, seconds2, MillSencinds);
            newY = 1 + rd.Next(32);


            count++;


            indexTime = count * 70;


            //70为采样频率
            if (indexTime + 140 > (chartInitiMaxSizeX + offsetNumber * 500))//
            {
                // count = 0;
                //每次x轴向右移动500个单位
                SetChartX(500);
                offsetNumber++;


            }
         
            chart.Series[0].Points.AddXY(indexTime, newY);
            hScrollBar.Maximum = chartInitiMaxSizeX;
            hScrollBar.Value = hScrollBar.Maximum;
     }


        private void hScrollBar_Scroll(object sender, ScrollEventArgs e)
        {


            //判断当前滚动条的值在哪个区间里面
            //并获取区间的上下限
            //判断滚动条的拖动方向
            int diffValue = e.NewValue - e.OldValue;
            if (diffValue > 0)
            {
                //向右移动
                UpDataX(hScrollBar.Value + 3500, 1);


            }
            else if (diffValue < 0)
            {
                //向左移动
                UpDataX(hScrollBar.Value, -1);
                
            }
            else
            {
                UpDataX(hScrollBar.Value, 0);
                
            }




        }
        //根据滚动条的值更新x轴坐标
        private void UpDataX(int hScrollVaule, int judgeX)
        {


            int x1 = (int)chart.Series[0].Points[1].XValue;
            if (judgeX == 1)
            {
                if (hScrollVaule > chart.Series[0].Points[1].XValue && chart.Series[0].Points[7].XValue < hScrollBar.Maximum)
                {
                    for (int i = 0; i < 8; i++)
                    {
                        chart.Series[0].Points[i].XValue += 500;
                    }


                    chart.ChartAreas[0].AxisX.Minimum = chart.Series[0].Points[0].XValue;
                    chart.ChartAreas[0].AxisX.Maximum = chart.Series[0].Points[7].XValue;


                }


            }
            else if (judgeX == -1)
            {
                if (hScrollVaule < chart.Series[0].Points[6].XValue && chart.Series[0].Points[0].XValue > 0)
                {
                    for (int i = 0; i < 8; i++)
                    {
                        chart.Series[0].Points[i].XValue -= 500;


                    }
                    chart.ChartAreas[0].AxisX.Minimum = chart.Series[0].Points[0].XValue;
                    chart.ChartAreas[0].AxisX.Maximum = chart.Series[0].Points[7].XValue;
                }
            }
        }


        private void stopBtn_Click(object sender, EventArgs e)
        {


            timer1.Stop();
           
            hScrollBar.Enabled = true;
            //设置滚动条hScrollBar1
            UpdataHScrollBar();
        }


        private void UpdataHScrollBar()
        {
            int cnt = chart.Series[0].Points.Count;//数量
            int max = (int)chart.Series[0].Points[cnt - 1].XValue;//最后一个point的x值
            cnt = max / 500 + 1;
            max = cnt * 500;//向上取整
            hScrollBar.Maximum = max;
            hScrollBar.Minimum = 0;// max - 3500;
            hScrollBar.Value = max;
        }


        private void vScrollBar_Scroll(object sender, ScrollEventArgs e)
        {


            double dbHeight;
            dbHeight = chart.ChartAreas[0].AxisY.Maximum - chart.ChartAreas[0].AxisY.Minimum;


            if ((chartVerRange - e.NewValue - dbHeight) < chart.ChartAreas[0].AxisY.Maximum)
            {
                chart.ChartAreas[0].AxisY.Minimum = (chartVerRange - e.NewValue) - dbHeight;


                if (chart.ChartAreas[0].AxisY.Minimum < 1)
                {
                    chart.ChartAreas[0].AxisY.Minimum = chartInitiVerScrollMin;
                }
                chart.ChartAreas[0].AxisY.Maximum = chart.ChartAreas[0].AxisY.Minimum + dbHeight;
            }
            else
            {
                chart.ChartAreas[0].AxisY.Maximum = chartVerRange - e.NewValue;
                chart.ChartAreas[0].AxisY.Minimum = (chartVerRange - e.NewValue) - dbHeight;
                if (chart.ChartAreas[0].AxisY.Minimum < 1)
                {
                    chart.ChartAreas[0].AxisY.Minimum = chartInitiVerScrollMin;
                }


            }
        }
        //保存
        private void saveBtn_Click(object sender, EventArgs e)
        {


             SaveFileDialog saveFileDialog1 = new SaveFileDialog();
             saveFileDialog1.RestoreDirectory = true ;
             saveFileDialog1.Title = "保存";
             saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
   
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                StreamWriter sw = new StreamWriter(saveFileDialog1.FileName, true);
              
                int counts=chart.Series[0].Points.Count-8;
                sw.WriteLine(counts.ToString());
                sw.Flush();
              
                //向创建的文件中写入内容
                for (int i = 8; i < chart.Series[0].Points.Count; i++)
                {
                    sw.WriteLine(chart.Series[0].Points[i].XValue + " " + chart.Series[0].Points[i].YValues[0]);
                    sw.Flush();
                }
                //关闭当前文件写入流
                sw.Close();
            }
                
        }
        
        //打开
        private void openBtn_Click(object sender, EventArgs e)
        {
            
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "打开";
            ofd.Filter = "txt文件|*.txt|所有文件|*.*";
           


            if (ofd.ShowDialog() == DialogResult.OK)
            {
                int cnt = 0;
                int Len = 0;
                FileStream fs = new FileStream(ofd.FileName, FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(fs);


                string strLine;
                strLine = sr.ReadLine();
                cnt = int.Parse(strLine);
                double[] temp0 = new double[cnt];
                double[] temp1 = new double[cnt];
                strLine = sr.ReadLine();//第二行
                while (strLine != null)
                {


                    string[] sl = strLine.Split(' ');
                    temp0[Len] = Convert.ToDouble(sl[0]);
                    temp1[Len] = Convert.ToDouble(sl[1]);
                    Len++;
                    strLine = sr.ReadLine();
                }
                maxX = (int)temp0[Len - 1];
              
                sr.Dispose();
                fs.Dispose();
                chart.Series[0].Points.Clear();
                InitChart();
                hScrollBar.Maximum = maxX;
                hScrollBar.Minimum = 0;
                hScrollBar.Value = 0;
                for (int pointIndex = 0; pointIndex < Len; pointIndex++)
                {


                    chart.Series[0].Points.AddXY(temp0[pointIndex], temp1[pointIndex]);
                } 
               
            }  
        }




    }



6.通过搜索注册表获取路径打开已安装软件

       

(未考虑版本不同的情况)


       string installLocation = null;
       
        string tracePath = null;
        private static System.Diagnostics.Process p;
        private void button1_Click(object sender, EventArgs e)
        {
            
            try  
            {

               //注册表HKEY_LOCAL_MACHINE / SOFTWARE
                RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Adobe\Photoshop", false);
                
                RegistryKey key2;
                if (key != null)//判断对象存在
                {
                    foreach (string keyName in key.GetSubKeyNames())//遍历子项名称的字符串数组
                    {
                        key2 = key.OpenSubKey(keyName, false);//遍历子项节点


                        if (key2 != null)
                        {
                            if (keyName.Equals("12.0"))
                            {
                                installLocation = key2.GetValue("ApplicationPath", "").ToString();//获取安装路径
                              
                                tracePath = installLocation;
                                tracePath += @"Photoshop.exe";
                                MessageBox.Show(tracePath);
                                if (p == null)
                                {
                                    p = new System.Diagnostics.Process();
                                    p.StartInfo.FileName = tracePath;


                                    p.Start();
                                }
                                else
                                {
                                    if (p.HasExited) //是否正在运行
                                    {
                                        p.Start();
                                    }
                                }
                                p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                                break;


                            }


                        }


                    }


                }
                else
                {
                    MessageBox.Show("尚未安装Photoshop!");
 
                }




            }  
            catch  
            {  
                 MessageBox.Show("获取程序路径失败!");  
            }  


        }
    


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值