elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Como proteger una cartera - billetera de Bitcoin


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  habilitar menustrip
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: habilitar menustrip  (Leído 3,704 veces)
d91

Desconectado Desconectado

Mensajes: 165


Ver Perfil
habilitar menustrip
« en: 16 Octubre 2015, 00:13 am »

hola a todos estoy haciendo una pequeña aplicación Windows forms, en un panel cargo los usercontrol de que tienen formularios, grid, etc. lo único es que quiero forzar al usuario a que cierre el usercontrol para poder abrir otro, pensé en deshabilitar el menú al abrir el usercontrol, pero no se en que evento habilitarlo para cuando se cierre el usercontrol
Código
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace SAF
  12. {
  13.    public partial class Form1 : Form
  14.    {
  15.        public Form1()
  16.        {
  17.            InitializeComponent();
  18.  
  19.        }
  20.  
  21.  
  22.  
  23.        private void Form1_Load(object sender, EventArgs e)
  24.        {
  25.  
  26.        }
  27.  
  28.        private void nuevoClienteToolStripMenuItem_Click_1(object sender, EventArgs e)
  29.        {
  30.  
  31.            if (!panel3.Controls.Contains(InsercionClientes.Instance))
  32.            {
  33.  
  34.                panel3.Controls.Add(InsercionClientes.Instance);
  35.                InsercionClientes.Instance.Dock = DockStyle.Fill;
  36.                InsercionClientes.Instance.BringToFront();
  37.  
  38.            }//if de estado de UC
  39.  
  40.            else {
  41.  
  42.                InsercionClientes.Instance.BringToFront();
  43.            }//else
  44.            menuStrip1.Enabled = false;
  45.  
  46.        }
  47.    }
  48. }
  49.  
  50.  
  51.  


En línea

Eleкtro
Ex-Staff
*
Conectado Conectado

Mensajes: 9.810



Ver Perfil
Re: habilitar menustrip
« Respuesta #1 en: 16 Octubre 2015, 06:17 am »

en un panel cargo los usercontrol

No es necesario que heredes la trivialidad de un UserControl, puedes heredar un Form, solamente tienes que tener en cuenta desactivar la propiedad Form.TopLevel para hacer el MDI.

Form.TopLevel Property - MSDN



quiero forzar al usuario a que cierre el usercontrol para poder abrir otro
pensé en deshabilitar el menú al abrir el usercontrol, pero no se en que evento habilitarlo para cuando se cierre el usercontrol

Te daré varias opciones...



1. - Si quieres desactivar/activar el menu entero, puedes suscribirte a los eventos Panel.ControlAdded y Panel.ControlRemoved

Ejemplo en VB.Net:
Código
  1. Private Sub Panel3_ControlAdded(ByVal sender As Object, ByVal e As ControlEventArgs) _
  2. Handles Panel3.ControlAdded,
  3.        Panel3.ControlRemoved
  4.  
  5.    Me.nuevoClienteToolStripMenuItem.Enabled =
  6.        Not DirectCast(sender, Panel).Controls.OfType(Of CustomMDIForm).Any
  7.  
  8. End Sub

Ejemplo en C#:
Código
  1. private void Panel3_ControlAdded_And_ControlRemoved(object sender, ControlEventArgs e) {
  2. this.nuevoClienteToolStripMenuItem.Enabled = !((Panel)sender).Controls.OfType<CustomMDIForm>.Any();
  3. }



2. - Si quieres desactivar/activar un item en particular del menu, puedes suscribirte al evento ToolStripMenuItem.DropDownOpening y acceder al item en cuestión en la colección ToolStripMenuItem.DropDownItems.

Ejemplo en VB.Net:
Código
  1. Private Sub NuevoClienteToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) _
  2. Handles nuevoClienteToolStripMenuItem.DropDownOpening
  3.  
  4.    DirectCast(sender, ToolStripMenuItem).DropDownItems(index:=0).Enabled =
  5.        Not Me.Panel3.Controls.OfType(Of CustomMDIForm).Any
  6.  
  7. End Sub

Ejemplo en C#:
Código
  1. private void NuevoClienteToolStripMenuItem_Click(object sender, EventArgs e) {
  2. ((ToolStripMenuItem)sender).DropDownItems(index: 0).Enabled = !this.Panel3.Controls.OfType<CustomMDIForm>.Any();
  3. }



