Сделал первый компонент

This commit is contained in:
2025-09-07 10:56:17 +04:00
parent b432ceb7ad
commit a372eeee57
5 changed files with 87 additions and 48 deletions

View File

@@ -0,0 +1,37 @@
namespace WinFormsControlLibrary1
{
partial class ComboSingleAddControl
{
/// <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();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
}
#endregion
}
}

View File

@@ -0,0 +1,50 @@
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 WinFormsControlLibrary1;
[DefaultEvent(nameof(SelectedValueChanged))]
public partial class ComboSingleAddControl : UserControl
{
private readonly ComboBox _combo = new();
public EventHandler? SelectedValueChanged;
public ComboSingleAddControl()
{
InitializeComponent();
Controls.Add(_combo);
_combo.SelectedIndexChanged += (_, __) => SelectedValueChanged?.Invoke(this, EventArgs.Empty);
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string SelectedValue
{
get => _combo.SelectedIndex.ToString() ?? string.Empty;
set
{
if(string.IsNullOrEmpty(value)) { _combo.SelectedIndex = -1; return; }
int id = _combo.Items.IndexOf(value);
if(id >= 0) _combo.SelectedIndex = id;
}
}
public void AddValue(string value)
{
if (string.IsNullOrEmpty(value)) return;
if (_combo.Items.Contains(value)) return;
_combo.Items.Add(value);
if(_combo.SelectedIndex < 0) _combo.SelectedIndex = 0;
}
public void ClearItems()
{
_combo.Items.Clear();
_combo.SelectedIndex = -1;
}
}

View File

@@ -1,38 +0,0 @@
namespace WinFormsControlLibrary1
{
partial class UserControl1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
}
#endregion
}
}

View File

@@ -1,10 +0,0 @@
namespace WinFormsControlLibrary1
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
}
}