Advertisement
JustAnotherEntity

Assignment 3 Coding Form 2

May 17th, 2024
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.01 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using System.Reflection;
  6.  
  7. namespace Assignment2
  8. {
  9.     public partial class Form2 : Form
  10.     {
  11.         public Form2() => InitializeComponent();
  12.  
  13.         private void Form2_Load(object sender, EventArgs e)
  14.         {
  15.             int y = 0;
  16.             int dist = 100;
  17.             int spaceBetween = 10;
  18.             foreach (PropertyInfo Prop in Form1.UserSettings.GetType().GetProperties())
  19.             {
  20.                 TextBox TB = new TextBox
  21.                 {
  22.                     Text = Prop.GetValue(Form1.UserSettings, null).ToString(),
  23.                     Location = new Point(dist, y),
  24.                     Width = 200,
  25.                     Tag = Prop.Name.ToLower()
  26.                 };
  27.                 TB.TextChanged += (object sender2, EventArgs e2) =>
  28.                 {
  29.                     ModifyCurrentSettings();
  30.                     (Owner as Form1).ApplySettings();
  31.                 };
  32.                 Controls.Add(TB);
  33.  
  34.                 Label LBL = new Label
  35.                 {
  36.                     Text = Prop.Name,
  37.                     Location = new Point(0, y),
  38.                     Width = dist,
  39.                     Height = TB.Height,
  40.                     Tag = Prop.Name.ToLower(),
  41.                     TextAlign = ContentAlignment.MiddleCenter,
  42.                     AutoSize = false
  43.                 };
  44.                 Controls.Add(LBL);
  45.  
  46.                 y += TB.Height + spaceBetween;
  47.             }
  48.         }
  49.  
  50.         private void Form2_FormClosing(object sender, FormClosingEventArgs e) => ModifyCurrentSettings();
  51.  
  52.         public void ModifyCurrentSettings()
  53.         {
  54.             Label[] lbls = Controls.OfType<Label>().ToArray();
  55.             TextBox[] tbs = Controls.OfType<TextBox>().ToArray();
  56.  
  57.             for (int i = 0; i < tbs.Length; i++)
  58.                 Form1.UserSettings.GetType().GetProperty(lbls[i].Text).SetValue(Form1.UserSettings, tbs[i].Text == "" ? "1" : tbs[i].Text);
  59.         }
  60.     }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement