2

I am having some problems in an exercise that I'm doing for the graphical interfaces course. I'm writing a program in F# defined in this way:

  • I have a class A, in which I override the method OnPaint;
  • I have another class B, in which I override the OnPaint method, OnMouse[Down/Move/Up] methods, etc.

I would use the overridden OnPaint method in A into the overridden OnPaint method defined in B (obviously, the OnPaint method in A, is defined in the class A, which means that I have to instantiate a type A object).

My question is: how can I do that? Do I need to define necessarily a method in A in which I pass a PaintEventArgs.Graphics parameter with the same tasks of the OnPaint method of A instead of override the OnPaint method in A?

An example: I've to do something like this:

type Ellipse() =
   ...
   override this.OnPaint e =
       e.Graphics.DrawEllipse(10.f, 10.f, 30.f, 30.f)

type classThatUseEllipse() =
   let ell = new Ellipse()
   ...
   override this.OnPaint e =
      ell.OnPaint(e)

Or something like this?:

type Ellipse() =
   ...
   let myOnPaint (e:PaintEventArgs) =
       e.Graphics.DrawEllipse(10.f, 10.f, 30.f, 30.f)

type classThatUseEllipse() =
   let ell = new Ellipse()
   ...
   override this.OnPaint e =
      ell.myOnPaint(e)

Or these two versions are the same?

I'm asking this because often the first version gave problems.

Carmine
  • 109
  • 2
  • 8
  • You really shouldn't call onpaint directly. Instead, just invalidate the child object (which may not even be required). – John Palmer Jan 22 '16 at 10:15
  • So, do something like what? Something like: ell.Invalidate() ? Does this work with the other overridden methods (i.e. OnMouse[Down/Move/Up]) too? – Carmine Jan 22 '16 at 10:34
  • Can you be specific about the problems? What problems did the first version give you? – TheInnerLight Jan 22 '16 at 14:01
  • Actually, the error shows up in a similar case, in which I do the same things described earlier, and the error message is: "Method 'OnPaint' is not accessible from this code location." – Carmine Jan 22 '16 at 15:33
  • Sounds like an accessibility modifier issue? What is the accessibility of `OnPaint`? – TheInnerLight Jan 22 '16 at 16:06
  • To override a method you need to inherit from an other class first. I can't see any inherit statement in your code samples. – Functional_S Jan 22 '16 at 16:29
  • Your graphical interfaces course uses F#? That would be awesome! Where do you study? – Asik Jan 22 '16 at 17:51
  • As has been mentioned, you can't override "into" a different class, but what you can do is use a delegate. Both inheritance/overrides and delegates should be in your course literature. If you've only covered one of them in class, that's the one you should use. – molbdnilo Jan 22 '16 at 18:21
  • @Carmine - invalidate essentially will call onpaint for you. The idea is that if you have a form, it calls invalidate on itself and all the child elements so that they all repaint themselves. – John Palmer Jan 22 '16 at 20:54

0 Answers0