Первый компонентик есть ^.^

This commit is contained in:
Максим Яковлев 2024-09-21 21:38:23 +04:00
parent d8ad4d290a
commit 2caa860fb5
24 changed files with 226 additions and 46 deletions

View File

@ -7,4 +7,8 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="PDFsharp-MigraDoc-GDI" Version="6.1.1" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,36 @@
namespace ComponentProgramming
{
partial class testComp
{
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Код, автоматически созданный конструктором компонентов
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}

View File

@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComponentProgramming
{
public partial class testComp : Component
{
private string _fileName;
public string FileName
{
set
{
if (string.IsNullOrEmpty(value)) return;
if (!value.EndsWith(".txt"))
{
throw new ArgumentException("No txt file");
}
_fileName = value;
}
}
public testComp()
{
InitializeComponent();
_fileName = string.Empty;
}
public testComp(IContainer container)
{
container.Add(this);
InitializeComponent();
_fileName = string.Empty;
}
public bool saveToFile(string[] texts)
{
CheckFileExist();
using var writer = new StreamWriter(_fileName, true);
foreach (var text in texts)
{
writer.WriteLine(text);
}
writer.Flush();
return true;
}
private void CheckFileExist()
{
if (string.IsNullOrEmpty(_fileName))
{
throw new ArgumentNullException(_fileName);
}
if(!File.Exists("../"+_fileName))
{
throw new FileNotFoundException(_fileName);
}
}
}
}

View File

@ -1,4 +1,4 @@
using ComponentProgramming.Exceptions;
using ComponentProgramming.Control.Exceptions;
using System;
using System.Collections.Generic;
using System.ComponentModel;

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComponentProgramming.Exceptions
namespace ComponentProgramming.Control.Exceptions
{
public class NumberException : Exception
{

View File

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Forms
namespace ComponentProgramming.Control
{
public class Person
{

View File

@ -0,0 +1,36 @@
namespace ComponentProgramming.Components
{
partial class LargeTextComponent
{
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Код, автоматически созданный конструктором компонентов
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
}
}

View File

@ -0,0 +1,63 @@
using System.ComponentModel;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.Rendering;
namespace ComponentProgramming.Components
{
public partial class LargeTextComponent : Component
{
private Document? _document;
private Section? _section;
public LargeTextComponent()
{
InitializeComponent();
}
public LargeTextComponent(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public void CreateDocument(string docPath, string title, string[] rows)
{
if(string.IsNullOrEmpty(docPath))
{
throw new ArgumentNullException("Введите путь до файла!");
}
if(string.IsNullOrEmpty(title))
{
throw new ArgumentNullException("Введите заголовок");
}
if(rows.Length <= 0)
{
throw new ArgumentNullException("Нету данных для сохранения");
}
_document = new Document();
var style = _document.Styles["Normal"];
style.Font.Name = "Times New Roman";
style.Font.Size = 14;
_section = _document.AddSection();
var paragraph = _section.AddParagraph(title);
paragraph.Format.SpaceAfter = "0.1cm";
paragraph.Format.Font.Bold = true;
foreach (var row in rows)
{
_section.AddParagraph(row);
}
var renderer = new PdfDocumentRenderer(true);
renderer.Document = _document;
renderer.RenderDocument();
renderer.PdfDocument.Save(docPath);
}
}
}

View File

@ -28,13 +28,13 @@
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
controlComboBox = new ComponentProgramming.ControlComboBox();
controlTextBox = new ComponentProgramming.ControlTextBox();
buttonGetObj = new Button();
buttonClear = new Button();
buttonEnter = new Button();
buttonAdd = new Button();
controlListBox = new ComponentProgramming.ControlListBox();
largeTextComponent = new ComponentProgramming.Components.LargeTextComponent(components);
SuspendLayout();
//
// controlComboBox
@ -58,7 +58,7 @@
//
// buttonGetObj
//
buttonGetObj.Location = new Point(23, 415);
buttonGetObj.Location = new Point(12, 415);
buttonGetObj.Name = "buttonGetObj";
buttonGetObj.Size = new Size(126, 23);
buttonGetObj.TabIndex = 3;
@ -66,16 +66,6 @@
buttonGetObj.UseVisualStyleBackColor = true;
buttonGetObj.Click += buttonGetObj_Click;
//
// buttonClear
//
buttonClear.Location = new Point(654, 415);
buttonClear.Name = "buttonClear";
buttonClear.Size = new Size(134, 26);
buttonClear.TabIndex = 4;
buttonClear.Text = "Очистить объекты";
buttonClear.UseVisualStyleBackColor = true;
buttonClear.Click += buttonClear_Click;
//
// buttonEnter
//
buttonEnter.Location = new Point(272, 36);
@ -87,34 +77,21 @@
buttonEnter.UseVisualStyleBackColor = true;
buttonEnter.Click += buttonEnter_Click;
//
// buttonAdd
//
buttonAdd.Location = new Point(331, 415);
buttonAdd.Margin = new Padding(3, 2, 3, 2);
buttonAdd.Name = "buttonAdd";
buttonAdd.Size = new Size(147, 23);
buttonAdd.TabIndex = 6;
buttonAdd.Text = "Добавить значение";
buttonAdd.UseVisualStyleBackColor = true;
buttonAdd.Click += buttonAdd_Click;
//
// controlListBox
//
controlListBox.GetIndex = -1;
controlListBox.Location = new Point(12, 65);
controlListBox.Name = "controlListBox";
controlListBox.Size = new Size(776, 344);
controlListBox.Size = new Size(334, 344);
controlListBox.TabIndex = 7;
//
// Form
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
ClientSize = new Size(759, 450);
Controls.Add(controlListBox);
Controls.Add(buttonAdd);
Controls.Add(buttonEnter);
Controls.Add(buttonClear);
Controls.Add(buttonGetObj);
Controls.Add(controlTextBox);
Controls.Add(controlComboBox);
@ -129,9 +106,8 @@
private ComponentProgramming.ControlComboBox controlComboBox;
private ComponentProgramming.ControlTextBox controlTextBox;
private Button buttonGetObj;
private Button buttonClear;
private Button buttonEnter;
private Button buttonAdd;
private ComponentProgramming.ControlListBox controlListBox;
private ComponentProgramming.Components.LargeTextComponent largeTextComponent;
}
}

View File

@ -8,6 +8,8 @@ namespace Forms
FillBox();
FillTextBox();
FillList();
string[] strings = new string[] { "У компонента должен быть публичный метод, который должен принимать на вход имя файла (включая путь до файла)", "название документа (заголовок в документе) и массив строк (каждая строка абзац текста в выходном документе или текст в ячейке для табличного документа)" };
largeTextComponent.CreateDocument("C:\\Users\\shotb\\source\\repos\\KOP\\ComponentProgramming\\Forms\\text.pdf", "Заголовок", strings);
}
private void FillBox()
@ -53,21 +55,10 @@ namespace Forms
var obj = controlListBox.GetSelectedObject<Person>();
MessageBox.Show($"{obj.Name} {obj.Surname}");
}
private void buttonClear_Click(object sender, EventArgs e)
{
//controlDataGrid.ClearData();
}
private void buttonEnter_Click(object sender, EventArgs e)
{
var val = controlTextBox.text;
MessageBox.Show($"Ââåäåíî {val}");
}
private void buttonAdd_Click(object sender, EventArgs e)
{
//controlDataGrid.SetData<Person>(new Person(1, "asdasdas", "asdasd1asd"), 2, 3);
}
}
}

View File

@ -117,4 +117,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="largeTextComponent.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>

View File

@ -8,6 +8,10 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="PDFsharp-MigraDoc-GDI" Version="6.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ComponentProgramming\ComponentProgramming.csproj" />
</ItemGroup>

Binary file not shown.