первая лаба

This commit is contained in:
Данила Селяев 2024-11-06 18:18:12 +04:00
parent e9ac95bb9f
commit e49f5b1108
7 changed files with 242 additions and 125 deletions

View File

@ -1,69 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace VisComLib
{
public partial class UserComboBox : UserControl
{
public UserComboBox()
{
InitializeComponent();
}
public void ComboBoxClear()
{
comboBox1.Items.Clear();
comboBox1.SelectedItem = null;
}
public string SelectedItem
{
get
{
if (comboBox1.Items.Count == 0)
{
return " ";
}
if (comboBox1.SelectedItem == null)
{
return " ";
}
return comboBox1.SelectedItem.ToString()!;
}
set
{
if (comboBox1.Items.Contains(value))
{
comboBox1.SelectedItem = value;
}
}
}
public ComboBox.ObjectCollection ComboBoxItems
{
get { return comboBox1.Items; }
}
private EventHandler _onValueChangedEvent;
public event EventHandler ValueChanged
{
add
{
_onValueChangedEvent += value;
}
remove
{
_onValueChangedEvent -= value;
}
}
private void CustomComboBox_SelectedValueChanged(object sender, EventArgs e)
{
_onValueChangedEvent?.Invoke(sender, e);
}
}
}

View File

@ -1,6 +1,6 @@
namespace VisComLib
namespace VisComLib.Components
{
partial class UserComboBox
partial class UserListBox
{
/// <summary>
/// Обязательная переменная конструктора.
@ -28,29 +28,30 @@
/// </summary>
private void InitializeComponent()
{
comboBox1 = new ComboBox();
listBox1 = new ListBox();
SuspendLayout();
//
// comboBox1
// listBox1
//
comboBox1.FormattingEnabled = true;
comboBox1.Location = new Point(3, 3);
comboBox1.Name = "comboBox1";
comboBox1.Size = new Size(299, 23);
comboBox1.TabIndex = 0;
listBox1.FormattingEnabled = true;
listBox1.ItemHeight = 15;
listBox1.Location = new Point(3, 3);
listBox1.Name = "listBox1";
listBox1.Size = new Size(367, 139);
listBox1.TabIndex = 0;
//
// UserComboBox
// UserListBox
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
Controls.Add(comboBox1);
Name = "UserComboBox";
Size = new Size(305, 146);
Controls.Add(listBox1);
Name = "UserListBox";
Size = new Size(373, 154);
ResumeLayout(false);
}
#endregion
private ComboBox comboBox1;
private ListBox listBox1;
}
}

View File

@ -0,0 +1,137 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace VisComLib.Components
{
public partial class UserListBox : UserControl
{
private string template;
private string start;
private string end;
public UserListBox()
{
InitializeComponent();
}
public void setTemplate(string _template, string _start, string _end)
{
if (string.IsNullOrEmpty(_template) || string.IsNullOrEmpty(_start) || string.IsNullOrEmpty(_end))
{
throw new ArgumentNullException("Wrong template");
}
template = _template;
start = _start;
end = _end;
}
public int SelectedRow
{
get
{
return listBox1.SelectedIndex;
}
set
{
if (value < 0) return;
listBox1.SelectedIndex = value;
}
}
public T GetObjectFromStr<T>() where T : class, new()
{
if (listBox1.SelectedIndex < 0)
{
return null;
}
string row = listBox1.SelectedItem.ToString();
T curObject = new T();
StringBuilder sb = new StringBuilder(row);
string[] words = template.Split(new[] { char.Parse(start), char.Parse(end) });
StringBuilder myrow = new StringBuilder(row);
List<string> flexPartsTemplate = new();
foreach (string word in words)
{
if (row.Contains(word) && !string.IsNullOrEmpty(word))
{
myrow.Replace(word, end);
}
else
{
flexPartsTemplate.Add(word);
}
}
string[] flexParts = myrow.ToString().Split(end);
int i = 1;
StringBuilder result = new StringBuilder(template);
foreach (string word in flexPartsTemplate)
{
if (!string.IsNullOrEmpty(word))
{
result.Replace(word, flexParts[i]);
i++;
}
}
sb = result;
foreach (var property in typeof(T).GetProperties())
{
if (!property.CanWrite)
{
continue;
}
int startBorder = sb.ToString().IndexOf(start);
if (startBorder == -1)
{
break;
}
int endBorder = sb.ToString().IndexOf(end, startBorder + 1);
if (endBorder == -1)
{
break;
}
string propertyValue = sb.ToString(startBorder + 1, endBorder - startBorder - 1);
sb.Remove(0, endBorder + 1);
property.SetValue(curObject, Convert.ChangeType(propertyValue, property.PropertyType));
}
return curObject;
}
public void FillProperty<T>(T dataObject, int rowIndex, string propertyName)
{
while (listBox1.Items.Count <= rowIndex)
{
listBox1.Items.Add(template);
}
string row = listBox1.Items[rowIndex].ToString();
PropertyInfo propertyInfo = dataObject.GetType().GetProperty(propertyName);
if (propertyInfo != null)
{
var propertyValue = propertyInfo.GetValue(dataObject);
row = row.Replace($"{start}{propertyName}{end}", propertyValue.ToString());
listBox1.Items[rowIndex] = row;
}
}
public int CountRows()
{
return listBox1.Items.Count;
}
}
}