3. - También puedes administrar la visibilidad del menú desde tu Form heredado o desde un Usercontrol, de la misma manera.
 
En tu herencia, le pasas la instancia del ToolStipMenuItem al Constructor de la class, y cambias su visibilidad suplantando los métodos OnLoad y OnDispose.

Ejemplo en VB.Net:
Código
  1. Public NotInheritable Class CustomMDIForm : Inherits Form
  2.  
  3.    Public ReadOnly MenuItem As ToolStripMenuItem
  4.  
  5.    Public Sub New(ByVal parent As Control, ByVal menuItem As ToolStripMenuItem)
  6.  
  7.        With Me
  8.            .TopLevel = False
  9.            .Parent = parent
  10.            .StartPosition = FormStartPosition.CenterParent
  11.            .FormBorderStyle = FormBorderStyle.Sizable
  12.            .Dock = DockStyle.Fill
  13.        End With
  14.  
  15.        Me.MenuItem = menuItem
  16.  
  17.    End Sub
  18.  
  19.    Private Sub New()
  20.    End Sub
  21.  
  22.    Protected Overrides Sub OnLoad(ByVal e As EventArgs)
  23.        If (Me.MenuItem IsNot Nothing) Then
  24.            Me.MenuItem.Enabled = False
  25.        End If
  26.        MyBase.OnLoad(e)
  27.    End Sub
  28.  
  29.    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
  30.        If (Me.MenuItem IsNot Nothing) Then
  31.            Me.MenuItem.Enabled = True
  32.        End If
  33.        MyBase.Dispose(Disposing)
  34.    End Sub
  35.  
  36. End Class
+
Código
  1. Public NotInheritable Class Form1 : Inherits Form
  2.  
  3.    Private cf As CustomMDIForm
  4.  
  5.    Public Sub New()
  6.        Me.InitializeComponent()
  7.        Me.cf = New CustomMDIForm(parent:=Me.Panel3, menuItem:=Me.nuevoClienteToolStripMenuItem)
  8.    End Sub
  9.  
  10.    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
  11.    Handles MyBase.Load
  12.  
  13.        Me.cf.Show()
  14.  
  15.    End Sub
  16.  
  17. End Class

Traducción online a C#:
Código
  1. using Microsoft.VisualBasic;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Diagnostics;
  7.  
  8. public sealed class CustomMDIForm : Form {
  9.  
  10. public readonly ToolStripMenuItem MenuItem;
  11.  
  12. public CustomMDIForm(Control parent, ToolStripMenuItem menuItem) {
  13. this.TopLevel = false;
  14. this.Parent = parent;
  15. this.StartPosition = FormStartPosition.CenterParent;
  16. this.FormBorderStyle = FormBorderStyle.Sizable;
  17. this.Dock = DockStyle.Fill;
  18.  
  19. this.MenuItem = menuItem;
  20. }
  21.  
  22. private CustomMDIForm() {
  23. }
  24.  
  25. protected override void OnLoad(EventArgs e) {
  26. if ((this.MenuItem != null)) {
  27. this.MenuItem.Enabled = false;
  28. }
  29. base.OnLoad(e);
  30. }
  31.  
  32. protected override void Dispose(bool disposing) {
  33. if ((this.MenuItem != null)) {
  34. this.MenuItem.Enabled = true;
  35. }
  36. base.Dispose(disposing);
  37. }
  38.  
  39. }
  40.  
  41. //=======================================================
  42. //Service provided by Telerik (www.telerik.com)
  43. //=======================================================
+
Código
  1. using Microsoft.VisualBasic;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Diagnostics;
  7.  
  8. public sealed class Form1 : Form {
  9.  
  10. private CustomMDIForm cf;
  11.  
  12. public Form1() {
  13. Load += Form1_Load;
  14. this.InitializeComponent();
  15. this.cf = new CustomMDIForm(parent: this.Panel3, menuItem: this.nuevoClienteToolStripMenuItem);
  16. }
  17.  
  18. private void Form1_Load(object sender, EventArgs e) {
  19. this.cf.Show();
  20. }
  21. }
  22.  
  23. //=======================================================
  24. //Service provided by Telerik (www.telerik.com)
  25. //=======================================================


Saludos!


« Última modificación: 16 Octubre 2015, 06:21 am por Eleкtro » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines