diff --git a/Components/Components.csproj b/Components/Components.csproj index 3d78086..3b33f9a 100644 --- a/Components/Components.csproj +++ b/Components/Components.csproj @@ -7,4 +7,9 @@ enable + + + + + diff --git a/Components/UncheckedNullException.cs b/Components/Exceptions/UncheckedNullException.cs similarity index 90% rename from Components/UncheckedNullException.cs rename to Components/Exceptions/UncheckedNullException.cs index 73b6e8c..e66fdae 100644 --- a/Components/UncheckedNullException.cs +++ b/Components/Exceptions/UncheckedNullException.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace WinFormsLibrary1 +namespace Components.Exceptions { public class UncheckedNullException : Exception { diff --git a/Components/UnexpectedTypeException.cs b/Components/Exceptions/UnexpectedTypeException.cs similarity index 90% rename from Components/UnexpectedTypeException.cs rename to Components/Exceptions/UnexpectedTypeException.cs index 19eace9..e742c0a 100644 --- a/Components/UnexpectedTypeException.cs +++ b/Components/Exceptions/UnexpectedTypeException.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace WinFormsLibrary1 +namespace Components.Exceptions { public class UnexpectedTypeException : Exception { diff --git a/Components/Nonvisual/UserControlTableDocument.Designer.cs b/Components/Nonvisual/UserControlTableDocument.Designer.cs new file mode 100644 index 0000000..525ee88 --- /dev/null +++ b/Components/Nonvisual/UserControlTableDocument.Designer.cs @@ -0,0 +1,36 @@ +namespace Components +{ + partial class UserControlTableDocument + { + /// + /// Обязательная переменная конструктора. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Освободить все используемые ресурсы. + /// + /// истинно, если управляемый ресурс должен быть удален; иначе ложно. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Код, автоматически созданный конструктором компонентов + + /// + /// Требуемый метод для поддержки конструктора — не изменяйте + /// содержимое этого метода с помощью редактора кода. + /// + private void InitializeComponent() + { + components = new System.ComponentModel.Container(); + } + + #endregion + } +} diff --git a/Components/Nonvisual/UserControlTableDocument.cs b/Components/Nonvisual/UserControlTableDocument.cs new file mode 100644 index 0000000..a1ce594 --- /dev/null +++ b/Components/Nonvisual/UserControlTableDocument.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Components +{ + public partial class UserControlTableDocument : Component + { + public UserControlTableDocument() + { + InitializeComponent(); + } + + public UserControlTableDocument(IContainer container) + { + container.Add(this); + + InitializeComponent(); + } + + public void SaveToDocument(string filePath, string header, List tables) + { + if (string.IsNullOrEmpty(filePath)) throw new ArgumentNullException("Empty string instead of path to file"); + if (string.IsNullOrEmpty(header)) throw new ArgumentNullException("Header string is empty"); + if (tables.Count == 0) throw new ArgumentNullException("Table list is empty"); + + SaveToPdf saver = new SaveToPdf(); + + saver.CreateDoc(filePath, header, tables); + } + } +} diff --git a/Components/SaveToPdf.cs b/Components/SaveToPdf.cs new file mode 100644 index 0000000..7ac3f44 --- /dev/null +++ b/Components/SaveToPdf.cs @@ -0,0 +1,117 @@ +using MigraDoc.DocumentObjectModel; +using MigraDoc.DocumentObjectModel.Tables; +using MigraDoc.Rendering; +using Components.SaveToPdfHelpers; + +namespace Components +{ + internal class SaveToPdf + { + private Document? _document; + private Section? _section; + private Table? _table; + public void CreateDoc(string title, string path, List tables) + { + _document = new Document(); + DefineStyles(_document); + _section = _document.AddSection(); + + CreateParagraph(new PdfParagraph + { + Text = title, + Style = "NormalTitle", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + + foreach (var table in tables) + { + if (table.Length == 0) continue; + + CreateTable(Enumerable.Repeat("3cm", table[0].Length).ToList()); + + foreach (var row in table) + { + CreateRow(new PdfRowParameters + { + Texts = row.ToList(), + Style = "Normal", + ParagraphAlignment = PdfParagraphAlignmentType.Left + }); + } + + CreateParagraph(new PdfParagraph()); + } + + var renderer = new PdfDocumentRenderer(true) + { + Document = _document + }; + renderer.RenderDocument(); + renderer.PdfDocument.Save(path); + } + private static ParagraphAlignment GetParagraphAlignment(PdfParagraphAlignmentType type) + { + return type switch + { + PdfParagraphAlignmentType.Center => ParagraphAlignment.Center, + PdfParagraphAlignmentType.Left => ParagraphAlignment.Left, + PdfParagraphAlignmentType.Right => ParagraphAlignment.Right, + _ => ParagraphAlignment.Justify, + }; + } + private static void DefineStyles(Document document) + { + var style = document.Styles["Normal"]; + style.Font.Name = "Times New Roman"; + style.Font.Size = 14; + style = document.Styles.AddStyle("NormalTitle", "Normal"); + style.Font.Bold = true; + } + protected void CreateParagraph(PdfParagraph pdfParagraph) + { + if (_section == null) + { + return; + } + var paragraph = _section.AddParagraph(pdfParagraph.Text); + paragraph.Format.SpaceAfter = "1cm"; + paragraph.Format.Alignment = GetParagraphAlignment(pdfParagraph.ParagraphAlignment); + paragraph.Style = pdfParagraph.Style; + } + protected void CreateTable(List columns) + { + if (_document == null) + { + return; + } + _table = _document.LastSection.AddTable(); + foreach (var elem in columns) + { + _table.AddColumn(elem); + } + } + protected void CreateRow(PdfRowParameters rowParameters) + { + if (_table == null) + { + return; + } + var row = _table.AddRow(); + for (int i = 0; i < rowParameters.Texts.Count; ++i) + { + row.Cells[i].AddParagraph(rowParameters.Texts[i]); + if (!string.IsNullOrEmpty(rowParameters.Style)) + { + row.Cells[i].Style = rowParameters.Style; + } + Unit borderWidth = 0.5; + row.Cells[i].Borders.Left.Width = borderWidth; + row.Cells[i].Borders.Right.Width = borderWidth; + row.Cells[i].Borders.Top.Width = borderWidth; + row.Cells[i].Borders.Bottom.Width = borderWidth; + row.Cells[i].Format.Alignment = GetParagraphAlignment(rowParameters.ParagraphAlignment); + row.Cells[i].VerticalAlignment = VerticalAlignment.Center; + } + } + } +} diff --git a/Components/SaveToPdfHelpers/PdfParagraph.cs b/Components/SaveToPdfHelpers/PdfParagraph.cs new file mode 100644 index 0000000..ac1f2ab --- /dev/null +++ b/Components/SaveToPdfHelpers/PdfParagraph.cs @@ -0,0 +1,9 @@ +namespace Components.SaveToPdfHelpers +{ + public class PdfParagraph + { + public string Text { get; set; } = string.Empty; + public string Style { get; set; } = string.Empty; + public PdfParagraphAlignmentType ParagraphAlignment { get; set; } + } +} diff --git a/Components/SaveToPdfHelpers/PdfParagraphAlignmentType.cs b/Components/SaveToPdfHelpers/PdfParagraphAlignmentType.cs new file mode 100644 index 0000000..bde3c13 --- /dev/null +++ b/Components/SaveToPdfHelpers/PdfParagraphAlignmentType.cs @@ -0,0 +1,9 @@ +namespace Components.SaveToPdfHelpers +{ + public enum PdfParagraphAlignmentType + { + Center, + Left, + Right + } +} diff --git a/Components/SaveToPdfHelpers/PdfRowParameters.cs b/Components/SaveToPdfHelpers/PdfRowParameters.cs new file mode 100644 index 0000000..76435a6 --- /dev/null +++ b/Components/SaveToPdfHelpers/PdfRowParameters.cs @@ -0,0 +1,10 @@ + +namespace Components.SaveToPdfHelpers +{ + public class PdfRowParameters + { + public List Texts { get; set; } = new(); + public string Style { get; set; } = string.Empty; + public PdfParagraphAlignmentType ParagraphAlignment { get; set; } + } +} diff --git a/Components/User.cs b/Components/User.cs deleted file mode 100644 index 8a521b8..0000000 --- a/Components/User.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace WinFormsLibrary1 -{ - public class User - { - public string username { get; private set; } - public string email { get; private set; } - public DateTime birthday { get; private set; } - - public User(string username, string email, DateTime birthday) - { - this.username = username; - this.email = email; - this.birthday = birthday; - } - - } -} diff --git a/Components/ColumnInfo.cs b/Components/Visual/ColumnInfo.cs similarity index 52% rename from Components/ColumnInfo.cs rename to Components/Visual/ColumnInfo.cs index f504008..08a8955 100644 --- a/Components/ColumnInfo.cs +++ b/Components/Visual/ColumnInfo.cs @@ -4,20 +4,14 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace WinFormsLibrary1 +namespace Components.Visual { public class ColumnInfo { public string HeaderText { get; set; } = string.Empty; - public int Width { get; set; } + public int Width { get; set; } public bool Visible { get; set; } public string Name { get; set; } = string.Empty; - public ColumnInfo(string headerText, string name, int width, bool visible) - { - HeaderText = headerText; - Name = name; - Width = width; - Visible = visible; - } + public string DataPropertyName { get; set; } = string.Empty; } } diff --git a/Components/UserControlIntegerInput.Designer.cs b/Components/Visual/UserControlIntegerInput.Designer.cs similarity index 98% rename from Components/UserControlIntegerInput.Designer.cs rename to Components/Visual/UserControlIntegerInput.Designer.cs index e978ffc..5a4661d 100644 --- a/Components/UserControlIntegerInput.Designer.cs +++ b/Components/Visual/UserControlIntegerInput.Designer.cs @@ -1,4 +1,4 @@ -namespace WinFormsLibrary1 +namespace Components { partial class UserControlIntegerInput { diff --git a/Components/UserControlIntegerInput.cs b/Components/Visual/UserControlIntegerInput.cs similarity index 96% rename from Components/UserControlIntegerInput.cs rename to Components/Visual/UserControlIntegerInput.cs index 3413573..b6f982c 100644 --- a/Components/UserControlIntegerInput.cs +++ b/Components/Visual/UserControlIntegerInput.cs @@ -1,4 +1,5 @@ -using System; +using Components.Exceptions; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -8,7 +9,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; -namespace WinFormsLibrary1 +namespace Components { public partial class UserControlIntegerInput : UserControl { diff --git a/Components/UserControlIntegerInput.resx b/Components/Visual/UserControlIntegerInput.resx similarity index 100% rename from Components/UserControlIntegerInput.resx rename to Components/Visual/UserControlIntegerInput.resx diff --git a/Components/UserControlStringsListBox.Designer.cs b/Components/Visual/UserControlStringsListBox.Designer.cs similarity index 98% rename from Components/UserControlStringsListBox.Designer.cs rename to Components/Visual/UserControlStringsListBox.Designer.cs index 6b42aee..382b913 100644 --- a/Components/UserControlStringsListBox.Designer.cs +++ b/Components/Visual/UserControlStringsListBox.Designer.cs @@ -1,4 +1,4 @@ -namespace WinFormsLibrary1 +namespace Components { partial class UserControlStringsListBox { diff --git a/Components/UserControlStringsListBox.cs b/Components/Visual/UserControlStringsListBox.cs similarity index 98% rename from Components/UserControlStringsListBox.cs rename to Components/Visual/UserControlStringsListBox.cs index 0cf9580..8693f27 100644 --- a/Components/UserControlStringsListBox.cs +++ b/Components/Visual/UserControlStringsListBox.cs @@ -8,7 +8,7 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; -namespace WinFormsLibrary1 +namespace Components { public partial class UserControlStringsListBox : UserControl { diff --git a/Components/UserControlStringsListBox.resx b/Components/Visual/UserControlStringsListBox.resx similarity index 100% rename from Components/UserControlStringsListBox.resx rename to Components/Visual/UserControlStringsListBox.resx diff --git a/Components/UserControlTable.Designer.cs b/Components/Visual/UserControlTable.Designer.cs similarity index 98% rename from Components/UserControlTable.Designer.cs rename to Components/Visual/UserControlTable.Designer.cs index 9db1b39..01b9469 100644 --- a/Components/UserControlTable.Designer.cs +++ b/Components/Visual/UserControlTable.Designer.cs @@ -1,4 +1,4 @@ -namespace WinFormsLibrary1 +namespace Components { partial class UserControlTable { diff --git a/Components/UserControlTable.cs b/Components/Visual/UserControlTable.cs similarity index 87% rename from Components/UserControlTable.cs rename to Components/Visual/UserControlTable.cs index f341e31..116c37f 100644 --- a/Components/UserControlTable.cs +++ b/Components/Visual/UserControlTable.cs @@ -8,8 +8,9 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using Components.Visual; -namespace WinFormsLibrary1 +namespace Components { public partial class UserControlTable : UserControl { @@ -41,6 +42,7 @@ namespace WinFormsLibrary1 HeaderText = columnInfo[i].HeaderText, Width = columnInfo[i].Width, Visible = columnInfo[i].Visible, + DataPropertyName = columnInfo[i].DataPropertyName, }; dataGridViewTable.Columns.Add(column); } @@ -51,9 +53,22 @@ namespace WinFormsLibrary1 dataGridViewTable.Rows.Clear(); } - public void AddObjectList(List os) + public void AddList(List values) { - dataGridViewTable.DataSource = os ?? throw new ArgumentNullException(); + ClearRows(); + var props = typeof(T).GetProperties(); + + foreach (T value in values) + { + int newRowInd = dataGridViewTable.Rows.Add(); + foreach (DataGridViewColumn col in dataGridViewTable.Columns) + { + var prop = props.FirstOrDefault(x => x.Name == col.DataPropertyName) + ?? throw new InvalidOperationException($"No property {col.DataPropertyName} found in type {typeof(T).Name}"); + var val = prop.GetValue(value); + dataGridViewTable.Rows[newRowInd].Cells[col.Index].Value = val; + } + } } public T GetObjectFromRow() @@ -73,28 +88,10 @@ namespace WinFormsLibrary1 for (int i = 0; i < dataGridViewTable.ColumnCount; i++) { var curCell = cells[i]; - var prop = props.FirstOrDefault(x => x.Name == dataGridViewTable.Columns[i].Name); + var prop = props.FirstOrDefault(x => x.Name == dataGridViewTable.Columns[i].DataPropertyName); prop?.SetValue(returnObject, curCell.Value); } return returnObject; } - - public void AddList (List values) - { - ClearRows(); - var props = typeof(T).GetProperties(); - - foreach (T value in values) - { - int newRowInd = dataGridViewTable.Rows.Add(value); - foreach (DataGridViewColumn col in dataGridViewTable.Columns) - { - var prop = props.FirstOrDefault(x => x.Name == col.Name) - ?? throw new InvalidOperationException($"No property {col.Name} found in type {typeof(T).Name}"); - var val = prop.GetValue(value); - dataGridViewTable.Rows[newRowInd].Cells[col.Index].Value = val; - } - } - } } } diff --git a/Components/UserControlTable.resx b/Components/Visual/UserControlTable.resx similarity index 100% rename from Components/UserControlTable.resx rename to Components/Visual/UserControlTable.resx diff --git a/ComponentsProgramming.sln b/ComponentsProgramming.sln index b8d9e2a..af87b6f 100644 --- a/ComponentsProgramming.sln +++ b/ComponentsProgramming.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 17.9.34728.123 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Components", "Components\Components.csproj", "{260D3E8C-3599-49F1-BF42-64A92DD0FB62}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestingForm", "TestingForm\TestingForm.csproj", "{4F4882A1-4DF1-4BC3-B022-9935E70E5552}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {260D3E8C-3599-49F1-BF42-64A92DD0FB62}.Debug|Any CPU.Build.0 = Debug|Any CPU {260D3E8C-3599-49F1-BF42-64A92DD0FB62}.Release|Any CPU.ActiveCfg = Release|Any CPU {260D3E8C-3599-49F1-BF42-64A92DD0FB62}.Release|Any CPU.Build.0 = Release|Any CPU + {4F4882A1-4DF1-4BC3-B022-9935E70E5552}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4F4882A1-4DF1-4BC3-B022-9935E70E5552}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4F4882A1-4DF1-4BC3-B022-9935E70E5552}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4F4882A1-4DF1-4BC3-B022-9935E70E5552}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/TestingForm/FormMain.Designer.cs b/TestingForm/FormMain.Designer.cs new file mode 100644 index 0000000..05ec2ff --- /dev/null +++ b/TestingForm/FormMain.Designer.cs @@ -0,0 +1,45 @@ +namespace TestingForm +{ + partial class FormMain + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + SuspendLayout(); + // + // FormMain + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Name = "FormMain"; + Text = "cB"; + ResumeLayout(false); + } + + #endregion + } +} diff --git a/TestingForm/FormMain.cs b/TestingForm/FormMain.cs new file mode 100644 index 0000000..80839c4 --- /dev/null +++ b/TestingForm/FormMain.cs @@ -0,0 +1,10 @@ +namespace TestingForm +{ + public partial class FormMain : Form + { + public FormMain() + { + InitializeComponent(); + } + } +} diff --git a/TestingForm/FormMain.resx b/TestingForm/FormMain.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/TestingForm/FormMain.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/TestingForm/Program.cs b/TestingForm/Program.cs new file mode 100644 index 0000000..47a2336 --- /dev/null +++ b/TestingForm/Program.cs @@ -0,0 +1,17 @@ +namespace TestingForm +{ + internal static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + // To customize application configuration such as set high DPI settings or default font, + // see https://aka.ms/applicationconfiguration. + ApplicationConfiguration.Initialize(); + Application.Run(new FormMain()); + } + } +} \ No newline at end of file diff --git a/TestingForm/TestingForm.csproj b/TestingForm/TestingForm.csproj new file mode 100644 index 0000000..5a248b2 --- /dev/null +++ b/TestingForm/TestingForm.csproj @@ -0,0 +1,15 @@ + + + + WinExe + net7.0-windows + enable + true + enable + + + + + + + \ No newline at end of file