Blareprefix
Blareprefix

Reputation: 870

C#, need some help with changing form

  1. I would like som help to close form1 as you open form2.

    Form2 myForm = new Form2(); 
    myForm.Show();
    
  2. I would like to know how to communicate between forms, like sending integers between?

Thanks!

Upvotes: 0

Views: 84

Answers (4)

Anirudh Ramanathan
Anirudh Ramanathan

Reputation: 46728

There are several ways you could do that. See this, it lists 4 ways you could go about doing just that...

Upvotes: 2

Roman Royter
Roman Royter

Reputation: 1665

Forms are just classes. When you say Form2 myForm = new Form2(); you just create a new instance of a class. You communicate to an object (the instance of a class) by calling its methods, setting its properties or raising its events. No magic here.

In particular when you say myForm.Show(), you have already communicated to the other form. You just didn't realize it. It just so happened, that your Form2 class has had a method called Show, so it worked. But you can create your own methods and call them in the same way.

Upvotes: 0

Mediator
Mediator

Reputation: 15378

Form2 myForm = new Form2(this);
myForm.Show();

constructor Form2:

Window _parent;
void Form2(Window parent)
{
   _parent = parent;
}

and use _parent

Upvotes: 0

Denis Biondic
Denis Biondic

Reputation: 8201

form1.Close();
myForm.Show();

For second question -> forms are just objects. Learn OOP first, and concepts of class variables, properties, constructors etc... Then, use that to pass data between two objects (two forms)

Upvotes: 4

Related Questions