How Can I Make an Object in Form1 Work in Form2?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dr Cake
    New Member
    • May 2012
    • 8

    How Can I Make an Object in Form1 Work in Form2?

    Hello! I've got a calendar in Form 1 in my code, and buttons in Form2. I need these buttons to send codes for this calendar in Form1. How may I make these work? My friend told me to do a function as a new class, but I'm not sure how can I do this. Glad if you can help :)
  • Subin Ninan
    New Member
    • Sep 2010
    • 91

    #2
    Make control static.

    public static Calender clndr = new Calender();

    Comment

    • India777
      New Member
      • Apr 2012
      • 61

      #3
      Create the object for Form1 in Form2.

      Form1 frm1=new Form1();
      access the Controls in Form1 using the Variable frm1.

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Both of those are terrible suggestions. Making it static will have major consequences if you use more than one instance of the form. And India777's answer simply will not work. That's creating a new instance of Form1, not the one that already exists.

        Read this:



        Edit: Forgot that I posted that article here at Bytes too:



        The other one has comments where I expand on a few things, so I'll leave it posted.
        Last edited by Curtis Rutland; May 21 '12, 07:42 PM.

        Comment

        • Dr Cake
          New Member
          • May 2012
          • 8

          #5
          Thanks Curtis! I might be new to C#, but I already knew those other 2 replies wouldn't work, so I didn't even try. But you linked me to really what I needed. Many thanks!!

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            No problem. I actually wrote that article, so let me know if you have any questions.

            Comment

            Working...