Skip to content

Instantly share code, notes, and snippets.

@Fhernd
Created March 3, 2018 16:04
Show Gist options
  • Save Fhernd/ae9683079956524a1fee468dbcc6079e to your computer and use it in GitHub Desktop.
Save Fhernd/ae9683079956524a1fee468dbcc6079e to your computer and use it in GitHub Desktop.
Menú contextual creado a partir de los ítems de un menú estándar. C#.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace R712ClonarMenu
{
public partial class Principal : Form
{
private MainMenu mnuPrincipal;
private MenuItem mnuArchivo;
private MenuItem mnuAbrir;
private MenuItem mnuGuardar;
private MenuItem mnuSalir;
private MenuItem mnuEditar;
public Principal()
{
InitializeComponent();
SuspendLayout();
mnuPrincipal = new MainMenu();
mnuArchivo = new MenuItem();
mnuAbrir = new MenuItem();
mnuGuardar = new MenuItem();
mnuSalir = new MenuItem();
mnuEditar = new MenuItem("Editar");
mnuEditar.Index = 1;
mnuPrincipal.MenuItems.AddRange(new MenuItem[] { mnuArchivo, mnuEditar });
mnuArchivo.MenuItems.AddRange(new MenuItem[] { mnuAbrir, mnuGuardar, mnuSalir });
mnuArchivo.Index = 0;
mnuArchivo.Text = "Archivo";
mnuAbrir.Index = 0;
mnuAbrir.Text = "Abrir";
mnuAbrir.Click += new EventHandler(mnuAbrir_Click);
mnuGuardar.Index = 1;
mnuGuardar.Text = "Guardar";
mnuGuardar.Click += new EventHandler(mnuGuardar_Click);
mnuSalir.Index = 2;
mnuSalir.Text = "Salir";
mnuSalir.Click += new EventHandler(mnuSalir_Click);
Menu = mnuPrincipal;
ResumeLayout(false);
PerformLayout();
}
protected override void OnLoad(EventArgs evt)
{
base.OnLoad(evt);
ContextMenu cmuMenuContextual = new ContextMenu();
foreach (MenuItem mnuItem in mnuArchivo.MenuItems)
{
if (mnuItem != null)
{
cmuMenuContextual.MenuItems.Add(mnuItem.CloneMenu());
}
}
txtContenido.ContextMenu = cmuMenuContextual;
}
private void mnuAbrir_Click(object sender, EventArgs evtArgs)
{
MessageBox.Show("Se ha abierto el contenido", "Mensaje", MessageBoxButtons.OK);
}
private void mnuGuardar_Click(object sender, EventArgs eventArgs)
{
MessageBox.Show("Se ha guardado el contenido", "Mensaje", MessageBoxButtons.OK);
}
private void mnuSalir_Click(object sender, EventArgs eventArgs)
{
MessageBox.Show("Se ha salido del documento", "Mensaje", MessageBoxButtons.OK);
}
private void txtContenido_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
txtContenido.ContextMenu.Show(txtContenido, new Point(e.X, e.Y));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment