7

Is it possible to design a method in such a fashion, that it knows it must automatically call a next method in succession upon exiting?

In the following example, I must call Refresh() to cause my form to repaint after this event takes place. The problem is that, it's ugly to call Refresh() after, for example, 20 different events which must make the form refresh. e.g

private void PriorityLine_Click(object sender, EventArgs e)
{
   _showPriorityLine = (_showPriorityLine) ? false : true;
  Refresh(); // Must call refresh for changes to take effect.
}

I suppose what I'm looking for is some kind of signature I can apply to the method to cause it to automatically chain to the next method, regardless from where its called. e.g

(I know this isn't syntactically correct.)

private void PriorityLine_Click(object sender, EventArgs e).Refresh()
{
   _showPriorityLine = (_showPriorityLine) ? false : true;
}

I want to seperate the interface of the method, from the logic contained within the method. I understand it would be the exact amount of effort, if not more. For example, if I were to edit the method and accidently removed Refresh, it would cause my application to break. Whereas, if the Refresh method was outside of the actual logic of the method, I could do anything within the method without worrying about removing the next chain of logic.

14
  • 5
    Off-topic, consider writing the first statement as _showPriorityLine = !_showPriorityLine;
    – Ani
    Jan 28, 2011 at 15:11
  • Are you setting these handlers up from the designer?
    – Ani
    Jan 28, 2011 at 15:12
  • @Ani Yes. I am setting these up from the designer. Jan 28, 2011 at 15:13
  • 2
    If you have to add the call in the method signature is there really a difference? You still need to deal with the multiple handlers; unless you can share the same handler...which will simplify your code. Jan 28, 2011 at 15:14
  • 1
    Sounds like Aspects are what you want. Jan 28, 2011 at 15:14

7 Answers 7

7

Sounds like what you want is Aspect Oriented Programming, there are a number of different frameworks to enable you to have stuff "magically" happen after some set of methods have run, have a look here AOP programming in .Net?

2
  • +1 for being the only one to give the correct name for what the OP is describing
    – user229044
    Jan 28, 2011 at 16:16
  • No reason to use a sledgehammer when a hammer -- or even a thumb -- will do.
    – Pedro
    Jan 28, 2011 at 16:34
1

I'm not aware of any really clean way. One method would be to use PostSharp.

0

You could encapsulate the changes which would cause the form to refresh into form-level properties.

For instance,

private bool _showPriorityLine;
private bool ShowPriorityLine
{
    get { return _showPriorityLine; }
    set
    {
        _showPriorityLine = value;
        Refresh();
    }
}

Then your event would just be

private void PriorityLine_Click(object sender, EventArgs e)
{
    ShowPriorityLine = !ShowPriorityLine;
}

Of course, that only cleans up your code if you have several events manipulating the same variables that cause the form to need refreshing.

3
  • Still pretty new to StackExchange, so this may be out of line but I'm wondering why (when I took the extra time to give the code example and explain the caveat to my solution) this was worthy of a down vote? Did I break some protocol or something? I was just offering an example that I use often for simple cases like this to do exactly what was requested -- cleanly separate UI from logic (without taking the time to offer up the complexity of a design pattern like MVP).
    – Eric H
    Jan 28, 2011 at 17:20
  • that question, no doubt, has been asked many times. Some of the voting is a mystery on this site. A lot of it has to do with people following the pack. There are cliques and cult followings here as well. Sometimes two answers say the exact same thing (posted at the same time too) and one of them will get up-voted while the other one won't. Now, to your answer specifically, look at my comment on James's answer. The same applies to you. I don't know why you got down-voted and he didn't. I suspect he's one of the "beautiful people", though.
    – Pedro
    Jan 28, 2011 at 21:34
  • +1 to balance out the -1 as we do have the same idea in mind. @Pedro - I don't know why my answer never got downvoted yet Eric's did, perhaps it was because mines was more of a generic/extensible solution. Or maybe people were too busy reading your back and forths with people :P IMO your solution got downvoted because you basically demanded that it was the correct solution and then stamped your feet because no one agreed. It's up to the OP to decide what solution best suits their requirements. All we can do is give our 2 cents...
    – James
    Jan 29, 2011 at 1:43
0

Taking into consideration your particular problem and the solutions posted, I would say the "cleanest" approach here would be to implement a Property Changed Notification just for internal use in the form i.e. you don't need to expose the event like in the MSDN example.

This way you could maintain an internal list of properties that you know will require the form to be refreshed e.g.

  private List<string> _refreshProps = new List<string>();
  private bool _showPriority;

  public void Form()
  {
      _refreshProps.Add("ShowPriority");
      ... etc
  }

  // only implement properties like this that need some extra work done
  public bool ShowPriority
  {
      get { return _showPriority; }
      set
      {
          if (_showPriority != value)
          {
              _showPriority = value;
              // Call OnPropertyChanged whenever the property is updated
              OnPropertyChanged("ShowPriority");
          }
      }
  }

  // basic property that doesn't require anything extra
  public bool AnotherProperty { get; set; }

  public void Refresh()
  {
      // refresh the form
  }

  protected void OnPropertyChanged(string name)
  {
      if (_refreshProps.Contains(name))
          Refresh();
  }

The benefit of this approach is if in the future you needed to do other "stuff" after particular properties you can simply introduce another list and handle it again in your OnPropertyChanged method.

2
  • One problem with this is that you may end up calling Refresh() many times because you changed many properties - when all that was required was to call Refresh() at the end.
    – Pedro
    Jan 28, 2011 at 16:01
  • @Pedro - Good point, although you could introduce a flag which could prevent that from happening i.e. RefreshCount > 0. I am just trying to give a basis from which the OP could work from.
    – James
    Jan 28, 2011 at 16:08
0

Don't call Refresh, call Invalidate. The mechanism you need is already built into Windows. Calling Invalidate simply makes a note that the window needs repainting. The operating system will eventually post a WM_PAINT message (typically after the root DispatchMessage call finishes, but the exact implementation is irrelevant).

2
  • While that is good advice, which I will take into account, using Refresh is just an example, and not the basis of the question. :) Jan 28, 2011 at 16:12
  • You're right, I completely misread the question. Sorry about that.
    – Tergiver
    Jan 28, 2011 at 20:04
-1

Use a property that calls Refresh in the setter.

4
  • 1
    this moves his "problem" to property setters rather than event handlers. He still has the same problem as before, just in a different spot. Jan 28, 2011 at 15:14
  • @Anthony Technically no...if _showGridLines was set via its property ShowGridLines versus its variable instance...refresh would only be called in one place and the 2 lines would be replaced with ShowGridLines = !ShowGridLines; Jan 28, 2011 at 15:23
  • 1
    Just for the record, I didn't supply the downvote. @Aaron, presumably, his 20+ events are changing different variables or properties, so those 20+ events would have 20+ properties, which would still leave Refresh() in 20+ different places. Unless I'm either reading or assuming something entirely wrong. Jan 28, 2011 at 15:37
  • @Anthony You are correct...I was under the impression the property was the same across the multiple handlers; after looking at it again it appears the property would differ with each handler lending the above solution to not be very effective as you mentioned. Jan 28, 2011 at 16:02
-5

Something like this:

private void RefreshAfter(Action action)
    {
        action();
        Refresh();
    }

UPDATED TO MAKE IT MORE OBVIOUS:

    private void DoSomeUiShiznit(Action action)
    {
        action();
        // other parts of the code don't realize that Refresh has to be called.
        // But that's cool. I got it covered.
        Refresh();
    }

    private void PriorityLine_Click(object sender, EventArgs e)
    {
        DoSomeUiShiznit(() => { _showPriorityLine = !_showPriorityLine; });
    }

UPDATE -- Just a message to the down-voters:

What some of you are too blind to see is that this is not all that different from:

[SomeRefreshAttribute]  
private void PriorityLine_Click(object sender, EventArgs e)  
{
    _showPriorityLine = !_showPriorityLine;  
}  

Except that it is simpler, and doesn't require adding another framework to the solution. And yet the other answer suggesting as much don't get down-voted!

What's wrong with you people?

9
  • @George: updated to make it obvious how to use it. No charge.
    – Pedro
    Jan 28, 2011 at 15:45
  • 1
    @Pedro This doesn't effectively solve the issue with seperating the interface of the method from the logic. I would still need to manually call your DoSomeUiShizit method, which would only serve to add additional steps to achieve the same result. Jan 28, 2011 at 16:15
  • 2
    @Pedro This is far from the best answer. You're replacing a simple and obvious call to Refresh() with this obfuscated DoSomeUiShiznit call. It's more typing every time, for no benefit. If @George has to type out an extra function call every time anyways, he might as well type Refresh(). Talk about "jumping through hoops"...
    – user229044
    Jan 28, 2011 at 16:28
  • 2
    @Pedro Read the very first sentence of the question. There is nothing automatic about your solution; you're replacing one word with another word. There is a 1-to-1 substitution of function calls. You're adding complexity for no gain, and not addressing the question.
    – user229044
    Jan 28, 2011 at 16:37
  • 1
    @Pedro Look. Your solution to "hide calling Refresh()" is literally equivalent to renaming the function from "Refresh" to "DoSomeUiShiznit()" or "X" or whatever name you choose. You've solved nothing. You're not hiding the method call in any way. I don't know why you quoted OP about applying a signature to the method, because that is not at all what you're doing.
    – user229044
    Jan 28, 2011 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.