Advertisement
JustAnotherEntity

Assignment 3 Coding Form 1

May 17th, 2024
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.62 KB | None | 0 0
  1. using System.Windows.Forms;
  2. using Newtonsoft.Json;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Drawing;
  6. using System.Collections.Generic;
  7. using System;
  8.  
  9. namespace Assignment2
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         private static readonly string UserSettingsPath = Path.Combine(Environment.CurrentDirectory, @"../../UserSettings.json");
  14.         private static readonly string idcanymore = Path.Combine(Environment.CurrentDirectory, @"../../magic.html");
  15.         public static Settings UserSettings;
  16.  
  17.         public Form1() => InitializeComponent();
  18.  
  19.         private void Form1_Load(object sender, EventArgs e)
  20.         {
  21.             string[] Lines = File.ReadAllLines(UserSettingsPath);
  22.             string AllFileLines = string.Join("", Lines);
  23.             UserSettings = JsonConvert.DeserializeObject<Settings>(AllFileLines);
  24.  
  25.             ApplySettings();
  26.  
  27.             webView21.Source = new Uri(idcanymore);
  28.  
  29.             foreach (Control Control in GetAllControlsOfType(this, typeof(Label)))
  30.             {
  31.                 Label Label = Control as Label;
  32.  
  33.                 Label.MaximumSize = new Size(Label.Parent.Width - 4, 1000);
  34.             }
  35.             foreach (Control Control in GetAllControlsOfType(this, typeof(TextBox)))
  36.             {
  37.                 TextBox TextBox = Control as TextBox;
  38.  
  39.                 TextBox.Height = 13 * (TextBox.Text.Split('\n').Length + 1);
  40.                 TextBox.MaximumSize = new Size(TextBox.Parent.Width - 4, 1000);
  41.             }
  42.         }
  43.  
  44.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  45.         {
  46.             JsonSerializer JsonSerializer = new JsonSerializer
  47.             {
  48.                 Formatting = Formatting.Indented
  49.             };
  50.             using (StreamWriter SW = new StreamWriter(UserSettingsPath))
  51.             {
  52.                 using (JsonWriter Writer = new JsonTextWriter(SW))
  53.                 {
  54.                     JsonSerializer.Serialize(Writer, UserSettings);
  55.                 }
  56.             }
  57.         }
  58.  
  59.         private float GetValidValue(string val)
  60.         {
  61.             if (NullEmptyOrWhitespace(val) || val == "0")
  62.                 return 8f;
  63.             return float.Parse(val);
  64.         }
  65.  
  66.         private bool NullEmptyOrWhitespace(string str) =>
  67.             str == null || str.Trim() == "";
  68.  
  69.         public void ApplySettings()
  70.         {
  71.             Font TitleFont = new Font(UserSettings.TitleFontName == "" ? "Microsoft Sans Serif" : UserSettings.TitleFontName, GetValidValue(UserSettings.TitleFontSize));
  72.             Font GeneralFont = new Font(UserSettings.GeneralFontName == "" ? "Microsoft Sans Serif" : UserSettings.GeneralFontName, GetValidValue(UserSettings.GeneralFontSize));
  73.             Font CodeFont = new Font(UserSettings.CodeFontName == "" ? "Microsoft Sans Serif" : UserSettings.CodeFontName, GetValidValue(UserSettings.CodeFontSize));
  74.  
  75.             foreach (Control Control in GetAllControlsOfType(tabControl1, typeof(Label)))
  76.             {
  77.                 Label Label = Control as Label;
  78.  
  79.                 Label.Margin = new Padding(Label.Margin.Left, Label.Margin.Top, Label.Margin.Right, UserSettings.TextBoxMargining == "" ? 10 : int.Parse(UserSettings.TextBoxMargining));
  80.                
  81.                 if ((string)Control.Tag == "title")
  82.                 {
  83.                     Label.Font = TitleFont;
  84.                     Label.ForeColor = Color.FromName(UserSettings.TitleFontColor == "" ? "black" : UserSettings.TitleFontColor);
  85.                 }
  86.                 else if ((string)Control.Tag == "general")
  87.                 {
  88.                     Label.Font = GeneralFont;
  89.                     Label.ForeColor = Color.FromName(UserSettings.GeneralFontColor == "" ? "black" : UserSettings.GeneralFontColor);
  90.                 }
  91.             }
  92.  
  93.             foreach (Control Control in GetAllControlsOfType(tabControl1, typeof(TextBox)))
  94.             {
  95.                 TextBox TextBox = Control as TextBox;
  96.  
  97.                 TextBox.Margin = new Padding(TextBox.Margin.Left, TextBox.Margin.Top, TextBox.Margin.Right, UserSettings.TextBoxMargining == "" ? 10 : int.Parse(UserSettings.TextBoxMargining));
  98.  
  99.                 if ((string)Control.Tag == "code")
  100.                 {
  101.                     TextBox.Font = CodeFont;
  102.                     TextBox.ForeColor = Color.FromName(UserSettings.CodeFontColor == "" ? "black" : UserSettings.CodeFontColor);
  103.                 }
  104.             }
  105.         }
  106.  
  107.         public IEnumerable<Control> GetAllControlsOfType(Control Control, Type Type)
  108.         {
  109.             IEnumerable<Control> Controls = Control.Controls.Cast<Control>();
  110.  
  111.             return Controls
  112.                 .SelectMany(Ctrl => GetAllControlsOfType(Ctrl, Type))
  113.                 .Concat(Controls)
  114.                 .Where(c => c.GetType() == Type);
  115.         }
  116.  
  117.         public void Alert(object obj) => MessageBox.Show(obj.ToString());
  118.  
  119.         private void OpenSettings_Click(object sender, EventArgs e)
  120.         {
  121.             Form2 F2 = new Form2
  122.             {
  123.                 Owner = this
  124.             };
  125.             F2.Show();
  126.         }
  127.     }
  128.  
  129.     public class Settings
  130.     {
  131.         public string TextBoxMargining { get; set; }
  132.         public string TitleFontName { get; set; }
  133.         public string TitleFontSize { get; set; }
  134.         public string TitleFontColor { get; set; }
  135.         public string GeneralFontName { get; set; }
  136.         public string GeneralFontSize { get; set; }
  137.         public string GeneralFontColor { get; set; }
  138.         public string CodeFontName { get; set; }
  139.         public string CodeFontSize { get; set; }
  140.         public string CodeFontColor { get; set; }
  141.     }
  142. }
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement