using RyzStudio.Drawing; using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using UIResource = RandomFileRunner.UIResource; namespace RyzStudio.Windows.ThemedForms { public partial class TListBox : RyzStudio.Windows.ThemedForms.TUserControl { protected readonly Padding textboxPadding = new Padding(6, 2, 4, 2); public TListBox() : base() { InitializeComponent(); this.Font = new Font(this.Font, FontStyle.Regular); this.Margin = new Padding(10, 4, 10, 4); listBox1.Font = this.Font; listBox1.BorderStyle = BorderStyle.None; } //protected override void OnResize(EventArgs e) //{ // base.OnResize(e); // //int b = (styleActive.BorderWidth + 1) + styleActive.BorderPadding; // //this.Height = comboBox1.Height + (b + textboxPadding.Top) + ((b - 1) + textboxPadding.Bottom); //} protected override void OnGotFocus(EventArgs e) { base.OnGotFocus(e); listBox1.Focus(); } [Browsable(true), EditorBrowsable(EditorBrowsableState.Advanced)] [Category("Appearance")] public System.Windows.Forms.ListBox ListBox { get => listBox1; set => listBox1 = value; } [Browsable(true), EditorBrowsable(EditorBrowsableState.Advanced)] [Category("Appearance")] public TButton SubmitButton { get; set; } = null; [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)] public new Padding Margin { get => base.Margin; set => base.Margin = value; } [Browsable(false)] public event EventHandler OnAdd; [Browsable(false)] public event EventHandler OnEdit; protected override void updateBackground(Graphics g, ThemeStyle style) { int b = (styleActive.BorderWidth + 1) + styleActive.BorderPadding; this.Padding = new Padding((b + textboxPadding.Left), (b + textboxPadding.Top), ((b - 1) + textboxPadding.Right), ((b - 1) + textboxPadding.Bottom)); Rectangoid area = new Rectangoid(this.ClientRectangle, style.BorderRadius, style.BorderWidth); g.FillPath(new SolidBrush(style.BackColour), area.ToGraphicsPath()); g.DrawPath(new Pen(new SolidBrush(style.BorderColour), style.BorderWidth), area.ToGraphicsPath()); } /// /// Add /// /// /// private void imageBox1_MouseClick(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) { return; } this.OnAdd?.Invoke(sender, e); } /// /// Edit /// /// /// private void tImageBox1_MouseClick(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) { return; } editToolStripMenuItem_Click(sender, e); } /// /// Menu /// /// /// private void imageBox5_MouseClick(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) { return; } contextMenuStrip1.Show(Cursor.Position); } /// /// Move up /// /// /// private void imageBox3_MouseClick(object sender, MouseEventArgs e) { if (e != null) { if (e.Button != MouseButtons.Left) { return; } } moveUpToolStripMenuItem_Click(sender, e); } /// /// Move down /// /// /// private void imageBox4_MouseClick(object sender, MouseEventArgs e) { if (e != null) { if (e.Button != MouseButtons.Left) { return; } } moveDownToolStripMenuItem_Click(sender, e); } private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e) { editToolStripMenuItem_Click(sender, e); } private void listBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { switch (e.KeyCode) { case Keys.Enter: if (this.SubmitButton != null) { this.SubmitButton.PerformClick(); } break; case Keys.Escape: close(); break; case Keys.Up: if (e.Alt) { imageBox3_MouseClick(sender, null); } break; case Keys.Down: if (e.Alt) { imageBox4_MouseClick(sender, null); } break; default: break; } } #region context menu /// /// Add /// /// /// private void addItemToolStripMenuItem_Click(object sender, EventArgs e) { this.OnAdd?.Invoke(sender, e); } /// /// Copy /// /// /// private void toolStripMenuItem2_Click(object sender, EventArgs e) { if (listBox1.SelectedIndex <= 0) { return; } if (listBox1.SelectedItem == null) { return; } object item = listBox1.SelectedItem; listBox1.Items.Add(item); listBox1.SelectedIndex = (listBox1.Items.Count- 1); } /// /// Edit /// /// /// private void editToolStripMenuItem_Click(object sender, EventArgs e) { if (listBox1.SelectedIndex < 0) { return; } if (listBox1.SelectedItem == null) { return; } this.OnEdit?.Invoke(sender, e); } /// /// Remove /// /// /// private void removeToolStripMenuItem_Click(object sender, EventArgs e) { if (listBox1.SelectedIndex < 0) { return; } int pos = listBox1.SelectedIndex; listBox1.Items.RemoveAt(pos); if (pos > (listBox1.Items.Count - 1)) { pos = (listBox1.Items.Count - 1); } listBox1.SelectedIndex = pos; } /// /// Remove all /// /// /// private void toolStripMenuItem1_Click(object sender, EventArgs e) { listBox1.Items.Clear(); } /// /// Move to top /// /// /// private void moveToTopToolStripMenuItem_Click(object sender, EventArgs e) { if (listBox1.SelectedIndex <= 0) { return; } if (listBox1.SelectedItem == null) { return; } object item = listBox1.SelectedItem; int pos = listBox1.SelectedIndex; listBox1.Items.RemoveAt(pos); listBox1.Items.Insert(0, item); listBox1.SelectedIndex = 0; } /// /// Move up /// /// /// private void moveUpToolStripMenuItem_Click(object sender, EventArgs e) { if (listBox1.SelectedIndex <= 0) { return; } if (listBox1.SelectedItem == null) { return; } object item = listBox1.SelectedItem; int pos = listBox1.SelectedIndex; listBox1.Items.RemoveAt(pos); listBox1.Items.Insert((pos - 1), item); listBox1.SelectedIndex = (pos - 1); } /// /// Move down /// /// /// private void moveDownToolStripMenuItem_Click(object sender, EventArgs e) { if (listBox1.SelectedIndex >= (listBox1.Items.Count - 1)) { return; } if (listBox1.SelectedItem == null) { return; } object item = listBox1.SelectedItem; int pos = listBox1.SelectedIndex; listBox1.Items.RemoveAt(pos); listBox1.Items.Insert((pos + 1), item); listBox1.SelectedIndex = (pos + 1); } /// /// Move to bottom /// /// /// private void moveToBottomToolStripMenuItem_Click(object sender, EventArgs e) { int n = (listBox1.Items.Count - 1); if (listBox1.SelectedIndex >= (listBox1.Items.Count - 1)) { return; } if (listBox1.SelectedItem == null) { return; } object item = listBox1.SelectedItem; int pos = listBox1.SelectedIndex; listBox1.Items.RemoveAt(pos); listBox1.Items.Insert(n, item); listBox1.SelectedIndex = n; } #endregion } }