C# Winform文本面板带滚动条的实现过程

  using Newtonsoft.Json;

  using System;

  using System.Drawing;

  using System.IO;

  using System.Text.RegularExpressions;

  using System.Windows.Forms;

  namespace Test

  {

  public partial class JsonWin : Form

  {

  public JsonWin(string json)

  {

  InitializeComponent();

  Json= json;

  }

  private string Json = string.Empty;

  private void JsonWin_Load(object sender, EventArgs e)

  {

  string json = PraseToJson(Json);

  string[] arr = Regex.Split(json, "

  ", RegexOptions.IgnoreCase);

  Size size = label1.Size;

  Size newSize = new Size(size.Width, arr.Length * 23);

  label1.Size = newSize;

  panel2.Size = newSize;

  label1.Text = json;

  }

  public string PraseToJson(string json)

  {

  try

  {

  JsonSerializer s = new JsonSerializer();

  JsonReader reader = new JsonTextReader(new StringReader(json));

  Object jsonObject = s.Deserialize(reader);

  StringWriter sWriter = new StringWriter();

  JsonWriter writer = new JsonTextWriter(sWriter);

  writer.Formatting = Newtonsoft.Json.Formatting.Indented;

  s.Serialize(writer, jsonObject);

  return sWriter.ToString();

  }

  catch (Exception)

  {

  return json;

  }

  }

  }

  }