Код:
using System;
using System.IO;
using Newtonsoft.Json;
using System.Windows.Forms;
using WindowsFormsApp1.Util;
using WindowsFormsApp1.View;
using WindowsFormsApp1.Model;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
Graph graph;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
int count;
if (!Int32.TryParse(textBox1.Text, out count))
{
count = 5;
}
if (checkBox2.Checked)
{
graph = Creator.createDirected(count);
}
else
{
graph = Creator.createNonDirected(count);
}
if (checkBox1.Checked)
{
Creator.createWeight(graph, 5, 20);
}
Display();
}
private void button2_Click(object sender, EventArgs e)
{
var serializer = new JsonSerializer();
FileDialog dialog = new SaveFileDialog();
dialog.Filter = "Text files (*.txt)|*.txt";
if (dialog.ShowDialog() == DialogResult.OK)
{
using (StreamWriter sw = new StreamWriter(dialog.FileName))
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, graph);
}
}
}
private void button3_Click(object sender, EventArgs e)
{
var serializer = new JsonSerializer();
FileDialog dialog = new OpenFileDialog();
dialog.Filter = "Text files (*.txt)|*.txt";
if (dialog.ShowDialog() == DialogResult.OK)
{
using (StreamReader sr = new StreamReader(dialog.FileName))
using (JsonReader reader = new JsonTextReader(sr))
{
graph = serializer.Deserialize<Graph>(reader);
}
Display();
}
}
private void Display()
{
tableGetHeader(dataGridView1, graph.matrix.Count);
if (graph.weight != null)
tableGetHeader(dataGridView2, graph.weight.Count);
else
{
tableGetHeader(dataGridView2, 0);
}
pictureBox1.Image = GraphDisplayer.draw(pictureBox1.Height, pictureBox1.Width, graph);
Table.DisplayInTable(graph, dataGridView1, dataGridView2);
}
private void tableGetHeader(DataGridView dgv, int count)
{
dgv.ColumnCount = count;
dgv.RowCount = count;
for (int i = 1; i <= count; ++i)
{
dgv.Columns[i - 1].HeaderText = i.ToString();
dgv.Rows[i - 1].HeaderCell.Value = i.ToString();
}
}
}
}