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 functions and methods in a child form

Calling functions and methods in a child form

Scheduled Pinned Locked Moved C#
questioncsharp
6 Posts 4 Posters 1 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.
  • A Offline
    A Offline
    alias bill
    wrote on last edited by
    #1

    I am relatively new to C# windows programming and have lots to learn. I want to call a function in a child window from the parent window in an MDI application. If I make the function STATIC the parent form can see it, however a control(textbox)in the child function is not recognized, 'MainWindow.FileForm.textBox1' denotes a 'field' where a 'class' was expected'. If I make the function non-static I cannot call it from the parent form. How do I accomplish the call? Thank you..

    OriginalGriffO D N 3 Replies Last reply
    0
    • A alias bill

      I am relatively new to C# windows programming and have lots to learn. I want to call a function in a child window from the parent window in an MDI application. If I make the function STATIC the parent form can see it, however a control(textbox)in the child function is not recognized, 'MainWindow.FileForm.textBox1' denotes a 'field' where a 'class' was expected'. If I make the function non-static I cannot call it from the parent form. How do I accomplish the call? Thank you..

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      You do indeed have lots to learn, and this bit is fundamental. When you declare a class, it does not exist yet:

      public class Car
      {
      public static int GetWheels()
      {
      return 4;
      }
      public int GetFuel()
      {
      return fuel;
      }
      }

      You can happily count the wheels, because that is common to all cars - they all have the same number of wheels.

      ...
      Console.WriteLine("All cars have {0} wheels", Car.GetWheels());
      ...

      But if you try to get the fuel, the compiler complains that you "need an instance". What it is saying is that a car could be diesel, petrol, or electric. GetFuel() cannot be static, because it is not shared by all cars. You can however refer to instances of cars:

      ...
      Car myCar = new Car("Mitsubishi", "Diesel");
      Car yourCar = new Car("Ford", "Petrol");
      Car hisCar = new Car("Toyota", "Hybrid");
      ...

      and then it is fine to refer to the specific car and it's fuel type:

      Console.WriteLine("Your car is {0}", yourCar.GetFuel());
      Console.WriteLine("My car is {0}", myCar.GetFuel());
      Console.WriteLine("His car has {0} wheels", hisCar.GetWheels());

      Does that make sense?

      No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • A alias bill

        I am relatively new to C# windows programming and have lots to learn. I want to call a function in a child window from the parent window in an MDI application. If I make the function STATIC the parent form can see it, however a control(textbox)in the child function is not recognized, 'MainWindow.FileForm.textBox1' denotes a 'field' where a 'class' was expected'. If I make the function non-static I cannot call it from the parent form. How do I accomplish the call? Thank you..

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        I like OriginalGriff's analogy! Just in case it's not clear, and without the car thing... Somewhere in your parent you are creating an instance of FileForm. Maybe something like this:

        FileForm fileForm = new FileForm();

        The instance fileForm is what you need to access that instance. If you need to, you can use the Parent's MdiChildren property to iterate over all child windows and find the instance you need.

        foreach (Form form in MdiChildren)
        {
        if (form is FileForm)
        {
        FileForm fileForm = form as FileForm;
        fileForm.YourMethod();
        break;
        }
        }

        I notice you are trying to access FileForm.textBox1. Control instances in a form are by default added as private and should stay that way. If you want to do something with a control, add a method or property in the FileForm class and use that to manipulate the control.

        public void SetTextBox1(string text)
        {
        textBox1.Text = text;
        }

        fileForm.SetTextBox1("Hello World!");

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        A 1 Reply Last reply
        0
        • D DaveyM69

          I like OriginalGriff's analogy! Just in case it's not clear, and without the car thing... Somewhere in your parent you are creating an instance of FileForm. Maybe something like this:

          FileForm fileForm = new FileForm();

          The instance fileForm is what you need to access that instance. If you need to, you can use the Parent's MdiChildren property to iterate over all child windows and find the instance you need.

          foreach (Form form in MdiChildren)
          {
          if (form is FileForm)
          {
          FileForm fileForm = form as FileForm;
          fileForm.YourMethod();
          break;
          }
          }

          I notice you are trying to access FileForm.textBox1. Control instances in a form are by default added as private and should stay that way. If you want to do something with a control, add a method or property in the FileForm class and use that to manipulate the control.

          public void SetTextBox1(string text)
          {
          textBox1.Text = text;
          }

          fileForm.SetTextBox1("Hello World!");

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
          Why are you using VB6? Do you hate yourself? (Christian Graus)

          A Offline
          A Offline
          alias bill
          wrote on last edited by
          #4

          Thank for taking the time to answer. Your suggestions worked! I mistakingly thought that instantiating the FileForm in the parent form would make FileForm labeled "FileWindow" visible. FileForm FileWindow=new FileForm(); FileWindow.MdiParent=this; FileWindow.Show(); As I'm an engineer an not a professional programmer I will never learn everything necessary to be competent. Just enough to solve a problem. If there are any books or references that you may know of that focus on applications and you have the time would you please respond. Regards, Bill C.

          D 1 Reply Last reply
          0
          • A alias bill

            I am relatively new to C# windows programming and have lots to learn. I want to call a function in a child window from the parent window in an MDI application. If I make the function STATIC the parent form can see it, however a control(textbox)in the child function is not recognized, 'MainWindow.FileForm.textBox1' denotes a 'field' where a 'class' was expected'. If I make the function non-static I cannot call it from the parent form. How do I accomplish the call? Thank you..

            N Offline
            N Offline
            nelsonpaixao
            wrote on last edited by
            #5

            i don know what kind of design you have in mind for your application :doh: you can also use usercontrols like pages create a single form that you fill with the usercontrols designed like page, then bring to front the page you want. You can do this insead of creating child forms, if you don´t want to open new windows and run all the show in the same window you have to manage delegates here

            nelsonpaixao@yahoo.com.br trying to help & get help

            modified on Thursday, August 6, 2009 8:05 PM

            1 Reply Last reply
            0
            • A alias bill

              Thank for taking the time to answer. Your suggestions worked! I mistakingly thought that instantiating the FileForm in the parent form would make FileForm labeled "FileWindow" visible. FileForm FileWindow=new FileForm(); FileWindow.MdiParent=this; FileWindow.Show(); As I'm an engineer an not a professional programmer I will never learn everything necessary to be competent. Just enough to solve a problem. If there are any books or references that you may know of that focus on applications and you have the time would you please respond. Regards, Bill C.

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              alias bill wrote:

              I will never learn everything necessary to be competent

              Don't sell yourself short! C# isn't *that* hard. I don't know everything by a long way and still ask questions here myself from time to time, but I consider myself competent. I don't have any C# books, so I can't recommend any personally. All my knowledge has been gained from studying articles/blogs/MSDN - and with the help of CodeProject, I've learned enough to know when what I am reading is rubbish or is good stuff. Just keep at it, and with an internet connection you have all you need, though a good book will help if you can find any recommendations.

              Dave
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
              Why are you using VB6? Do you hate yourself? (Christian Graus)

              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