Create ComboBox

This commit is contained in:
Максим Яковлев 2024-09-05 23:14:57 +04:00
parent 16dee17c97
commit 47a5e5c726
7 changed files with 343 additions and 14 deletions

View File

@ -8,19 +8,28 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Forms; using System.Windows.Forms;
using static System.Runtime.InteropServices.JavaScript.JSType; using static System.Runtime.InteropServices.JavaScript.JSType;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace ComponentProgramming namespace ComponentProgramming
{ {
public partial class ControlComboBox : UserControl public partial class ControlComboBox : UserControl
{ {
private event EventHandler? _comboBoxChanged; private event EventHandler? _comboBoxChanged;
private event Action? _errorOccured;
public string Error { get; private set; }
public object?[] elements public string elements
{ {
get { return comboBoxElements.SelectedItem == null ? [" "] : [comboBoxElements.SelectedItem]; } get
set { comboBoxElements.Items.AddRange(value); } {
if (comboBoxElements.SelectedItem == null)
{
return "";
}
return comboBoxElements.SelectedItem.ToString()!;
}
set
{
comboBoxElements.Items.AddRange(value.Split(", "));
}
} }
public event EventHandler ComboBoxChanged public event EventHandler ComboBoxChanged
@ -28,16 +37,10 @@ namespace ComponentProgramming
add { _comboBoxChanged += value; } add { _comboBoxChanged += value; }
remove { _comboBoxChanged -= value; } remove { _comboBoxChanged -= value; }
} }
public event Action AnErrorOccured
{
add { _errorOccured += value; }
remove { _errorOccured -= value; }
}
public ControlComboBox() public ControlComboBox()
{ {
InitializeComponent(); InitializeComponent();
Error = string.Empty;
} }
public void ClearComboBox() public void ClearComboBox()

View File

@ -0,0 +1,70 @@
namespace ComponentProgramming
{
partial class ControlTextBox
{
/// <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()
{
textBox = new TextBox();
checkBox = new CheckBox();
SuspendLayout();
//
// textBox
//
textBox.Location = new Point(30, 3);
textBox.Name = "textBox";
textBox.Size = new Size(117, 23);
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;
//
// ControlTextBox
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
Controls.Add(checkBox);
Controls.Add(textBox);
Name = "ControlTextBox";
Size = new Size(150, 29);
ResumeLayout(false);
PerformLayout();
}
#endregion
private TextBox textBox;
private CheckBox checkBox;
}
}

View File

@ -0,0 +1,89 @@
using ComponentProgramming.Exceptions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ComponentProgramming
{
public partial class ControlTextBox : UserControl
{
private Regex regex = new(@"^\+7\d{10}$");
public string? text
{
get
{
if (checkBox.Checked)
{
return textBox.Text = null;
}
if (regex.IsMatch(textBox.Text))
{
return textBox.Text;
}
else
{
throw new NumberException(textBox.Text + " не соответствует стандарту!");
}
}
set
{
if (checkBox.Checked)
{
textBox.Text = null;
}
if (regex.IsMatch(value!))
{
textBox.Text = value;
}
else
{
throw new NumberException(textBox.Text + " не соответствует стандарту!");
}
}
}
public ControlTextBox()
{
InitializeComponent();
}
private event EventHandler? _checkBoxChanged;
public event EventHandler CheckBoxChanged
{
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;
}
}
}
}

View File

@ -0,0 +1,120 @@
<?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
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<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
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
mimetype set.
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
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
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
: 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
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ComponentProgramming.Exceptions
{
public class NumberException : Exception
{
public NumberException() { }
public NumberException(string message) : base(message)
{
}
}
}

View File

@ -29,21 +29,32 @@
private void InitializeComponent() private void InitializeComponent()
{ {
controlComboBox = new ComponentProgramming.ControlComboBox(); controlComboBox = new ComponentProgramming.ControlComboBox();
controlTextBox = new ComponentProgramming.ControlTextBox();
SuspendLayout(); SuspendLayout();
// //
// controlComboBox // controlComboBox
// //
controlComboBox.elements = "";
controlComboBox.Location = new Point(12, 3); controlComboBox.Location = new Point(12, 3);
controlComboBox.Name = "controlComboBox"; controlComboBox.Name = "controlComboBox";
controlComboBox.Size = new Size(177, 31); controlComboBox.Size = new Size(177, 31);
controlComboBox.TabIndex = 0; controlComboBox.TabIndex = 0;
controlComboBox.ComboBoxChanged += controlComboBox_ComboBoxChanged; controlComboBox.ComboBoxChanged += controlComboBox_ComboBoxChanged;
// //
// controlTextBox
//
controlTextBox.Location = new Point(226, 3);
controlTextBox.Name = "controlTextBox";
controlTextBox.Size = new Size(150, 29);
controlTextBox.TabIndex = 1;
controlTextBox.CheckBoxChanged += controlTextBox_CheckBoxChanged;
//
// Form // Form
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450); ClientSize = new Size(800, 450);
Controls.Add(controlTextBox);
Controls.Add(controlComboBox); Controls.Add(controlComboBox);
Name = "Form"; Name = "Form";
Text = "Form"; Text = "Form";
@ -54,5 +65,6 @@
private ComponentProgramming.ControlImage control; private ComponentProgramming.ControlImage control;
private ComponentProgramming.ControlComboBox controlComboBox; private ComponentProgramming.ControlComboBox controlComboBox;
private ComponentProgramming.ControlTextBox controlTextBox;
} }
} }

View File

@ -6,17 +6,34 @@ namespace Forms
{ {
InitializeComponent(); InitializeComponent();
FillBox(); FillBox();
FillTextBox();
} }
private void FillBox() private void FillBox()
{ {
controlComboBox.elements = new object[] {"Çíà÷åíèå 1", "Çíà÷åíèå 2", "Çíà÷åíèå 3", "Çíà÷åíèå 4" }; controlComboBox.elements = "Çíà÷åíèå 1, Çíà÷åíèå 2, Çíà÷åíèå 3, Çíà÷åíèå 4";
}
private void FillTextBox()
{
controlTextBox.text = "+79063908075";
} }
private void controlComboBox_ComboBoxChanged(object sender, EventArgs e) private void controlComboBox_ComboBoxChanged(object sender, EventArgs e)
{ {
var elem = controlComboBox.elements[0]; var elem = controlComboBox.elements;
MessageBox.Show($"Âûáðàííî: {elem?.ToString()}"); MessageBox.Show($"Âûáðàííî: {elem}");
}
private void controlTextBox_CheckBoxChanged(object sender, EventArgs e)
{
if (controlTextBox.text == null)
{
MessageBox.Show($"CheckBox checked");
}
else
{
MessageBox.Show($"CheckBox not checked");
}
} }
} }
} }