// *********************************************************************** // Assembly : HZH_Controls // Created : 08-08-2019 // // *********************************************************************** // // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com // // // Blog: https://www.cnblogs.com/bfyx // GitHub:https://github.com/kwwwvagaa/NetWinformControl // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git // // If you use this code, please keep this note. // *********************************************************************** using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using HZH_Controls; namespace KS_Controls.Controls { /// /// Class UCHorizontalList. /// Implements the /// /// public partial class KSHorizontalList : UserControl { /// /// Gets or sets the selected item. /// /// The selected item. public KSHorizontalListItem SelectedItem { get; set; } /// /// Occurs when [selected item event]. /// public event EventHandler SelectedItemEvent; /// /// The m start item index /// private int m_startItemIndex = 0; /// /// The is automatic select first /// private bool isAutoSelectFirst = true; /// /// Gets or sets a value indicating whether this instance is automatic select first. /// /// true if this instance is automatic select first; otherwise, false. public bool IsAutoSelectFirst { get { return isAutoSelectFirst; } set { isAutoSelectFirst = value; } } /// /// The data source /// private List> dataSource = null; /// /// Gets or sets the data source. /// /// The data source. public List> DataSource { get { return dataSource; } set { dataSource = value; ReloadSource(); } } private Color selectedColor = Color.FromArgb(255, 77, 59); [Description("选中颜色"), Category("自定义")] public Color SelectedColor { get { return selectedColor; } set { selectedColor = value; } } /// /// Initializes a new instance of the class. /// public KSHorizontalList() { InitializeComponent(); } /// /// Reloads the source. /// public void ReloadSource() { try { ControlHelper.FreezeControl(this, true); this.panList.SuspendLayout(); this.panList.Controls.Clear(); this.panList.Width = this.panMain.Width; if (DataSource != null) { foreach (var item in DataSource) { KSHorizontalListItem uc = new KSHorizontalListItem(); uc.SelectedColor = selectedColor; uc.DataSource = item; uc.SelectedItem += uc_SelectItem; this.panList.Controls.Add(uc); } } this.panList.ResumeLayout(true); if (this.panList.Controls.Count > 0) this.panList.Width = panMain.Width + this.panList.Controls[0].Location.X * -1; this.panList.Location = new Point(0, 0); m_startItemIndex = 0; if (this.panList.Width > panMain.Width) panRight.Visible = true; else panRight.Visible = false; panLeft.Visible = false; panList.SendToBack(); panRight.SendToBack(); if (isAutoSelectFirst && DataSource != null && DataSource.Count > 0) { SelectItem((KSHorizontalListItem)this.panList.Controls[0]); } } finally { ControlHelper.FreezeControl(this, false); } } /// /// Handles the SelectItem event of the uc control. /// /// The source of the event. /// The instance containing the event data. void uc_SelectItem(object sender, EventArgs e) { SelectItem(sender as KSHorizontalListItem); } /// /// Selects the item. /// /// The item. private void SelectItem(KSHorizontalListItem item) { if (SelectedItem != null && !SelectedItem.IsDisposed) SelectedItem.SetSelect(false); SelectedItem = item; SelectedItem.SetSelect(true); if (SelectedItemEvent != null) SelectedItemEvent(item, null); } /// /// Handles the MouseDown event of the panLeft control. /// /// The source of the event. /// The instance containing the event data. private void panLeft_MouseDown(object sender, MouseEventArgs e) { if (this.panList.Location.X >= 0) { this.panList.Location = new Point(0, 0); return; } for (int i = m_startItemIndex; i >= 0; i--) { if (this.panList.Controls[i].Location.X < this.panList.Controls[m_startItemIndex].Location.X - panMain.Width) { m_startItemIndex = i + 1; break; ; } if (i == 0) { m_startItemIndex = 0; } } ResetListLocation(); panRight.Visible = true; if (this.panList.Location.X >= 0) { panLeft.Visible = false; } else { panLeft.Visible = true; } panList.SendToBack(); panRight.SendToBack(); } /// /// Handles the MouseDown event of the panRight control. /// /// The source of the event. /// The instance containing the event data. private void panRight_MouseDown(object sender, MouseEventArgs e) { if (this.panList.Location.X + this.panList.Width <= this.panMain.Width) return; if (this.panList.Controls.Count <= 0) return; for (int i = m_startItemIndex; i < this.panList.Controls.Count; i++) { if (this.panList.Location.X + this.panList.Controls[i].Location.X + this.panList.Controls[i].Width > panMain.Width) { m_startItemIndex = i; break; } } ResetListLocation(); panLeft.Visible = true; if (panList.Width + panList.Location.X <= panMain.Width) panRight.Visible = false; else panRight.Visible = true; panList.SendToBack(); panRight.SendToBack(); } /// /// Resets the list location. /// private void ResetListLocation() { if (this.panList.Controls.Count > 0) { this.panList.Location = new Point(this.panList.Controls[m_startItemIndex].Location.X * -1, 0); } } /// /// Sets the select. /// /// The string key. public void SetSelect(string strKey) { foreach (KSHorizontalListItem item in this.panList.Controls) { if (item.DataSource.Key == strKey) { SelectItem(item); return; } } } } }