Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

c# global object

HI,

I create an object from form1, how can I refer it from form2?
Thanks,
JT
0
jtran007
Asked:
jtran007
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
Depending on what it is you are trying to achieve, you would probably just be better of passing the object to the second form in its constructor.
0
 
it_saigeDeveloperCommented:
As long as the object is declared publically you could use:

form1.objectname in form2.

This is not in anyway the recommended way of accomplishing this.  Carls suggestion is more accurate from a programming standpoint.

-saige-
0
 
jtran007Author Commented:
Hi,

Thanks for your suggestion. Also I have a minor to ask. In the form1, I have define  
a textbox, right after the Initialization, I try copy the content of object to textbox.txt but
the compiler complains that textbox is not defined. I don't understand. Please help.
Thanks,
JT
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
Carl TawnSystems and Integration DeveloperCommented:
Right after initialization where? As long as it is after the call to InitializeComponent() it should be ok.
0
 
käµfm³d 👽Commented:
0
 
jtran007Author Commented:
Hi,

In the maiform, right after initialization.

Regards,
JT
0
 
jtran007Author Commented:
Hi,

In fact, after InitializeComponent();

Regards,
JT
0
 
Carl TawnSystems and Integration DeveloperCommented:
Can you post the code in question?
0
 
jtran007Author Commented:
Hi,

In the form:

 static void Setup()
{
   this.textbox1.Text = "testing";   <--- compiler complains
}
 public Initialization()
        {
            InitializeComponent();

            Setup();
         }


Regards,
JT
0
 
Carl TawnSystems and Integration DeveloperCommented:
It will. The TextBox is an instance member of the Form, you cannot access it from a static method.
0
 
jtran007Author Commented:
HI,
Sorry I click the wrong button.
I want to credit this point to Carl.

Regards,
JT
0
 
jtran007Author Commented:
Thanks,
JT
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now