0

I Show and Hide my user controls in one of the usercontrols I have another one I want when the main usercontrol Show,the other usercontrol hide.

I get this when I want to add visible function for usercontrol

private void AddVisibleChangedEventHandler()
{
        this.VisibleChanged += new EventHandler(VisibleChanged);
}

private void VisibleChanged(object sender, EventArgs e)
{
        MessageBox.Show("Visible change event raised!!!");
}
Jayakumar Thangavel
  • 1,884
  • 1
  • 22
  • 29
games dl
  • 25
  • 1
  • 7
  • 1
    You're trying to assign an event handler to a *method*, not an event. What is the event you're trying to handle? Where is it defined? – David Nov 13 '19 at 16:44
  • Please add a [mcve]. It's pretty obvious from your code why it does not work, but I suspect there is more code that makes you think it *should*. You need to post that, otherwise you will not find the explanation satisfying. – nvoigt Nov 13 '19 at 16:44
  • I do not see any difference to the documentation : https://learn.microsoft.com/de-de/dotnet/api/system.windows.forms.control.visiblechanged?view=netframework-4.8 Are you sure you get this error ? – Holger Nov 13 '19 at 16:45
  • I Show and Hide my user controls in one of the usercontrols I have another one I want when the main usercontrol Show,the other usercontrol hide – games dl Nov 13 '19 at 16:47
  • Yes i think the problem is it's usercontrol – games dl Nov 13 '19 at 16:48
  • Link of error image http://uupload.ir/files/8r2h_add.png – games dl Nov 13 '19 at 16:50

2 Answers2

2

Rename your event handler function. You have declared a method with the name VisibleChanged. This name is already defined as an event in UserControl. Name your method anything else and it will start working correctly. For example:

private void AddVisibleChangedEventHandler()
{
    this.VisibleChanged += MyVisibleChangedHandler;
}

private void MyVisibleChangedHandler(object sender, EventArgs e)
{
    MessageBox.Show("Visible change event raised!!!");
}
dotNET
  • 33,414
  • 24
  • 162
  • 251
1

You need to have an event declared in your class like this:

public event EventHandler ExampleEvent;

and then you need to associate a method delegate to that event, turning that method/delegate into the event handler.

this.ExampleEvent+= this.ExampleEventHandlerClassMethod;

or a static method

this.ExampleEvent += ExampleClass.ExampleStaticEventHandler;

Your example looks very brief, but it looks like that class either has an event called VisibleChanged or your declaring a method called Visible Changed and then trying to assign that method to handle itself, which doesn't make sense.

If you already have the event, then you should try changing your method name that you want to use to handle that event to something like VisibleChangedHandler and then do this.VisibleChanged += this.VisibleChangedHandler;

This looks relevant: Cannot Assign because it is a method group C#?

And this: https://learn.microsoft.com/en-us/dotnet/standard/events/

n234
  • 249
  • 1
  • 6
  • c is inacsessable – games dl Nov 13 '19 at 17:00
  • what is the 'c' – games dl Nov 13 '19 at 17:02
  • 1
    `c` is an instance of a class that has the event `ExampleEvent`. That was a mistake in my answer, you might try `this` instead of `c`, unless you want to bind the event handler from outside of the class, in which case you would use `instanceOfExampleClass.ExampleEvent += ExampleEventHandler` – n234 Nov 13 '19 at 17:17