示例#1
0
        private void btBrowse_Click(object sender, EventArgs e)
        {
            tbFile.Text = "";
            lbOutput.Items.Clear();
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "*.csv | *.*";
            dialog.InitialDirectory = Directory.GetCurrentDirectory().ToString();
            dialog.Title = "Select CSV File";
            if (dialog.ShowDialog() == DialogResult.OK)
            {

                string filename = dialog.FileName;
                tbFile.Text = filename;
                var reader = new System.IO.StreamReader(File.OpenRead(filename));
                List<string> listA = new List<string>();
                List<string> listB = new List<string>();
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine();
                    string output = "Default";
                    if(line.Equals(""))
                    {
                        output = "No Data Found";
                    }
                    else
                    {
                        var values = line.Split(',');
                        string shape = values[0];
                        string length = values[1];
                        double n;
                        bool isNumeric = double.TryParse(length, out n);
                        if (isNumeric)
                        {
                            switch (shape)
                            {
                                case "circle":
                                    Circle c = new Circle(length);
                                    output = c.GetOutput();
                                    break;
                                case "triangle":
                                    Triangle t = new Triangle(length);
                                    output = t.GetOutput();
                                    break;
                                case "square":
                                    Square s = new Square(length);
                                    output = s.GetOutput();
                                    break;
                                case "pentagon":
                                    Pentagon p = new Pentagon(length);
                                    output = p.GetOutput();
                                    break;
                                default:
                                    // You can use the default case.
                                    output = "Shape " + shape + " is not a valid shape";
                                    break;
                            }
                        }
                        else
                            output = "Shape " + shape + " does not have a numeric value ( " +  length + " )";

                    }
                    lbOutput.Items.Add(output);
                }
            }
        }