0
点赞
收藏
分享

微信扫一扫

C# Code


  N年前的代码了,好久没用C#,早忘记了...

1.保存对话框  private void btnSaveData_Click(object sender, EventArgs e)

        {           


            if (sw != null)


            {


                sw.Close();


            }


            DialogResult result;


            SaveFileDialog save = new SaveFileDialog();


            save.InitialDirectory = Application.StartupPath;


            save.FileName = saveFilePath;


            save.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";


            result = save.ShowDialog();


            if (result != DialogResult.Cancel)


            {


                try


                {


                    sw = new StreamWriter(save.FileName);


                }


                catch (IOException)


                {


                }


            }


            if (sw != null)


            {


                sw.Close();


            }


            save.Dispose();


        }


2.double i = 22.22323;


            Console.WriteLine(i.ToString("#.###"));


            Console.WriteLine(i.ToString("F2"));


3.


            //不显示


           // for (int i = 0; i < chart1.Series.Count(); i++)


          //  {


                // chart1.Series.Remove(chart1.Series[i]);


          //      chart1.Series[i].Points.Clear();


          //  }


          //  showChart(chart1, new int[5] { 1, 1, 1, 1, 1 });


4.


Attach the first series to a chart area.


chart1.Series["Series1"].ChartArea = "Default";


    


// Attach the second series to a chart area.


chart1.Series["Series2"].ChartArea = "Chart Area 2";


    


// Remove series from chart areas.


chart1.Series["Series3"].ChartArea = "";


5.


  private void clearChart(Chart chartName)


        {




            for (int i = 0; i < chart1.Series.Count(); i++)


            {


                //清楚series的所有点


                chartName.Series[i].Points.Clear();


            }


            showChart(chart1, new int[5] { 1, 1, 1, 1, 1 });


            showChart(chart2, new int[5] { 1, 1, 1, 1, 1 });


        }


6.combobox selectedIndex = 0;//属性栏竟然没有!!


7.series 传参:int String


method(int index,String name){


Series[index] or


Series["name"]


}




8. //   FileStream fs = new FileStream(filePath, FileMode.Create);


            //   StreamWriter sw = new StreamWriter(fs);        


            //   fs.Close();


9.chart1.Series["Series 1"].Enabled = false;


10 add and remove series


using System.Windows.Forms.DataVisualization.Charting;


...




private void AddButton_Click(object sender, System.EventArgs e)


{


    Random rnd = new Random();




    Series series = Chart1.Series.Add("Series 1");


    series.ChartArea = "Default";


    series.ChartType = SeriesChartType.Spline;


    series.BorderWidth = 2;




    int j = 0;


    int MaxPoints = 10;


    while(j++ < MaxPoints)


    {


        series.Points.Add(rnd.Next(5,20));


    }


    ...


}




private void RemoveButton_Click(object sender, System.EventArgs e)


{


    // Remove the last series in the list of the series


    Chart1.Series.RemoveAt(Chart1.Series.Count-1);


}




11legend1.Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Bottom;




CursorPositionChanging 


this.Chart1.GetToolTipText += new System.EventHandler<System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs>(this.Chart1_GetToolTipText);


 private void chart1_GetToolTipText(object sender, System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs e)


        {




            // Check selected chart element and set tooltip text


            switch (e.HitTestResult.ChartElementType)


            {


                case ChartElementType.DataPoint:


                  e.Text = "Data Point:" + e.HitTestResult.PointIndex.ToString();


                  e.Text = "Data Point:" + chart1.HitTest(e.X, e.Y);


                //  System.Windows.Forms.DataVisualization.Charting.DataPoint ws=null;


               //  double i = ws.XValue;                


                    break;


                /*


                case ChartElementType.Axis:


                    e.Text = e.HitTestResult.Axis.Name;


                    break;     


                case ChartElementType.LegendArea:


                    e.Text = "Legend Area";


                    break;


                case ChartElementType.LegendItem:


                    e.Text = "Legend Item";


                    break;


                case ChartElementType.PlottingArea:


                    e.Text = "Plotting Area";


                    break;


                case ChartElementType.StripLines:


                    e.Text = "Strip Lines";


                    break;


                case ChartElementType.TickMarks:


                    e.Text = "Tick Marks";


                    break;


                case ChartElementType.Title:


                    e.Text = "Title";


                    break;


                 */


            }


12. using System.Windows.Forms.DataVisualization.Charting;


...




private void Save_Click(object sender, System.EventArgs e)


{


    // Create a new save file dialog


    SaveFileDialog saveFileDialog1 = new SaveFileDialog();




    // Sets the current file name filter string, which determines 


    // the choices that appear in the "Save as file type" or 


    // "Files of type" box in the dialog box.


    saveFileDialog1.Filter = "Bitmap (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg|EMF (*.emf)|*.emf|PNG (*.png)|*.png|SVG (*.svg)|*.svg|GIF (*.gif)|*.gif|TIFF (*.tif)|*.tif";


    saveFileDialog1.FilterIndex = 2 ;


    saveFileDialog1.RestoreDirectory = true ;




    // Set image file format


    if(saveFileDialog1.ShowDialog() == DialogResult.OK)


    {


        ChartImageFormat format = ChartImageFormat.Bmp;




        if( saveFileDialog1.FileName.EndsWith( "bmp" ) )


        {


            format = ChartImageFormat.Bmp;


        }


        else if( saveFileDialog1.FileName.EndsWith( "jpg" ) )


        {


            format = ChartImageFormat.Jpeg;


        }


        else if( saveFileDialog1.FileName.EndsWith( "emf" ) )


        {


            format = ChartImageFormat.Emf;


        }


        else if( saveFileDialog1.FileName.EndsWith( "gif" ) )


        {


            format = ChartImageFormat.Gif;


        }


        else if( saveFileDialog1.FileName.EndsWith( "png" ) )


        {


            format = ChartImageFormat.Png;


        }


        else if( saveFileDialog1.FileName.EndsWith( "tif" ) )


        {


            format = ChartImageFormat.Tiff;


        }


        else if( saveFileDialog1.FileName.EndsWith( "svg" ) )


        {


            format = ChartImageFormat.Svg;


        }




        // Save image


        Chart1.SaveImage( saveFileDialog1.FileName, format );


    }


}




private void Copy_Click(object sender, System.EventArgs e)


{


    // create a memory stream to save the chart image    


    System.IO.MemoryStream stream = new System.IO.MemoryStream();     




    // save the chart image to the stream    


    Chart1.SaveImage(stream,System.Drawing.Imaging.ImageFormat.Bmp);     




    // create a bitmap using the stream    


    Bitmap bmp = new Bitmap(stream);     




    // save the bitmap to the clipboard    


    Clipboard.SetDataObject(bmp); 


}


13.using System.Windows.Forms.DataVisualization.Charting;


...




if(comboBoxTemplate.Text == "None")


{


    // Reset chart appearance


    chart1.Serializer.Content = SerializationContents.Appearance;


    chart1.Serializer.Reset();


}


else


{


    // Load appearance template        


    chart1.Serializer.IsResetWhenLoading = false;


    chart1.LoadTemplate("MyTemplate.xml");


}


...


Note that special keywords can be used to insert various chart values. For example, "#VALX" is used to display a data point's X value in the tooltip.


14.隐藏series。chartArea=“”。enabled=false




//


        //ToolTip事件


        //


        private void chart1_GetToolTipText(object sender,System.Windows.Forms.DataVisualization.Charting.ToolTipEventArgs e)


        { 


            switch (e.HitTestResult.ChartElementType)


            {


                case ChartElementType.DataPoint:


                    e.Text = "#VAL";                   


                    break;




            }


        } //       chart1.Series[1].Points[i].ToolTip = "#VAL";


             //       chart1.Series[2].Points[i].ToolTip = "#VAL";


                    //     chart1.Series[3].Points[i].ToolTip = "#VAL";


                    //      chart1.Series[4].Points[i].ToolTip = "#VAL";


                    //      chart1.Series[5].Points[i].ToolTip = "#VAL";


                    


             //       chart2.Series[1].Points[i].ToolTip = "#VAL";


                    //      chart2.Series[2].Points[i].ToolTip = "#VAL";


                    //      chart2.Series[3].Points[i].ToolTip = "#VAL";


                    //     chart2.Series[4].Points[i].ToolTip = "#VAL";


                    //     chart2.Series[5].Points[i].ToolTip = "#VAL";


for(int i=0;i<chart1.Series[0].Points.Count();i++)


chart1.Series[0].ToolTip = "#VAL"; 


            chart1.Series[0].LegendToolTip = "Income in #LABEL  is #VAL million";


            chart1.Series[0].LabelToolTip = "#VAL";




this.comboBoxTo.SelectedIndexChanged += new System.EventHandler(this.comboBoxFrom_SelectedIndexChanged);












chart1.Series["Series 1"].Points.DataBindY();


double [] yval = { 2,6,4,5,3};




// Initialize an array of string


string [] xval = { "Peter", "Andrew", "Julie", "Mary", "Dave"};




// Bind the double array to the Y axis points of the Default data series


chart1.Series["Series 1"].Points.DataBindXY(xval,yval);


... 


realTime






 chart1.Series[0].IsValueShownAsLabel = true;///marker point






15.rename :1.File.Move(srcFile,destFile);


2.Copy(old,new);Delete(old);




series 传参(int seriesIndex or String seriesName)




16.//


        //将数据(data)写入文件(filePath)中,暂时没用


        //


        private void writeToFile(String filePath, String data)


        {


            StreamWriter sw = new StreamWriter(filePath, true, System.Text.Encoding.Default);


            //开始写入


            sw.Write(data);


            //清空缓冲区


            sw.Flush();


            //关闭流


            sw.Close();


        }


        private void writeToFile(String filePath, byte[] data)


        {


            StreamWriter sw = new StreamWriter(filePath, true, System.Text.Encoding.Default);


            //开始写入


            sw.Write(data.ToString());


            //清空缓冲区


            sw.Flush();


            //关闭流


            sw.Close();


        }


        //


        //清空文件(saveFilePath)中的内容【将原内容覆盖为一个空字符串】


        //


        private void btnFileClear_Click(object sender, EventArgs e)


        {


            StreamWriter streamWrite = new StreamWriter(saveFilePath);


            streamWrite.Write("");


            streamWrite.Close();


        }




17  //


        // 测试,初始化Chart图表         


        //


        private void initChart(Chart chartName)


        {


            chartName.Series[0].BorderColor = Color.Red;


            chartName.Series[1].BorderColor = Color.Green;


            double[] testData1 = new double[10] { 0, 1, 2, 3, 4, 4, 3, 2, 1, 0 };


            double[] testData2 = new double[10] { 0, -1, -2, -3, -4, -4, -3, -2, -1, 0 };


            for (int i = 0; i < 10; i++)


            {


                chartName.Series[0].Points.AddY(testData1[i]);


                chartName.Series[1].Points.AddY(testData2[i]);


            }


            chartName.Series[0].ChartType = SeriesChartType.Spline;


            chartName.Series[1].ChartType = SeriesChartType.Spline;


            // Set point labels 


            chartName.Series[0].IsValueShownAsLabel = false;


            chartName.Series[1].IsValueShownAsLabel = false;


            // Enable X axis margin("ChartArea1")   


            chartName.ChartAreas[0].AxisX.IsMarginVisible = false;            


        }




18 Application.Exit();


            this.Dispose();//The same.


            this.Close();


19. //要访问UI资源,用Invoke方式同步UI


            this.Invoke((EventHandler)delegate


            {


                //btnStart.Enabled = false;


                //byte[] buffer = new byte[126];


            });


18 if(int.Parse(TextBox1.Text)>0){}


try{


 Convert.ToInt32(TextBox1.Text.Trim());


 return true;


}catch(Exception){


 return false;


}


举报

相关推荐

0 条评论