Click here to Skip to main content
15,909,437 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So, I have a form1 with some buttons like button1,button2...etc. and also have an UserControl1 with panel1 and panel2. Both of these panels do contain their own buttons and textfields. I am able to add this UserControl1 to the form1 panel whenever I click button1 on the form1 and can show these UserControl1 panels dynamically using the panel visible property set to true/false by clicking on a button on the UserControl1

Now I want this form1 button2 enable property to be set into true/false based on UserControl1 panel visible property.

Suppose here I want to enable the form1 button2 when the UserControl1 Panel2 is visible and disable when it is not visible.

What I have tried:

I am currently stuck with this problem and can't figure it out how to do it!

I am relatively new to C# so any help will be appreciated with much respect Thank you
Posted
Updated 7-Jan-18 1:06am
v2
Comments
Maciej Los 7-Jan-18 6:46am    
If you're able to set UserControl1 visible=true on Button1_Click event, then you're able to set Button2.Enabled=false in the same event.

1 solution

OK. That's not really difficult: all you need to do is create an event in your UserControl, which is handled by your Form. Sounds complicated? It isn't not really.
In your user control, add this code:
C#
/// <summary>
/// Event to indicate VisibiltyChanged
/// </summary>
public event EventHandler VisibilityChanged;
/// <summary>
/// Called to signal to subscribers that VisibiltyChanged
/// </summary>
/// <param name="e"></param>
protected virtual void OnVisibilityChanged(EventArgs e)
    {
    EventHandler eh = VisibilityChanged;
    if (eh != null)
        {
        eh(this, e);
        }
    }

Now, when you have changed the visibility of the panel inside the UserControl, signal the event:
C#
OnVisibilityChanged(new EventArgs());
Add a property to your UserControl, which lets the outside world know what the visibility currently is:
C#
public bool IsVisible { get { return myPanel.Visible; }}
And handle the event in your form.
When the handler is called, use the information:
C#
void myUserControl_VisibilityChanged(object sender, EventArgs e)
    {
    myUserControl mc = sender as MyUserControl;
    if (mc != null)
        {
        MyButton.Enabled = mc.IsVisible;
        }
    }
 
Share this answer
 
Comments
Maciej Los 7-Jan-18 7:44am    
Well explained!
Adnan Maruf 7-Jan-18 10:23am    
@OriginalGriff Sir thanks for your kind instruction. I don't know why but it is not working for me. I might messed up somewhere in between your codes and my codes. I think I did exactly how you said, but I might be wrong as I am extremely new to C#. Here's a brief explanation what I did->>

On the UserControl1
I exactly copy-paste your code that you provide for creating the event.
Then I add
public bool IsVisible { get { return panel2.Visible; } }


this piece of code just below the created event on the UserContrlo1. more specific

 public event EventHandler VisibilityChanged;
        protected virtual void OnVisibilityChanged(EventArgs e)
        {
            EventHandler eh = VisibilityChanged;
            if (eh != null)
            {
                eh(this, e);
            }
        }

        public bool IsVisible { get { return panel2.Visible; } }


Then as I am changing the UserControl panel visibility within the button it containing like when panel1 is showing then the click_event of button1 that is placed on the panel1 is changing the visibility property of panel2 into true and this panel2 also contain a button which is button2 and it's click_event changing the pane2 visibility property to false

[side.note: please don't get confuse these buttons with [form1{button1,button2}] and just to clear the whole thing again, I am trying to change the form1,button2 enable property to be changed accordingly with this UserControl1,panel2 visible property]

so I put this code
OnVisibilityChanged(new EventArgs());
inside the button1_clickEvent on the UserControl1.

On the form1.cs [[I think I am missing things here so I put the whole code]]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        void UserControl1_VisibilityChanged(object sender, EventArgs e)
        {
            UserControl1 uc = sender as UserControl1;
            if (uc != null)
            {
                button2.Enabled = uc.IsVisible;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            if (!mainPanel.Controls.Contains(UserControl1.Instance))
            {
                mainPanel.Controls.Add(UserControl1.Instance);
                UserControl1.Instance.Dock = DockStyle.Fill;
                UserControl1.Instance.BringToFront();
            }
            else
            {
                UserControl1.Instance.BringToFront();//
            }
           
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
    }
}



Do I did all these correctly?? anyways thanks for your time... :) !
OriginalGriff 7-Jan-18 10:46am    
The bit you probably forgot was to hook up the VisibitityChanged event to your handler - that doesn't happen automatically any more than it does when you add a textbox!

In the designer, add the handler to the UserControl1 instance event.
Adnan Maruf 7-Jan-18 13:32pm    
@OriginalGriff sir sorry for asking your time again. I vaguely understand your this statement "In the designer, add the handler to the UserControl1 instance event" here. Do you mean to add these code-->>

/// <summary>
/// Event to indicate VisibiltyChanged
/// </summary>
public event EventHandler VisibilityChanged;
/// <summary>
/// Called to signal to subscribers that VisibiltyChanged
/// </summary>
/// <param name="e"></param>
protected virtual void OnVisibilityChanged(EventArgs e)
    {
    EventHandler eh = VisibilityChanged;
    if (eh != null)
        {
        eh(this, e);
        }
    }


to the UserControl1.Designer.cs ??

if so then I already tried that and it giving me a bunch of error...!

and if not then can please show me exactly, what to put where? and what to do?! Please do consider me as a novices here cause my knowledge about coding and C# is so thin. Hope you will understand what I am trying to say. Thanks again :)
OriginalGriff 7-Jan-18 14:03pm    
No, that's the code that creates and raises the event - it goes in your UserControl1.cs file as part of your class, together with the property I showed you. Don't modify the designer file until you know exactly what you are doing - you can mess your whole control up so it won't display any more.

You added your user control to your form by dragging it from the toolbox, yes? That is "the designer" - highlight the control there and use the Properties pane ti examine the Events.

(If you don't understand that, find a basic Visual Studio tutorial and watch that - it will explain this stuff and there are loads of them. Watch one on the VS debugger as well - you will need to get familiar with that, it's the greatest tool you will ever meet!)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900