10 Commits

6 changed files with 75 additions and 25 deletions

View File

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

View File

@@ -0,0 +1,13 @@
namespace Components.Exceptions
{
internal class MaxCheckedItemsCountExceedException : Exception
{
public MaxCheckedItemsCountExceedException()
{
}
public MaxCheckedItemsCountExceedException(string? message) : base(message)
{
}
}
}

View File

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

View File

@@ -1,10 +1,25 @@
namespace Components
using Components.Exceptions;
using System.Windows.Forms;
namespace Components
{
public enum Types : int
{
Exception = 0,
DeleteFirst = 1,
}
public partial class UserControlCheckedList : UserControl
{
private List<int> _lastCheckedIndexes = new List<int>();
public int MaxCheckedItemsCount { get; set; } = 1;
private Types typeOfMaxChecked = Types.DeleteFirst;
private EventHandler _onCheckedItemChangedEvent;
public void ChangeTypeOfMaxChecked(Types type)
{
typeOfMaxChecked = type;
}
public event EventHandler CheckedItemChanged
{
add
@@ -30,6 +45,28 @@
}
}
}
public List<string> CheckedItemss
{
get
{
List<string> list = new List<string>();
foreach (var item in checkedListBox.CheckedItems)
{
list.Add(item.ToString());
}
return list;
}
set
{
foreach (var item in value)
{
if (checkedListBox.Items.Contains(item))
{
checkedListBox.SetItemCheckState(checkedListBox.Items.IndexOf(item), CheckState.Checked);
}
}
}
}
public UserControlCheckedList()
{
InitializeComponent();
@@ -45,25 +82,27 @@
public void ClearCheckedListBoxValues()
{
checkedListBox.Items.Clear();
_lastCheckedIndexes.Clear();
}
private void checkedListBox_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
this.BeginInvoke((MethodInvoker)(
() =>
{
_lastCheckedIndexes.Add(e.Index);
while (_lastCheckedIndexes.Count > MaxCheckedItemsCount)
if (checkedListBox.CheckedItems.Count > MaxCheckedItemsCount)
{
checkedListBox.SetItemCheckState(_lastCheckedIndexes[0], CheckState.Unchecked);
}
}
else if (e.NewValue == CheckState.Unchecked)
{
_lastCheckedIndexes.Remove(e.Index);
}
if (typeOfMaxChecked == Types.Exception)
{
MessageBox.Show("Превышено максимальное число выбранных элементов");
checkedListBox.SetItemChecked(e.Index, false);
}
else if (typeOfMaxChecked == Types.DeleteFirst)
{
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("<22><><EFBFBD><EFBFBD><EFBFBD>");
list.Add("C#");
userControlCheckedList.SetCheckedListBoxValues(list);
userControlCheckedList.CheckedItem = list[3];
userControlCheckedList.MaxCheckedItemsCount = 3;
userControlCheckedList.ChangeTypeOfMaxChecked(Components.Types.DeleteFirst);
}
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,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new FormNoVisual());
Application.Run(new FormVisual());
}
}
}