Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. calling a parent's form method from a child form

calling a parent's form method from a child form

Scheduled Pinned Locked Moved C#
helpcsharpvisual-studioquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rzvme
    wrote on last edited by
    #1

    Harta hr = this.ParentForm; hr.OnPaint1(); this code raises the folowing error Error 1 Cannot implicitly convert type 'System.Windows.Forms.Form' to 'THE_PROJECT.Harta'. An explicit conversion exists (are you missing a cast?) C:\Documents and Settings\Razvan\My Documents\Visual Studio 2005\Projects\THE PROJECT v2\THE PROJECT v2\Elemente.cs 48 23 THE PROJECT v2 i knew it was imposible for that to work but i need to call that method from the child form. Can somebody help me?

    rzvme

    P T 2 Replies Last reply
    0
    • R rzvme

      Harta hr = this.ParentForm; hr.OnPaint1(); this code raises the folowing error Error 1 Cannot implicitly convert type 'System.Windows.Forms.Form' to 'THE_PROJECT.Harta'. An explicit conversion exists (are you missing a cast?) C:\Documents and Settings\Razvan\My Documents\Visual Studio 2005\Projects\THE PROJECT v2\THE PROJECT v2\Elemente.cs 48 23 THE PROJECT v2 i knew it was imposible for that to work but i need to call that method from the child form. Can somebody help me?

      rzvme

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Harta hr = this.ParentForm as Harta; if (hr != null) hr.OnPaint1();

      Arthur Dent - "That would explain it. All my life I've had this strange feeling that there's something big and sinister going on in the world." Slartibartfast - "No. That's perfectly normal paranoia. Everybody in the universe gets that." Deja View - the feeling that you've seen this post before.

      1 Reply Last reply
      0
      • R rzvme

        Harta hr = this.ParentForm; hr.OnPaint1(); this code raises the folowing error Error 1 Cannot implicitly convert type 'System.Windows.Forms.Form' to 'THE_PROJECT.Harta'. An explicit conversion exists (are you missing a cast?) C:\Documents and Settings\Razvan\My Documents\Visual Studio 2005\Projects\THE PROJECT v2\THE PROJECT v2\Elemente.cs 48 23 THE PROJECT v2 i knew it was imposible for that to work but i need to call that method from the child form. Can somebody help me?

        rzvme

        T Offline
        T Offline
        tgrt
        wrote on last edited by
        #3

        The method that Pete mentions is valid. However, I would suggest you look at having the child form raise an event that the parent form subscribes to. public class MainForm : Form {     public void CreateChild()     {        ChildForm cForm = new ChildForm();        cForm.ParentForm = this;        cForm.MyOnPaint += new EventHandler(OnPaint1);        cForm.Show();     }     protected override void OnPaint1(object sender, EventArgs e)     {     } } public class ChildForm : Form {     public event EventHandler MyEvent;     protected override void OnMyEvent(object sender, EventArgs e)     {        if (MyEvent != null)           MyEvent(sender, e);     } } Please forgive any typos as I wrote the above code on-the-fly. So, in the child form when something happens that you want to communicate back the event you just call OnMyEvent from within the ChildForm. The nice thing about this is that it promotes loose coupling, because the ChildForm doesn't know if it's parent is MainForm or SomeOtherForm. You can take it a step further by making the event part of an interface that ChildForm implements. In that way you decouple the child from the parent as well.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups