Click to See Complete Forum and Search --> : How to adress Form?


orhor
February 18th, 2007, 06:14 AM
Hi, this will look trivial to you:

I know that I must adress a form by the word 'this' when I write some code within the form class.
But how to adress a form when I want to adress it from fe. another form?

I cant get oriented what is what. I cant get oriented what is the top level entity, what is wich parent and child etc.

What if I create some object in Program Class, right before a line Application.Run(new Form1()); I can verify that object exist by MessageBox for example. How can I adress this object from Form1?

Can somebody help me please? Thank you.

I use VS2005

petes1234
February 18th, 2007, 07:51 AM
As far as I understand it, your form is nothing more than a subclass of the parent class "Form", and when instantiated is nothing more than an object of your subclass. You refer to it the same way you would any other object. Whether or not it is within scope at the particular location where you want to find a reference to it depends entirely upon your code.

What if I create some object in Program Class, right before a line Application.Run(new Form1()); I can verify that object exist by MessageBox for example. How can I adress this object from Form1?
If I wanted to refer to an object that was located in a class that instantiates my form object, I'd create a new constructor for my form that allows me to pass this object as a parameter.

orhor
February 18th, 2007, 08:19 AM
You refer to it the same way you would any other object.

And thats why Im asking.
There are two forms. Both created in the Main() method. One of them by this statement
Form2 Form2 = new Form2();

and one by this statement
Application.Run(new Form1());

I can let shown any property of the Form2 by calling
MessageBox.Show(Form2.PROPERTY.ToString()); within the Main() method.

But what to do, if I want have shown this property from Form1 after clicking a button.


private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(???????.Form2 .PROPERTY.ToString());
}


Thank you

petes1234
February 18th, 2007, 08:36 AM
In this situation, Form2 is not in Form1's scope, so the last bit of code of yours will fail. One fix for this is to create a constructor in Form1 that contains a Form2 parameter and pass your Form2 object to Form1. I'm guessing that a better way is to use delegates.... but I'm just learning about these now myself. Let's see what the real guru's have to say.

orhor
February 18th, 2007, 08:40 AM
OK, thanks for tip. Ill take a look at delegates and will wait a little if someone else can say smtg to this. Thnx

petes1234
February 18th, 2007, 11:46 AM
Here are a few decent article on delegates:
.NET Delegates: A C# Bedtime Story (http://www.sellsbrothers.com/writing/default.aspx?content=delegates.htm)
The C# Station Tutorial, Lesson 14: Introduction to Delegates and Events (http://www.csharp-station.com/Tutorials/Lesson14.aspx)
C# Help: Delegates and Events (http://www.csharphelp.com/archives4/archive678.html)

I have one piece of code that does seem to work like you desired, but isn't yet thread safe. In this example I have 3 forms, a parent form called ParentForm and two child forms called ChildFormA and ChildFormB. My goal is to be able to read ChildFormBTxtBx.Text from ChildFormB into a ChildFormATxtBx on ChildFormA at the press of a button called GetBsTextBoxBtn located on ChildFormA.

Some of ParentForm's code:

namespace FooProj6
{
public partial class ParentForm : Form
{
private void createChildFormsBtn_Click(object sender, EventArgs e)
{
ChildFormA myChildFormA = new ChildFormA();
ChildFormB myChildFormB = new ChildFormB();
myChildFormA.getTextEvent += new getTextFromOtherForm(myChildFormB.getTextBoxText);
myChildFormA.Show();
myChildFormB.Show();
}
}
public delegate string getTextFromOtherForm();
}


Some of ChildFormA's code:

namespace FooProj6
{
public partial class ChildFormA : Form
{
public event getTextFromOtherForm getTextEvent;


private void GetBsTextBoxBtn_Click(object sender, EventArgs e)
{
if (this.getTextEvent != null)
{
ChildFormATxtBx.Text = getTextEvent();
}
}
}
}


And finally, some of ChildFormB's code:

namespace FooProj6
{
public partial class ChildFormB : Form
{

public string getTextBoxText()
{
return ChildFormBTxtBx.Text;
}
}
}

orhor
February 18th, 2007, 12:03 PM
thank you,
Im not sure if it is what Im looking for.
What I need to know is the name of the scope the Forms are created in. (sorry if I use those words not right)

I need to know what is the complet path to the object Form1.

Is it AppNamespaceName.Program.Form1(which is not) or what? Thats what I cant find.

Thank You

petes1234
February 18th, 2007, 01:49 PM
Form1 is the class name. Since the main app form is created without an object name (I think this may be called creating it anonymously) with the code Application.Run(new Form1());I'm not sure if the main form object has a name. When I've tried to get info from the main form in the past, I've often passed the main form object as a parameter to the subform in an overloaded subform constructor:

// in the main form I create the subform like so. "this" refers to the main form.
SubForm mySubForm = new SubForm(this);


// subForm code:
// variable to hold the parent form object in the subform
private Form1 myParentForm;

// the subform has a constructor overload that takes the main form as a parameter:
public SubForm(Form1 parentForm)
{
InitializeComponent();
myParentForm = parentForm;
}

How is your app set up? Do you have a main form (Form1 or whatever) that then creates and displays subforms? Which form needs info from which other form? What info do you need and where do you need it? I'll bet if you spell this out here as clearly as possible, you'll find someone who has solved your problem before and can lead you in the right direction. Good luck.

/Pete

orhor
February 18th, 2007, 02:55 PM
even if I created a form and then passed to a method

Form1 form1 = new Form1();
Application.Run(form1);

that means the object is a named instance, I dont know how to adress it instead.

OK, lets have a scenario:
I need my app to start invisible, only a notifyIcon should be visible.
I know I should do it with ApplicationContext. OK. I created my class myAppContext, I created an instance wich I passed to Application.Run().
Because I dont know any way to hve a notifyIcon 'standalone' - I think it has to be placed on some form I create a simple form In the mujAppContext constructor. this form has just the notifyicon wich is visible after running an app, the second form should be created dynamicly in runtime. this is the main form of my app, and when user closes it I need to .......


Sorry, now I have an idea, wouldnt be better to make the first form the main one and make it just hide instead of closing? How?

petes1234
February 18th, 2007, 03:39 PM
I did a search on the forum and found a great thread on making main forms invisible. You can find it here. Another good thread is this one.

orhor
February 18th, 2007, 05:25 PM
oh thank you, now it works OK.

petes1234
February 18th, 2007, 05:59 PM
When/if you get a chance, please show us a snip of your code that now works. Thanks.