View File

@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->

View File

@ -36,8 +36,8 @@
buttonTextBox = new Button();
userTextBox1 = new VisComLib.Components.UserTextBox();
labeltextbox = new Label();
userComboBox1 = new VisComLib.UserComboBox();
buttonadd = new Button();
userListBox1 = new VisComLib.Components.UserListBox();
SuspendLayout();
//
// userddList1
@ -113,31 +113,31 @@
labeltextbox.TabIndex = 8;
labeltextbox.Text = "label1";
//
// userComboBox1
//
userComboBox1.Location = new Point(14, 163);
userComboBox1.Name = "userComboBox1";
userComboBox1.SelectedItem = " ";
userComboBox1.Size = new Size(311, 159);
userComboBox1.TabIndex = 9;
//
// buttonadd
//
buttonadd.Location = new Point(14, 328);
buttonadd.Name = "buttonadd";
buttonadd.Size = new Size(75, 23);
buttonadd.TabIndex = 10;
buttonadd.Text = "button1";
buttonadd.Text = "Получить";
buttonadd.UseVisualStyleBackColor = true;
buttonadd.Click += buttonAdd_Click;
//
// userListBox1
//
userListBox1.Location = new Point(12, 168);
userListBox1.Name = "userListBox1";
userListBox1.SelectedRow = -1;
userListBox1.Size = new Size(373, 154);
userListBox1.TabIndex = 11;
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(337, 369);
ClientSize = new Size(389, 366);
Controls.Add(userListBox1);
Controls.Add(buttonadd);
Controls.Add(userComboBox1);
Controls.Add(labeltextbox);
Controls.Add(userTextBox1);
Controls.Add(buttonTextBox);
@ -162,7 +162,7 @@
private Button buttonTextBox;
private VisComLib.Components.UserTextBox userTextBox1;
private Label labeltextbox;
private VisComLib.UserComboBox userComboBox1;
private Button buttonadd;
private VisComLib.Components.UserListBox userListBox1;
}
}

View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using static System.Runtime.InteropServices.Marshalling.IIUnknownCacheStrategy;
@ -6,6 +7,7 @@ namespace WinForms
public partial class Form1 : Form
{
List<string> list = new List<string>() { "Áåòîí", "Êèðïè÷", "Äðåâåñèíà" };
List<Student> students;
public Form1()
{
InitializeComponent();
@ -15,10 +17,26 @@ namespace WinForms
}
userTextBox1.Pattern = @"^\+7\d{10}$";
userComboBox1.ComboBoxItems.Add("one");
userComboBox1.ComboBoxItems.Add("two");
userComboBox1.SelectedItem = "one";
students = new List<Student>() {
new Student("Èâàí", "Èëþõèí", "ÏÈáä-11"),
new Student("Àðòåì", "Àðòåìîâ", "ÏÈáä-11"),
new Student("Èëüÿ", "Êîìèêîâ", "ÏÈáä-12")
};
userListBox1.setTemplate("Çàïîëíèòü {FirstName} {LastName} èç {Group}", "{", "}");
userListBox1.FillProperty(students[0], 0, "FirstName");
userListBox1.FillProperty(students[0], 0, "LastName");
userListBox1.FillProperty(students[0], 0, "Group");
userListBox1.FillProperty(students[1], 1, "FirstName");
userListBox1.FillProperty(students[1], 1, "LastName");
userListBox1.FillProperty(students[1], 1, "Group");
userListBox1.FillProperty(students[2], 2, "FirstName");
userListBox1.FillProperty(students[2], 2, "LastName");
userListBox1.FillProperty(students[2], 2, "Group");
}
private void CustomEventHandler(object sender, EventArgs e)
{
MessageBox.Show("Âûáðàííûé ýëåìåíò èçìåíåí");
@ -46,7 +64,15 @@ namespace WinForms
}
private void buttonAdd_Click(object sender, EventArgs e)
{
userComboBox1.ComboBoxItems.Add("added");
try
{
Student selectedPerson = userListBox1.GetObjectFromStr<Student>();
MessageBox.Show($"Èíôîðìàöèÿ î ñòóäåíòå: Èìÿ - {selectedPerson.FirstName}, Ôàìèëèÿ - {selectedPerson.LastName}, Ãðóïïà - {selectedPerson.Group}");
}
catch (Exception ex)
{
MessageBox.Show($"Îøèáêà: {ex.Message}");
}
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WinForms
{
public class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Group { get; set; }
public Student(string fn, string ln, string gr)
{
FirstName = fn;
LastName = ln;
Group = gr;
}
public Student() { }
}
}