Este artigo foi traduzido por máquina. Coloque o ponteiro do mouse sobre as frases do artigo para ver o texto original. Mais informações. |
Tradução
Original
|
Classe ToolStripDropDown
Representa um controle que permite ao usuário selecionar um único item em uma lista que é exibida quando o usuário clica em um ToolStripDropDownButton. Embora ToolStripDropDownMenu e ToolStripDropDown Substituir e adicionar funcionalidade para o Menu controle de versões anteriores, Menu é mantida para compatibilidade com versões anteriores e para uso futuro, se você escolher.
Assembly: System.Windows.Forms (em System.Windows.Forms.dll)
O exemplo de código a seguir usa o ToolStripDropDown e ToolStripDropDownButton classes para tornar um seletor de cores três botões que altera a cor de primeiro plano do formulário.
// Declare the drop-down button and the items it will contain.internal ToolStripDropDownButton dropDownButton1; internal ToolStripDropDown dropDown; internal ToolStripButton buttonRed; internal ToolStripButton buttonBlue; internal ToolStripButton buttonYellow; privatevoid InitializeDropDownButton() { dropDownButton1 = new ToolStripDropDownButton(); dropDown = new ToolStripDropDown(); dropDownButton1.Text = "A"; // Set the drop-down on the ToolStripDropDownButton. dropDownButton1.DropDown = dropDown; // Set the drop-down direction. dropDownButton1.DropDownDirection = ToolStripDropDownDirection.Left; // Do not show a drop-down arrow. dropDownButton1.ShowDropDownArrow = false; // Declare three buttons, set their foreground color and text, // and add the buttons to the drop-down. buttonRed = new ToolStripButton(); buttonRed.ForeColor = Color.Red; buttonRed.Text = "A"; buttonBlue = new ToolStripButton(); buttonBlue.ForeColor = Color.Blue; buttonBlue.Text = "A"; buttonYellow = new ToolStripButton(); buttonYellow.ForeColor = Color.Yellow; buttonYellow.Text = "A"; buttonBlue.Click += new EventHandler(colorButtonsClick); buttonRed.Click += new EventHandler(colorButtonsClick); buttonYellow.Click += new EventHandler(colorButtonsClick); dropDown.Items.AddRange(new ToolStripItem[] { buttonRed, buttonBlue, buttonYellow }); toolStrip1.Items.Add(dropDownButton1); } // Handle the buttons' click event by setting the foreground color of the// form to the foreground color of the button that is clicked.privatevoid colorButtonsClick(object sender, EventArgs e) { ToolStripButton senderButton = (ToolStripButton)sender; this.ForeColor = senderButton.ForeColor; }
O exemplo de código a seguir usa ToolStripControlHost para mostrar um ToolStripDropDown sistema autônomo um TreeView.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Security.Permissions; publicclass Form1 : Form { public Form1() { MyTreeViewCombo treeCombo = new MyTreeViewCombo(); treeCombo.TreeView.Nodes.Add("one"); treeCombo.TreeView.Nodes.Add("two"); treeCombo.TreeView.Nodes.Add("three"); this.Controls.Add(treeCombo); } [STAThread] staticvoid Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } [SecurityPermissionAttribute( SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] publicclass MyTreeViewCombo : ComboBox { ToolStripControlHost treeViewHost; ToolStripDropDown dropDown; public MyTreeViewCombo() { TreeView treeView = new TreeView(); treeView.BorderStyle = BorderStyle.None; treeViewHost = new ToolStripControlHost(treeView); dropDown = new ToolStripDropDown(); dropDown.Items.Add(treeViewHost); } public TreeView TreeView { get { return treeViewHost.Control as TreeView; } } privatevoid ShowDropDown() { if (dropDown != null) { treeViewHost.Width = DropDownWidth; treeViewHost.Height = DropDownHeight; dropDown.Show(this, 0, this.Height); } } privateconstint WM_USER = 0x0400, WM_REFLECT = WM_USER + 0x1C00, WM_COMMAND = 0x0111, CBN_DROPDOWN = 7; publicstaticint HIWORD(int n) { return (n >> 16) & 0xffff; } protectedoverridevoid WndProc(ref Message m) { if (m.Msg == (WM_REFLECT + WM_COMMAND)) { if (HIWORD((int)m.WParam) == CBN_DROPDOWN) { ShowDropDown(); return; } } base.WndProc(ref m); } protectedoverridevoid Dispose(bool disposing) { if (disposing) { if (dropDown != null) { dropDown.Dispose(); dropDown = null; } } base.Dispose(disposing); } } }
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
o.NET Framework e.NET Compact Framework não oferecem suporte a todas as versões de cada plataforma. Para obter uma lista de versões suportadas, consulte Requisitos de sistema do .NET framework.