правки на занятии

This commit is contained in:
Максим Яковлев 2024-09-06 17:40:13 +04:00
parent 6b9f5e4ac7
commit 0d948ba72e
9 changed files with 173 additions and 96 deletions

View File

@ -34,20 +34,23 @@
// comboBoxElements
//
comboBoxElements.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
comboBoxElements.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxElements.FormattingEnabled = true;
comboBoxElements.Location = new Point(4, 3);
comboBoxElements.Location = new Point(5, 4);
comboBoxElements.Margin = new Padding(3, 4, 3, 4);
comboBoxElements.Name = "comboBoxElements";
comboBoxElements.Size = new Size(170, 23);
comboBoxElements.Size = new Size(194, 28);
comboBoxElements.TabIndex = 0;
comboBoxElements.SelectedIndexChanged += comboBoxElements_SelectedIndexChanged;
//
// ControlComboBox
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
Controls.Add(comboBoxElements);
Margin = new Padding(3, 4, 3, 4);
Name = "ControlComboBox";
Size = new Size(177, 29);
Size = new Size(202, 39);
ResumeLayout(false);
}

View File

@ -8,7 +8,6 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Runtime.InteropServices.JavaScript.JSType;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace ComponentProgramming
{
@ -16,7 +15,7 @@ namespace ComponentProgramming
{
private event EventHandler? _comboBoxChanged;
public string elements
public string SelectedItem
{
get
{
@ -28,7 +27,7 @@ namespace ComponentProgramming
}
set
{
comboBoxElements.Items.AddRange(value.Split(", "));
comboBoxElements.SelectedItem = value;
}
}
@ -43,6 +42,11 @@ namespace ComponentProgramming
InitializeComponent();
}
public ComboBox.ObjectCollection ComboBoxItems
{
get { return comboBoxElements.Items; }
}
public void ClearComboBox()
{
comboBoxElements.Items.Clear();

View File

@ -1,4 +1,5 @@
using System;
using Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@ -12,6 +13,8 @@ namespace ComponentProgramming
{
public partial class ControlDataGrid : UserControl
{
private List<string> _props;
private int _numCols;
public ControlDataGrid()
{
InitializeComponent();
@ -27,9 +30,10 @@ namespace ComponentProgramming
dataGridView.Columns[i].Visible = isVisible[i];
dataGridView.Columns[i].Name = props[i];
}
dataGridView.Rows.Add("1", "sadasd");
dataGridView.Rows.Add("2", "sadsadfasdfasdfasd");
_props = props;
_numCols = numCol;
dataGridView.Rows.Add(new object[] { 1, "Александр", "Петров" });
dataGridView.Rows.Add(new object[] { 2, "Владимир", "Кузьмин" });
}
public void ClearData()
@ -37,7 +41,7 @@ namespace ComponentProgramming
dataGridView.Rows.Clear();
}
public int SelectedRow
public int? SelectedRow
{
get
{
@ -47,12 +51,28 @@ namespace ComponentProgramming
}
else
{
throw new Exception($"Вы не выбрали строку");
throw new Exception($"Вы не выбрали строку"); ;
}
}
set
{
dataGridView.Columns[0].DisplayIndex = value;
dataGridView.Columns[0].DisplayIndex = (int)value!;
}
}
public void SetData<T>(T obj, int col, int row) where T : class, new()
{
if(row > dataGridView.Rows.Count-1)
{
dataGridView.Rows.Add(new object[] { null, "", "" });
row = dataGridView.Rows.Count -1;
}
foreach (var prop in obj.GetType().GetProperties())
{
if (prop.ToString()!.Contains(_props[col]))
{
dataGridView.Rows[row].Cells[col].Value = prop.GetValue(obj);
}
}
}
@ -63,7 +83,7 @@ namespace ComponentProgramming
return null;
}
DataGridViewRow selectedString = dataGridView.SelectedRows[0];
DataGridViewRow selectedRow = dataGridView.SelectedRows[0];
T obj = new T();
foreach (var prop in obj.GetType().GetProperties())
@ -72,7 +92,7 @@ namespace ComponentProgramming
{
continue;
}
prop.SetValue(obj, selectedString.Cells[$"{prop.Name}"].Value);
prop.SetValue(obj, selectedRow.Cells[$"{prop.Name}"].Value);
}
return obj;
}

View File

