1 компонент

This commit is contained in:
ityurner02@mail.ru 2023-09-15 19:31:01 +04:00
parent a292f7107d
commit 96c441c7aa
6 changed files with 143 additions and 66 deletions

View File

@ -28,32 +28,32 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.comboBox = new System.Windows.Forms.ComboBox(); this.dropDownList = new System.Windows.Forms.ComboBox();
this.SuspendLayout(); this.SuspendLayout();
// //
// comboBox // dropDownList
// //
this.comboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.dropDownList.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox.FormattingEnabled = true; this.dropDownList.FormattingEnabled = true;
this.comboBox.Location = new System.Drawing.Point(3, 3); this.dropDownList.Location = new System.Drawing.Point(3, 3);
this.comboBox.Name = "comboBox"; this.dropDownList.Name = "dropDownList";
this.comboBox.Size = new System.Drawing.Size(187, 24); this.dropDownList.Size = new System.Drawing.Size(187, 24);
this.comboBox.TabIndex = 0; this.dropDownList.TabIndex = 0;
this.comboBox.SelectedValueChanged += new System.EventHandler(this.comboBox_SelectedValueChanged); this.dropDownList.SelectedValueChanged += new System.EventHandler(this.comboBox_SelectedValueChanged);
// //
// MyDropDownList // MyDropDownList
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.comboBox); this.Controls.Add(this.dropDownList);
this.Name = "MyDropDownList"; this.Name = "MyDropDownList";
this.Size = new System.Drawing.Size(800, 450); this.Size = new System.Drawing.Size(193, 30);
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout();
} }
#endregion #endregion
private System.Windows.Forms.ComboBox comboBox; private System.Windows.Forms.ComboBox dropDownList;
} }
} }

View File

@ -23,54 +23,39 @@ namespace VisualComponentsLib
{ {
return; return;
} }
comboBox.Items.AddRange(Values.ToArray()); dropDownList.Items.AddRange(Values.ToArray());
} }
//Отдельный публичный метод отчистки списка.
public void Clear() public void Clear()
{ {
comboBox.Items.Clear(); dropDownList.Items.Clear();
} }
//публичное свойство(set, get) для установки и получения выбранного значения (возвращает пустую строку, если нет выбранного значения)
public string SelectedValue public string SelectedValue
{ {
get get
{ {
if (comboBox.Items.Count == 0) if (dropDownList.Items.Count == 0)
{ {
return ""; return "";
} }
if (comboBox.SelectedItem == null) if (dropDownList.SelectedItem == null)
{ {
return ""; return "";
} }
return comboBox.SelectedItem.ToString(); return dropDownList.SelectedItem.ToString();
} }
set set
{ {
if (comboBox.Items.Contains(value)) if (dropDownList.Items.Contains(value))
{ {
comboBox.SelectedItem = value; dropDownList.SelectedItem = value;
} }
} }
} }
private EventHandler _event;
public event EventHandler Event
{
add
{
_event += value;
}
remove
{
_event -= value;
}
}
//событие, вызываемое при смене значения в ComboBox.
private void comboBox_SelectedValueChanged(object sender, EventArgs e) private void comboBox_SelectedValueChanged(object sender, EventArgs e)
{ {
_event?.Invoke(sender, e); if (dropDownList.BackColor == Color.LightGreen) dropDownList.BackColor = Color.Tomato;
else dropDownList.BackColor = Color.LightGreen;
} }
} }
} }

View File

@ -28,12 +28,82 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.components = new System.ComponentModel.Container(); dropDownList = new VisualComponentsLib.MyDropDownList();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; buttonAdd = new Button();
this.ClientSize = new System.Drawing.Size(800, 450); buttonInfo = new Button();
this.Text = "Form1"; labelInfo = new Label();
buttonClear = new Button();
SuspendLayout();
//
// dropDownList
//
dropDownList.Location = new Point(3, 3);
dropDownList.Margin = new Padding(3, 4, 3, 4);
dropDownList.Name = "dropDownList";
dropDownList.SelectedValue = "";
dropDownList.Size = new Size(196, 36);
dropDownList.TabIndex = 0;
//
// buttonAdd
//
buttonAdd.Location = new Point(3, 46);
buttonAdd.Name = "buttonAdd";
buttonAdd.Size = new Size(92, 29);
buttonAdd.TabIndex = 1;
buttonAdd.Text = "добавить";
buttonAdd.UseVisualStyleBackColor = true;
buttonAdd.Click += buttonAdd_Click;
//
// buttonInfo
//
buttonInfo.Location = new Point(211, 46);
buttonInfo.Name = "buttonInfo";
buttonInfo.Size = new Size(94, 29);
buttonInfo.TabIndex = 2;
buttonInfo.Text = "показать";
buttonInfo.UseVisualStyleBackColor = true;
buttonInfo.Click += buttonInfo_Click;
//
// labelInfo
//
labelInfo.AutoSize = true;
labelInfo.Location = new Point(205, 9);
labelInfo.Name = "labelInfo";
labelInfo.Size = new Size(154, 20);
labelInfo.TabIndex = 3;
labelInfo.Text = "Выбранный элемент";
//
// buttonClear
//
buttonClear.Location = new Point(101, 46);
buttonClear.Name = "buttonClear";
buttonClear.Size = new Size(94, 29);
buttonClear.TabIndex = 4;
buttonClear.Text = "очистить";
buttonClear.UseVisualStyleBackColor = true;
buttonClear.Click += buttonClear_Click;
//
// Form1
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450);
Controls.Add(dropDownList);
Controls.Add(buttonAdd);
Controls.Add(buttonInfo);
Controls.Add(labelInfo);
Controls.Add(buttonClear);
Name = "Form1";
Text = "Form1";
ResumeLayout(false);
PerformLayout();
} }
#endregion #endregion
private VisualComponentsLib.MyDropDownList dropDownList;
private Button buttonAdd;
private Button buttonInfo;
private Label labelInfo;
private Button buttonClear;
} }
} }

View File

@ -2,9 +2,27 @@ namespace WinForms
{ {
public partial class Form1 : Form public partial class Form1 : Form
{ {
List<string> list = new List<string>();
public Form1() public Form1()
{ {
list = new List<string>();
list.AddRange(new string[] { "õëåá", "ìîëîêî", "êîëáàñà" });
InitializeComponent(); InitializeComponent();
dropDownList.LoadValues(new List<string>() { "ñîê", "ÿáëîêî", "ëóê" });
}
private void buttonAdd_Click(object sender, EventArgs e)
{
dropDownList.LoadValues(list);
}
private void buttonInfo_Click(object sender, EventArgs e)
{
labelInfo.Text = dropDownList.SelectedValue;
}
private void buttonClear_Click(object sender, EventArgs e)
{
dropDownList.Clear();
} }
} }
} }

View File

@ -8,4 +8,8 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\VisualComponentsLib\VisualComponentsLib.csproj" />
</ItemGroup>
</Project> </Project>