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. How do you access the forms child objects from a different class?

How do you access the forms child objects from a different class?

Scheduled Pinned Locked Moved C#
helpquestion
4 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.
  • J Offline
    J Offline
    JstDaNuGuy
    wrote on last edited by
    #1

    I'm trying to access my forms (Form1) child objects, such as panels, sliderbars, toolbars, etc from a second class that i've added to the project. Currently i'm accessing Form1 via Form1.ActiveForm the problem that i'm having is that i can access the standard stuff from the form class, but nothing else, is this because Form1 is a public class, and all the child objects are private? if this is the case is it safe for me to change only the objects that i need to access to public, or is that unsafe coding? Is there a better way? or am i just going about this all wrong? :-D :laugh: ;p :^) :sigh:

    D C 2 Replies Last reply
    0
    • J JstDaNuGuy

      I'm trying to access my forms (Form1) child objects, such as panels, sliderbars, toolbars, etc from a second class that i've added to the project. Currently i'm accessing Form1 via Form1.ActiveForm the problem that i'm having is that i can access the standard stuff from the form class, but nothing else, is this because Form1 is a public class, and all the child objects are private? if this is the case is it safe for me to change only the objects that i need to access to public, or is that unsafe coding? Is there a better way? or am i just going about this all wrong? :-D :laugh: ;p :^) :sigh:

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      Is it safe? Yeah. Is it proper coding practice? No way! Your second class shouldn't know anything about the form that called it. Your Form should be passing the data it needs to to the class methods and processing any return data required. Your second class should not be trying to modify the form, or read from it, at all. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      1 Reply Last reply
      0
      • J JstDaNuGuy

        I'm trying to access my forms (Form1) child objects, such as panels, sliderbars, toolbars, etc from a second class that i've added to the project. Currently i'm accessing Form1 via Form1.ActiveForm the problem that i'm having is that i can access the standard stuff from the form class, but nothing else, is this because Form1 is a public class, and all the child objects are private? if this is the case is it safe for me to change only the objects that i need to access to public, or is that unsafe coding? Is there a better way? or am i just going about this all wrong? :-D :laugh: ;p :^) :sigh:

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        JstDaNuGuy wrote:

        if this is the case is it safe for me to change only the objects that i need to access to public, or is that unsafe coding?

        You should not make any field in a class public. See this article on Why make fields in a class private, why not make them public?[^]

        JstDaNuGuy wrote:

        Is there a better way?

        Yes. But it depends on what you are trying to do. What is the purpose of the second class? And what is the reason for the interaction? ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

        J 1 Reply Last reply
        0
        • C Colin Angus Mackay

          JstDaNuGuy wrote:

          if this is the case is it safe for me to change only the objects that i need to access to public, or is that unsafe coding?

          You should not make any field in a class public. See this article on Why make fields in a class private, why not make them public?[^]

          JstDaNuGuy wrote:

          Is there a better way?

          Yes. But it depends on what you are trying to do. What is the purpose of the second class? And what is the reason for the interaction? ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

          J Offline
          J Offline
          JstDaNuGuy
          wrote on last edited by
          #4

          thanks for the information guys... what im' trying to do is set the form state upon load then save on exit. but i have much more to set/save than just the form size, and i was just trying to do it from a different class i'm still learning... :-D :laugh: ;p :^) :sigh:

          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