@ -29,35 +29,26 @@
private void InitializeComponent()
{
textBox = new TextBox();
checkBox = new CheckBox();
SuspendLayout();
//
// textBox
//
textBox.Location = new Point(30, 3);
textBox.Location = new Point(3, 4);
textBox.Margin = new Padding(3, 4, 3, 4);
textBox.Name = "textBox";
textBox.Size = new Size(117, 23);
textBox.PlaceholderText = "+79063211213";
textBox.Size = new Size(133, 27);
textBox.TabIndex = 0;
textBox.KeyPress += textBox_KeyPress;
//
// checkBox
//
checkBox.AutoSize = true;
checkBox.Location = new Point(9, 7);
checkBox.Name = "checkBox";
checkBox.Size = new Size(15, 14);
checkBox.TabIndex = 1;
checkBox.UseVisualStyleBackColor = true;
checkBox.CheckedChanged += checkBox_CheckedChanged;
textBox.TextChanged += textBox_TextChanged;
//
// ControlTextBox
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
Controls.Add(checkBox);
Controls.Add(textBox);
Margin = new Padding(3, 4, 3, 4);
Name = "ControlTextBox";
Size = new Size(150, 29);
Size = new Size(138, 34);
ResumeLayout(false);
PerformLayout();
}
@ -65,6 +56,5 @@
#endregion
private TextBox textBox;
private CheckBox checkBox;
}
}

View File

@ -14,18 +14,28 @@ namespace ComponentProgramming
{
public partial class ControlTextBox : UserControl
{
public ControlTextBox()
{
InitializeComponent();
}
private string? _numPuttern;
private Regex regex = new(@"^\+7\d{10}$");
public string? NumPattern
{
get { return _numPuttern; }
set { _numPuttern = value!; }
}
public string? text
{
get
{
if (checkBox.Checked)
if (_numPuttern == null)
{
return textBox.Text = null;
throw new NumberException("Стандарт не задан!");
}
Regex regex = new(_numPuttern);
if (regex.IsMatch(textBox.Text))
{
return textBox.Text;
@ -37,53 +47,29 @@ namespace ComponentProgramming
}
set
{
if (checkBox.Checked)
if (_numPuttern == null)
{
textBox.Text = null;
return;
}
Regex regex = new(_numPuttern!);
if (regex.IsMatch(value!))
{
textBox.Text = value;
}
else
{
throw new NumberException(textBox.Text + " не соответствует стандарту!");
}
}
}
public ControlTextBox()
private event EventHandler? _textBoxChanged;
public event EventHandler TextBoxChanged
{
InitializeComponent();
add { _textBoxChanged += value; }
remove { _textBoxChanged -= value; }
}
private event EventHandler? _checkBoxChanged;
public event EventHandler CheckBoxChanged
private void textBox_TextChanged(object sender, EventArgs e)
{
add { _checkBoxChanged += value; }
remove { _checkBoxChanged -= value; }
}
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
if (checkBox.Checked)
{
textBox.Enabled = false;
_checkBoxChanged?.Invoke(this, e);
}
else
{
textBox.Enabled = true;
}
}
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == '\r')
{
text = textBox.Text;
}
_textBoxChanged?.Invoke(this, e);
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Forms
{
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public Person() { }
public Person(int id, string name, string surname)
{
Name = name;
Id = id;
Surname = surname;
}
}
}

View File

@ -33,37 +33,43 @@
controlDataGrid = new ComponentProgramming.ControlDataGrid();
buttonGetObj = new Button();
buttonClear = new Button();
buttonEnter = new Button();
buttonAdd = new Button();
SuspendLayout();
//
// controlComboBox
//
controlComboBox.elements = "";
controlComboBox.Location = new Point(12, 3);
controlComboBox.SelectedItem = "";
controlComboBox.Location = new Point(14, 4);
controlComboBox.Margin = new Padding(3, 5, 3, 5);
controlComboBox.Name = "controlComboBox";
controlComboBox.Size = new Size(177, 31);
controlComboBox.Size = new Size(202, 41);
controlComboBox.TabIndex = 0;
controlComboBox.ComboBoxChanged += controlComboBox_ComboBoxChanged;
//
// controlTextBox
//
controlTextBox.Location = new Point(226, 3);
controlTextBox.Location = new Point(258, 4);
controlTextBox.Margin = new Padding(3, 5, 3, 5);
controlTextBox.Name = "controlTextBox";
controlTextBox.Size = new Size(150, 29);
controlTextBox.Size = new Size(171, 39);
controlTextBox.TabIndex = 1;
controlTextBox.CheckBoxChanged += controlTextBox_CheckBoxChanged;
//
// controlDataGrid
//
controlDataGrid.Location = new Point(12, 111);
controlDataGrid.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
controlDataGrid.Location = new Point(9, 148);
controlDataGrid.Margin = new Padding(3, 5, 3, 5);
controlDataGrid.Name = "controlDataGrid";
controlDataGrid.Size = new Size(776, 296);
controlDataGrid.Size = new Size(899, 395);
controlDataGrid.TabIndex = 2;
//
// buttonGetObj
//
buttonGetObj.Location = new Point(23, 415);
buttonGetObj.Location = new Point(26, 553);
buttonGetObj.Margin = new Padding(3, 4, 3, 4);
buttonGetObj.Name = "buttonGetObj";
buttonGetObj.Size = new Size(126, 23);
buttonGetObj.Size = new Size(144, 31);
buttonGetObj.TabIndex = 3;
buttonGetObj.Text = "Получить объект";
buttonGetObj.UseVisualStyleBackColor = true;
@ -71,24 +77,48 @@
//
// buttonClear
//
buttonClear.Location = new Point(654, 415);
buttonClear.Location = new Point(747, 553);
buttonClear.Margin = new Padding(3, 4, 3, 4);
buttonClear.Name = "buttonClear";
buttonClear.Size = new Size(134, 23);
buttonClear.Size = new Size(153, 34);
buttonClear.TabIndex = 4;
buttonClear.Text = "Очистить объекты";
buttonClear.UseVisualStyleBackColor = true;
buttonClear.Click += buttonClear_Click;
//
// buttonEnter
//
buttonEnter.Location = new Point(311, 48);
buttonEnter.Name = "buttonEnter";
buttonEnter.Size = new Size(84, 32);
buttonEnter.TabIndex = 5;
buttonEnter.Text = "Ввод";
buttonEnter.UseVisualStyleBackColor = true;
buttonEnter.Click += buttonEnter_Click;
//
// buttonAdd
//
buttonAdd.Location = new Point(378, 553);
buttonAdd.Name = "buttonAdd";
buttonAdd.Size = new Size(168, 31);
buttonAdd.TabIndex = 6;
buttonAdd.Text = "Добавить значение";
buttonAdd.UseVisualStyleBackColor = true;
buttonAdd.Click += buttonAdd_Click;
//
// Form
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
ClientSize = new Size(914, 600);
Controls.Add(buttonAdd);
Controls.Add(buttonEnter);
Controls.Add(buttonClear);
Controls.Add(buttonGetObj);
Controls.Add(controlDataGrid);
Controls.Add(controlTextBox);
Controls.Add(controlComboBox);
Margin = new Padding(3, 4, 3, 4);
Name = "Form";
Text = "Form";
ResumeLayout(false);
@ -102,5 +132,7 @@
private ComponentProgramming.ControlDataGrid controlDataGrid;
private Button buttonGetObj;
private Button buttonClear;
private Button buttonEnter;
private Button buttonAdd;
}
}

View File

@ -12,24 +12,29 @@ namespace Forms
private void FillBox()
{
controlComboBox.elements = "Çíà÷åíèå 1, Çíà÷åíèå 2, Çíà÷åíèå 3, Çíà÷åíèå 4";
controlComboBox.ComboBoxItems.Add("Çíà÷åíèå 1");
controlComboBox.ComboBoxItems.Add("Çíà÷åíèå 2");
controlComboBox.ComboBoxItems.Add("Çíà÷åíèå 3");
controlComboBox.ComboBoxItems.Add("Çíà÷åíèå 4");
controlComboBox.SelectedItem = "dafafadsf";
}
private void FillTextBox()
{
controlTextBox.NumPattern = @"^\+7\d{10}$";
controlTextBox.text = "+79063908075";
}
private void FillGrid()
{
List<string> headers = new List<string>() { "Id", "Èìÿ" };
List<int> width = new List<int>() { 200, 200 };
List<bool> isVisible = new List<bool>() { true, true };
List<string> props = new List<string>() { "Id", "Name" };
controlDataGrid.CreateColumns(2, headers, width, isVisible, props);
List<string> headers = new List<string>() { "Id", "Èìÿ", "Ôàìèìëèÿ" };
List<int> width = new List<int>() { 100, 200, 200 };
List<bool> isVisible = new List<bool>() { true, true, true };
List<string> props = new List<string>() { "Id", "Name", "Surname" };
controlDataGrid.CreateColumns(3, headers, width, isVisible, props);
}
private void controlComboBox_ComboBoxChanged(object sender, EventArgs e)
{
var elem = controlComboBox.elements;
var elem = controlComboBox.SelectedItem;
MessageBox.Show($"Âűáđŕííî: {elem}");
}
@ -47,13 +52,24 @@ namespace Forms
private void buttonGetObj_Click(object sender, EventArgs e)
{
var da = controlDataGrid.GetObject<Person>();
MessageBox.Show($"{da?.Id}:{da?.Name} ");
var val = controlDataGrid.GetObject<Person>();
MessageBox.Show($"{val?.Name} {val?.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

@ -8,15 +8,17 @@ namespace Forms
{
public class Person
{
public string Id { get; set; }
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public Person() { }
public Person(string name, string id)
public Person(int id, string name, string surname)
{
Name = name;
Id = id;
Surname = surname;
}
}
}