0

I want to change control properties on my forms during run time using a PropertyGrid but first id like to disable all the functionality of the form (events).

So I was thinking when editing the properties, call the form in to some kind of Edition state, If call the form with controls disabled the user is going to be able to enable it again (changing Property Grid) so it doesn’t seem viable solution, is there a way which I can disable all events that has code of my controls? Or can anyone give me another idea? or if this is a really bad idea please let me know.

Barr J
  • 10,636
  • 1
  • 28
  • 46
Jorge
  • 17
  • 4
  • Have you tried anything yet? Please show us your effort and ask specific questions. – Md. Suman Kabir Oct 31 '18 at 05:48
  • Hi there! Your question is too broad. Here, on stackoverflow, we dealing with coding issues, so if you have a problem with your code (exceptions, unpredictable behavior, etc.) - edit your question or ask another one. You may also find this link useful: https://stackoverflow.com/help/how-to-ask – jazb Oct 31 '18 at 05:50
  • What I have done until now is add a strip menu to each control in the form using a recursive function and then how to explore the properties by right clicking on the control, and save the changed properties into an xml file… But this have nothing to do with disabling the functionality of the form, I have been thinking on it but I’m not sure what to do. – Jorge Oct 31 '18 at 06:02
  • Sorry I'm not having coding issues yet, I'm only dealing with the idea... but thank you any way – Jorge Oct 31 '18 at 06:05
  • Generally speaking, your question is about issue that has been solved many times. And you have not done good research asking it. But since I had this code right under my hand, I posted an answer for you. Don't get upset but your question will probably be closed – T.S. Oct 31 '18 at 06:08
  • Possible duplicate of [Stop comboBox's selectedIndexChanged event from firing when the form loads](https://stackoverflow.com/questions/3263240/stop-comboboxs-selectedindexchanged-event-from-firing-when-the-form-loads) – T.S. Oct 31 '18 at 06:09

2 Answers2

1

Usually, this is achieved in 2 ways (pseudo-code)

'#1'

public class MyForm : Form
{
    private bool _stopEvent = false;

    private void OnComboSelectedIndexChanged(sender, e)
    {
        if (_stopEvent)
            return;
        // REAL event code here
    }

    private void DoSomething()
    {
         _stopEvent = true;
         cbo.SelectedIndex = 3;
         _stopEvent = false;
    }  

    // . . .  . . .  . . .

'#2'

    private void DoSomething()
    {
         cbo.SelectedIndexChanged -= OnComboSelectedIndexChanged;
         cbo.SelectedIndex = 3;
         cbo.SelectedIndexChanged += OnComboSelectedIndexChanged;
    } 
T.S.
  • 18,195
  • 11
  • 58
  • 78
  • For solution number 2 I must know the methods name' of the event that I want to unsuscribe and I was thinking to implement this in any form whitout knowing th – Jorge Oct 31 '18 at 06:17
  • @Jorge I am wondering, how you gonna do that? Of course, you can assign anonymous delegate handler. This is the only way to do it without actual method name. The method itself in this case can come from any other class. Disable events while changing control properties. But your question doesn't ask about **dynamic delegate handlers**. It says: *"Disable events while changing control properties"*. so here is your answer. Please, don't make your question long mutating one. If you have 2 questions, then post 2 questions – T.S. Oct 31 '18 at 06:22
0

This is what I was looking for... in this link How to remove all event handlers from an event

public EventHandlerList DetachEvents(Component obj) {

        //**********************
        object objNew = obj.GetType().GetConstructor(new Type[] { }).Invoke(new object[] { });
        PropertyInfo propEvents = obj.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance);

        EventHandlerList eventHandlerList_obj = (EventHandlerList)propEvents.GetValue(obj, null);
        EventHandlerList eventHandlerList_objNew = (EventHandlerList)propEvents.GetValue(objNew, null);

        eventHandlerList_objNew.AddHandlers(eventHandlerList_obj);
        eventHandlerList_obj.Dispose();

        return eventHandlerList_objNew;
    }
Jorge
  • 17
  • 4