Jump to content

[VS2k10][.NET] Form components available in another form

- - - - -

  • Please log in to reply
5 replies to this topic

#1
niemamnie12

niemamnie12

    Newbie

  • Members
  • Pip
  • 3 posts
Hello. I have question concerning programming Windows Forms (.NET) in Microsoft Visual Studio 2010. I have such situation, my main program window is designed as Form1. In my application I need another window so I have add new item (Windows Form) to the project - Form2. I have designed Form2 and I have created new Form2 object in Form1, now by the use of Show() Hide() methods I "open" and "close" this window in Form1. Everything works fine, but now I need to change the state of some Form1 components by the methods from Form2 object. For example pressing Form2 button should change text in Form1 textBox. I have solve this problem by adding an argument to the Form2 constructor:
public partial class Form2 : Form

{

   private Form1 old_form;


   public Form2(Form1 F)

   {

      ...

      old_form = F;

   }

   ...

}
I change the given Form1 component (e.g. textBox) modifier to public and by this code I am able to change this object properties directly from Form2:
old_form.textBox1.Text = "Bla";
My first question is how it is possible? I don't understand why it works. As far as I know the old_form in Form2 is a new created object, which is only initialized by the argument F. And F is just a copy of the main Form1 object, so why changing its properties change in fact the first Form1? For me such behaviour seems like a reference, but there is no ref keyword in the constructor. Anyone could explain it to me?

My second question is if there is any other solution to solve this problem? To change Form1 components properties directly from the Form2? Because I think that described above solution is not quite "nice".

Thank You for help!

#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
Objects are passed by reference automatically, you don't need the ref keyword for them (using ref has some other effects, I suggest reading up about them.

old_form is not a newly created object, it's a reference to the original form. Thus changes you make to public properties are changes to that form. Your basic problem is not understanding how objects are passed.

As for your second question, it really depends on what you are trying to do. There might be a better way since now you have very tight coupling of the forms (form1 needs to know about form2, and form2 needs to know about form1).

#3
niemamnie12

niemamnie12

    Newbie

  • Members
  • Pip
  • 3 posts
I don't understand why in this case objects are passed automatically by reference. As far as I learn C# I know that by default parameters are value parameters and they are not passed by reference.
private void AddSome(int x)

{

      x += 12;

}
int x = 2;

AddSome(x);
In above code x will be not changed. So why and how in my case the arguments are passed automatically by reference?

#4
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
int is a value type, not an object type. Value types (int, long, float, double, decimal, etc.) are passed by value, objects are passed by reference.

Also, something to show the difference between passing an object, and passing an object reference:
using System;


namespace SquaredResult {

    static class Program {

        static void Main(string[] args) {

            O a = new O();


            a.Text = "This is the text for A";


            Proc1(a);

            Console.WriteLine("Procedure 1 = {0}", a.Text);


            Proc2(ref a);

            Console.WriteLine("Procedure 2 = {0}", a.Text);


            Console.ReadLine();

        }


        static void Proc1(O z) {

            z = new O();

            z.Text = "This is the text for Z";

        }


        static void Proc2(ref O z) {

            z = new O();

            z.Text = "This is the text for Z";

        }

    }


    class O {

        public String Text { get; set; }

    }

}


#5
niemamnie12

niemamnie12

    Newbie

  • Members
  • Pip
  • 3 posts
Than You for this explanation. I thought that those value types (int, double etc.) are just aliases for objects, i.e. typing "int x" create an object of System.Int32 class type. So if they are not objects, so what they are?

#6
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 242 posts
They are value types that can be automatically boxed (converted to objects when needed) and unboxed (converted back to value types) by the system. Things get more confusing when you consider that
3.ToString()
is a valid statement :)

The Int32, Int64, etc. types are actually structs, which are also considered value types (as are enums).

This is the documentation on value types.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users