Este es mi primer posteo en los foros de MSDN asique primeramente saludo a todos.
Tengo un problema que resolver. Primeramente les explico lo que quiero hacer:
En una base de datos tengo los usuarios, cada usuario tiene un perfil, cada perfil puede tener muchos menus y cada menu puede tener muchos botones. Todo esto esta en una base de datos. La idea de tenerlo asi es para poder cargar dinamicamente los menus de los usuarios.
Al iniciar el formulario principal, cargo el perfil del usuario, los menus del usuario y los botones del usuario. El problema principal es que quiero agregar el boton.Click handler para cada boton. Todos los botones se cargan en una iteracion, pero no se como reconocer que boton fue apretado, es decir, agrego el boton.click += new EventHandle(metodo) pero como todos los botones responden a esto, todos los botones van al mismo metodo al realizar el evento Click. Espero que se haya entendido el problema.
El codigo es el siguiente:
/// <summary>
/// Load the menus of a Core.Systems.IMenu interface
/// </summary>
/// <param name="pIMenu">Core.Systems.IMenu to load the menus</param>
/// <param name="pItem">ToolStipItem that will contains the new menus</param>
private void loadMenus(IMenu pIMenu, ToolStripItem pItem)
{
try
{
//Iteration foreach obj in the collection
foreach (IMenu m in pIMenu.menus)
{
//New instance of the ToolSripMenuItem class
ToolStripMenuItem tool = new ToolStripMenuItem();
//Set the text
tool.Text = m.text;
tool.ToolTipText = ((Sandbird_Client.Menu)m).tipText;
//Set the appearance
setAppearances(tool);
//Set the image of the tool
tool.Image = getItemImage(m);
//Check if the menu has menus inside
if (m.menus != null && m.menus.Count > 0)
{
//Use recursivity to load the menus thar a menu has
this.loadMenus(m, tool);
}
//Check if the menu has modules
if (m.modules != null)
{
//Iteration foreach IModule ni the collection
foreach (IModule pIModule in m.modules)
{
//New ToolStripButton
ToolStripMenuItem button = new ToolStripMenuItem();
//Set the text and the appearance of the button
button.Text = pIModule.text;
button.ToolTipText = ((Module)pIModule).tipText;
button.Image = getItemImage(pIModule);
setAppearances(button);
//Add the button to the menu
tool.DropDownItems.Add(button);
button.Click += new EventHandler(button_Click);
}
}
//Check if the ToolStipItem is a ToolStripDropDownButton
if (pItem is ToolStripDropDownButton)
{
//Add the new tool to the menu
((ToolStripDropDownButton)pItem).DropDownItems.Add(tool);
}
//Check if the ToolStrip is a ToolStipMenuItem
else if (pItem is ToolStripMenuItem)
{
//Add the new tool to the menu
((ToolStripMenuItem)pItem).DropDownItems.Add(tool);
}
}
}
catch (Exception ex)
{
//Throw the exception
throw ex;
}
}
void button_Click(object sender, EventArgs e)
{
}
Todos los botones generados dinamicamente al hacer click llaman al metodo button_Click. No se como se podria hacer para diferenciar dentro del metodo click a cada boton.Desde ya muchas gracias.
Saludos