public static void OpenCustomFigures(ComboBox combobox, List <Fabric> exsistingFabric) { try { using (StreamReader stream = new StreamReader(@"CustomFigures.json")) { if (stream.Peek() != -1) { MainFigure figure; List <MainFigure> figures = new List <MainFigure>(); List <Fabric> fabrics = new List <Fabric>(); string data = stream.ReadToEnd(); string[] dataArray = data.Split('\n'); List <int> countFigures = JsonConvert.DeserializeObject <List <int> >(dataArray[0]); int j = 1; foreach (var count in countFigures) { for (int i = j; i < count + j; i++) { InfoForJSON jSON = JsonConvert.DeserializeObject <InfoForJSON>(dataArray[i]); foreach (Fabric fab in exsistingFabric) { if (jSON.figureName == fab.ToString()) { jsonList.Add(jSON); figure = fab.FactoryMethod(jSON.fatness, jSON.color, jSON.topLeft, jSON.bottomRight); figures.Add(figure); fabrics.Add(fab); break; } } } AddCustomFigure(fabrics, figures, combobox); fabrics.Clear(); figures.Clear(); j = count + j; } } } } catch (Exception ex) { MessageBox.Show("Something wrong...", "Problems", MessageBoxButtons.OK, MessageBoxIcon.Warning); //continue; } jsonList.Clear(); }
private void openToolStripMenuItem_Click_1(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.InitialDirectory = "/files"; openFileDialog.RestoreDirectory = true; openFileDialog.FileName = "figures"; openFileDialog.DefaultExt = ".json"; if (openFileDialog.ShowDialog() == DialogResult.OK) { StreamReader stream = new StreamReader(openFileDialog.OpenFile()); string data = stream.ReadToEnd(); { string[] dataArray = data.Split('\n'); foreach (string dataBlock in dataArray) { try { InfoForJSON jSON = JsonConvert.DeserializeObject <InfoForJSON>(dataBlock); foreach (Fabric fab in allFabrics) { if (jSON.figureName == fab.ToString()) { jsonList.Add(jSON); Fabric factory = fab; figure = factory.FactoryMethod(jSON.fatness, jSON.color, jSON.topLeft, jSON.bottomRight); figure.Draw(graphics); picture.Image = bmap; break; } } } catch (Exception ex) { MessageBox.Show(message, title_mess, MessageBoxButtons.OK, MessageBoxIcon.Warning); continue; } } } stream.Close(); } }