переписал обработку при изменении itemcheck

теперь обрабатывается после срабатывания
This commit is contained in:
ker73rus 2024-12-11 14:39:27 +04:00
parent 315c6f7249
commit 0221ad1e7a
5 changed files with 27 additions and 24 deletions

View File

@ -7,7 +7,7 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Authors>ns.potapov</Authors> <Authors>ns.potapov</Authors>
<VersionPrefix>1.0.6</VersionPrefix> <VersionPrefix>1.0.7</VersionPrefix>
<Version>$(VersionPrefix)</Version> <Version>$(VersionPrefix)</Version>
</PropertyGroup> </PropertyGroup>

View File

@ -36,22 +36,20 @@
checkedListBox.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; checkedListBox.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
checkedListBox.CheckOnClick = true; checkedListBox.CheckOnClick = true;
checkedListBox.FormattingEnabled = true; checkedListBox.FormattingEnabled = true;
checkedListBox.Location = new Point(9, 8); checkedListBox.Location = new Point(8, 6);
checkedListBox.Margin = new Padding(3, 4, 3, 4);
checkedListBox.Name = "checkedListBox"; checkedListBox.Name = "checkedListBox";
checkedListBox.Size = new Size(509, 400); checkedListBox.Size = new Size(446, 292);
checkedListBox.TabIndex = 0; checkedListBox.TabIndex = 0;
checkedListBox.ItemCheck += checkedListBox_ItemCheck; checkedListBox.ItemCheck += checkedListBox_ItemCheck;
// //
// UserControlCheckedList // UserControlCheckedList
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
BackColor = Color.Firebrick; BackColor = Color.Firebrick;
Controls.Add(checkedListBox); Controls.Add(checkedListBox);
Margin = new Padding(3, 4, 3, 4);
Name = "UserControlCheckedList"; Name = "UserControlCheckedList";
Size = new Size(527, 415); Size = new Size(461, 311);
ResumeLayout(false); ResumeLayout(false);
} }

View File

@ -1,4 +1,5 @@
using Components.Exceptions; using Components.Exceptions;
using System.Windows.Forms;
namespace Components namespace Components
{ {
@ -44,12 +45,12 @@ namespace Components
} }
} }
} }
public List<string> CheckedItems public List<string> CheckedItemss
{ {
get get
{ {
List<string> list = new List<string>(); List<string> list = new List<string>();
foreach(var item in checkedListBox.CheckedItems ) foreach (var item in checkedListBox.CheckedItems)
{ {
list.Add(item.ToString()); list.Add(item.ToString());
} }
@ -83,21 +84,25 @@ namespace Components
checkedListBox.Items.Clear(); checkedListBox.Items.Clear();
} }
private void checkedListBox_ItemCheck(object sender, ItemCheckEventArgs e) private void checkedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
{
this.BeginInvoke((MethodInvoker)(
() =>
{ {
if (checkedListBox.CheckedItems.Count > MaxCheckedItemsCount) if (checkedListBox.CheckedItems.Count > MaxCheckedItemsCount)
{ {
if (typeOfMaxChecked == Types.Exception) if (typeOfMaxChecked == Types.Exception)
{ {
MessageBox.Show("Превышено максимальное число выбранных элементов");
checkedListBox.SetItemChecked(e.Index, false); checkedListBox.SetItemChecked(e.Index, false);
throw new MaxCheckedItemsCountExceedException("Превышено максимальное число выбранных элементов");
} }
else if (typeOfMaxChecked == Types.DeleteFirst) else if (typeOfMaxChecked == Types.DeleteFirst)
{ {
checkedListBox.SetItemChecked(0, false); checkedListBox.SetItemCheckState(checkedListBox.Items.IndexOf(CheckedItem), CheckState.Unchecked);
} }
} }
_onCheckedItemChangedEvent?.Invoke(sender, e); _onCheckedItemChangedEvent?.Invoke(sender, e);
}));
} }
} }
} }

View File

@ -18,8 +18,8 @@ namespace WinFormsApp
list.Add("Çíàòü"); list.Add("Çíàòü");
list.Add("C#"); list.Add("C#");
userControlCheckedList.SetCheckedListBoxValues(list); userControlCheckedList.SetCheckedListBoxValues(list);
userControlCheckedList.MaxCheckedItemsCount = 3;
userControlCheckedList.CheckedItem = list[3]; userControlCheckedList.ChangeTypeOfMaxChecked(Components.Types.DeleteFirst);
} }
private void buttonClearList_Click(object sender, EventArgs e) private void buttonClearList_Click(object sender, EventArgs e)

View File

@ -11,7 +11,7 @@ namespace WinFormsApp
// To customize application configuration such as set high DPI settings or default font, // To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration. // see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
Application.Run(new FormNoVisual()); Application.Run(new FormVisual());
} }
} }
} }