список splitbutton
Добрый день! Как ограничить длину выпадающего списка splitbutton?
Может быть поможет - меню можно напавить "вверх" настройкой вроде
Код:
DropDownDirection = ToolStripDropDownDirection.AboveRight
Для внедрения в тулбар (ToolStrip) компонентов WinForms в .NET существует класс ToolStripControlHost, также на RSDN есть статья затрагивающая этот класс.
Всё просто. Всем компонентам ToolStripMenuItem данной кнопки назначаем один обработчик события MouseHover.
Тем элементам, что должны быть изначально видны, в режиме дизайнера оставляем свойство Visible равным True, у остальных устанавливаем это свойство False. Главное, чтобы видимые элементы шли подряд, без пропусков.
В данном примере количество видимых элементов равно четырём. Именно четыре элемента должны в режиме дизайнера иметь свойство Visible равным True. При желании, это количество можно изменить, при этом также нужно изменить число 3 в коде (5 видимых элементов - пишем число 4; 10 видимых элементов - пишем число 9).
Собственно, приведённый код при задержке мыши на первом или последнем видимом элементе открывает соответственно предыдущий или последующий элемент, и скрывает элемент на другом конце списка.
Код:
private void oneToolStripMenuItem_MouseHover(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
ToolStripItemCollection coll = item.Owner.Items;
int index = coll.IndexOf(item);
if (index == 0 || index == coll.Count - 1)
return;
if (!coll[index - 1].Visible)
{
coll[index - 1].Visible = true;
coll[index + 3].Visible = false;
return;
}
if (!coll[index + 1].Visible)
{
coll[index + 1].Visible = true;
coll[index - 3].Visible = false;
return;
}
}
{
ToolStripItem item = (ToolStripItem)sender;
ToolStripItemCollection coll = item.Owner.Items;
int index = coll.IndexOf(item);
if (index == 0 || index == coll.Count - 1)
return;
if (!coll[index - 1].Visible)
{
coll[index - 1].Visible = true;
coll[index + 3].Visible = false;
return;
}
if (!coll[index + 1].Visible)
{
coll[index + 1].Visible = true;
coll[index - 3].Visible = false;
return;
}
}
Думаю, объяснил понятно. Если не понятно - спрашивайте.
решении проблемы!
Файл Form1.cs
Код:
using System;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void upToolStripMenuItem_MouseHover(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
ToolStripItemCollection coll = item.Owner.Items;
int index = 1;
while (!coll[index].Visible)
index++;
if (index == 1)
{
coll[0].Text = "";
return;
}
coll[index - 1].Visible = true;
coll[index + 2].Visible = false;
coll[coll.Count - 1].Text = "Y"; // это вместо стрелочки вниз
item.Visible = false;
item.Visible = true;
}
private void downToolStripMenuItem_MouseHover(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
ToolStripItemCollection coll = item.Owner.Items;
int index = coll.Count - 2;
while (!coll[index].Visible)
index--;
if (index == coll.Count - 2)
{
coll[coll.Count - 1].Text = "";
return;
}
coll[index + 1].Visible = true;
coll[index - 2].Visible = false;
coll[0].Text = "^"; // это вместо стрелочки вверх
item.Visible = false;
item.Visible = true;
}
}
}
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void upToolStripMenuItem_MouseHover(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
ToolStripItemCollection coll = item.Owner.Items;
int index = 1;
while (!coll[index].Visible)
index++;
if (index == 1)
{
coll[0].Text = "";
return;
}
coll[index - 1].Visible = true;
coll[index + 2].Visible = false;
coll[coll.Count - 1].Text = "Y"; // это вместо стрелочки вниз
item.Visible = false;
item.Visible = true;
}
private void downToolStripMenuItem_MouseHover(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
ToolStripItemCollection coll = item.Owner.Items;
int index = coll.Count - 2;
while (!coll[index].Visible)
index--;
if (index == coll.Count - 2)
{
coll[coll.Count - 1].Text = "";
return;
}
coll[index + 1].Visible = true;
coll[index - 2].Visible = false;
coll[0].Text = "^"; // это вместо стрелочки вверх
item.Visible = false;
item.Visible = true;
}
}
}
файл Form1.Designer.cs
Код:
namespace WindowsApplication1
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.toolStrip = new System.Windows.Forms.ToolStrip();
this.toolStripSplitButton = new System.Windows.Forms.ToolStripSplitButton();
this.upToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.oneToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.twoToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.threeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.fourToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.fiveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.sixToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.sevenToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.downToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStrip.SuspendLayout();
this.SuspendLayout();
//
// toolStrip1
//
this.toolStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripSplitButton});
this.toolStrip.Location = new System.Drawing.Point(0, 0);
this.toolStrip.Name = "toolStrip1";
this.toolStrip.Size = new System.Drawing.Size(292, 25);
this.toolStrip.TabIndex = 0;
this.toolStrip.Text = "toolStrip1";
//
// toolStripSplitButton2
//
this.toolStripSplitButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.toolStripSplitButton.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.upToolStripMenuItem,
this.oneToolStripMenuItem,
this.twoToolStripMenuItem,
this.threeToolStripMenuItem,
this.fourToolStripMenuItem,
this.fiveToolStripMenuItem,
this.sixToolStripMenuItem,
this.sevenToolStripMenuItem,
this.downToolStripMenuItem});
this.toolStripSplitButton.Image = ((System.Drawing.Image)(resources.GetObject("toolStripSplitButton2.Image")));
this.toolStripSplitButton.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripSplitButton.Name = "toolStripSplitButton2";
this.toolStripSplitButton.Size = new System.Drawing.Size(32, 22);
this.toolStripSplitButton.Text = "toolStripSplitButton2";
//
// upToolStripMenuItem
//
this.upToolStripMenuItem.AutoSize = false;
this.upToolStripMenuItem.Name = "upToolStripMenuItem";
this.upToolStripMenuItem.Size = new System.Drawing.Size(152, 11);
this.upToolStripMenuItem.MouseHover += new System.EventHandler(this.upToolStripMenuItem_MouseHover);
//
// oneToolStripMenuItem1
//
this.oneToolStripMenuItem.Name = "oneToolStripMenuItem1";
this.oneToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.oneToolStripMenuItem.Text = "One";
//
// twoToolStripMenuItem1
//
this.twoToolStripMenuItem.Name = "twoToolStripMenuItem1";
this.twoToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.twoToolStripMenuItem.Text = "Two";
//
// threeToolStripMenuItem1
//
this.threeToolStripMenuItem.Name = "threeToolStripMenuItem1";
this.threeToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.threeToolStripMenuItem.Text = "Three";
//
// fourToolStripMenuItem1
//
this.fourToolStripMenuItem.Name = "fourToolStripMenuItem1";
this.fourToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.fourToolStripMenuItem.Text = "Four";
this.fourToolStripMenuItem.Visible = false;
//
// fiveToolStripMenuItem1
//
this.fiveToolStripMenuItem.Name = "fiveToolStripMenuItem1";
this.fiveToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.fiveToolStripMenuItem.Text = "Five";
this.fiveToolStripMenuItem.Visible = false;
//
// sixToolStripMenuItem1
//
this.sixToolStripMenuItem.Name = "sixToolStripMenuItem1";
this.sixToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.sixToolStripMenuItem.Text = "Six";
this.sixToolStripMenuItem.Visible = false;
//
// sevenToolStripMenuItem1
//
this.sevenToolStripMenuItem.Name = "sevenToolStripMenuItem1";
this.sevenToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.sevenToolStripMenuItem.Text = "Seven";
this.sevenToolStripMenuItem.Visible = false;
//
// downToolStripMenuItem
//
this.downToolStripMenuItem.AutoSize = false;
this.downToolStripMenuItem.Name = "downToolStripMenuItem";
this.downToolStripMenuItem.Size = new System.Drawing.Size(152, 11);
this.downToolStripMenuItem.Text = "Y";
this.downToolStripMenuItem.MouseHover += new System.EventHandler(this.downToolStripMenuItem_MouseHover);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 275);
this.Controls.Add(this.toolStrip);
this.Name = "Form1";
this.Text = "Form1";
this.toolStrip.ResumeLayout(false);
this.toolStrip.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ToolStrip toolStrip;
private System.Windows.Forms.ToolStripSplitButton toolStripSplitButton;
private System.Windows.Forms.ToolStripMenuItem oneToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem twoToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem threeToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem fourToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem fiveToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem sixToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem sevenToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem upToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem downToolStripMenuItem;
}
}
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.toolStrip = new System.Windows.Forms.ToolStrip();
this.toolStripSplitButton = new System.Windows.Forms.ToolStripSplitButton();
this.upToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.oneToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.twoToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.threeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.fourToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.fiveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.sixToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.sevenToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.downToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStrip.SuspendLayout();
this.SuspendLayout();
//
// toolStrip1
//
this.toolStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripSplitButton});
this.toolStrip.Location = new System.Drawing.Point(0, 0);
this.toolStrip.Name = "toolStrip1";
this.toolStrip.Size = new System.Drawing.Size(292, 25);
this.toolStrip.TabIndex = 0;
this.toolStrip.Text = "toolStrip1";
//
// toolStripSplitButton2
//
this.toolStripSplitButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.toolStripSplitButton.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.upToolStripMenuItem,
this.oneToolStripMenuItem,
this.twoToolStripMenuItem,
this.threeToolStripMenuItem,
this.fourToolStripMenuItem,
this.fiveToolStripMenuItem,
this.sixToolStripMenuItem,
this.sevenToolStripMenuItem,
this.downToolStripMenuItem});
this.toolStripSplitButton.Image = ((System.Drawing.Image)(resources.GetObject("toolStripSplitButton2.Image")));
this.toolStripSplitButton.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripSplitButton.Name = "toolStripSplitButton2";
this.toolStripSplitButton.Size = new System.Drawing.Size(32, 22);
this.toolStripSplitButton.Text = "toolStripSplitButton2";
//
// upToolStripMenuItem
//
this.upToolStripMenuItem.AutoSize = false;
this.upToolStripMenuItem.Name = "upToolStripMenuItem";
this.upToolStripMenuItem.Size = new System.Drawing.Size(152, 11);
this.upToolStripMenuItem.MouseHover += new System.EventHandler(this.upToolStripMenuItem_MouseHover);
//
// oneToolStripMenuItem1
//
this.oneToolStripMenuItem.Name = "oneToolStripMenuItem1";
this.oneToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.oneToolStripMenuItem.Text = "One";
//
// twoToolStripMenuItem1
//
this.twoToolStripMenuItem.Name = "twoToolStripMenuItem1";
this.twoToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.twoToolStripMenuItem.Text = "Two";
//
// threeToolStripMenuItem1
//
this.threeToolStripMenuItem.Name = "threeToolStripMenuItem1";
this.threeToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.threeToolStripMenuItem.Text = "Three";
//
// fourToolStripMenuItem1
//
this.fourToolStripMenuItem.Name = "fourToolStripMenuItem1";
this.fourToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.fourToolStripMenuItem.Text = "Four";
this.fourToolStripMenuItem.Visible = false;
//
// fiveToolStripMenuItem1
//
this.fiveToolStripMenuItem.Name = "fiveToolStripMenuItem1";
this.fiveToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.fiveToolStripMenuItem.Text = "Five";
this.fiveToolStripMenuItem.Visible = false;
//
// sixToolStripMenuItem1
//
this.sixToolStripMenuItem.Name = "sixToolStripMenuItem1";
this.sixToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.sixToolStripMenuItem.Text = "Six";
this.sixToolStripMenuItem.Visible = false;
//
// sevenToolStripMenuItem1
//
this.sevenToolStripMenuItem.Name = "sevenToolStripMenuItem1";
this.sevenToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.sevenToolStripMenuItem.Text = "Seven";
this.sevenToolStripMenuItem.Visible = false;
//
// downToolStripMenuItem
//
this.downToolStripMenuItem.AutoSize = false;
this.downToolStripMenuItem.Name = "downToolStripMenuItem";
this.downToolStripMenuItem.Size = new System.Drawing.Size(152, 11);
this.downToolStripMenuItem.Text = "Y";
this.downToolStripMenuItem.MouseHover += new System.EventHandler(this.downToolStripMenuItem_MouseHover);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 275);
this.Controls.Add(this.toolStrip);
this.Name = "Form1";
this.Text = "Form1";
this.toolStrip.ResumeLayout(false);
this.toolStrip.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ToolStrip toolStrip;
private System.Windows.Forms.ToolStripSplitButton toolStripSplitButton;
private System.Windows.Forms.ToolStripMenuItem oneToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem twoToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem threeToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem fourToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem fiveToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem sixToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem sevenToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem upToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem downToolStripMenuItem;
}
}