[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Access a control on a different Form

Posted on 2004-11-11
7
Medium Priority
?
168 Views
Last Modified: 2010-04-24
Hi,

I have an application that has a Database control on one form.
When a connection is attempted, this is done by loading a new form and accessing the Database connection control on the Main form.
What is the simplest way to achieve this?
I have tried several ways, and even with the control marked as public, I get 'Undeclared Identifier'.
There is obviously something missing, but I can't see it!!!

Alternatively, is it possible/sensible to have an object Global to the application - if so, where should it reside?

Any help would be appreciated...

James
0
Comment
Question by:James Atkin
  • 3
  • 2
  • 2
7 Comments
 
LVL 6

Accepted Solution

by:
Svetlin_Panayotov earned 1000 total points
ID: 12554021
Well - I'll try to make the thnigs as simple as possible.
Let's say you have two forms - MainFrm and ChildFrm and you're trying to access a DataSet on MainFrm from ChildFrm:
In your ChildFrm add as public member
 System::Data::DataSet* pChildDataSet;
In your ChildFrm constructor add
 pChildDataSet = NULL;
In your MainFrm when showing ChildFrm the code should look like:
 ChildFrm* newFrm = new ChildFrm();
 newFrm->pChildDataSet = this->MainDataSet;
 newFrm->Show();

And when trying to access pChildDataSet - check if it's not NULL.

There's also alternate - and more elegant approach - to have Get/Set methods in your child form and keep it's DataSet private.

Svetlin
0
 
LVL 1

Assisted Solution

by:MOH_R
MOH_R earned 1000 total points
ID: 12554312
suppose you have form1.h file & form2.h file and you want to use any control of form2.h in form1.h.
I encountered with this problem several times in my projects.

solution :
1) define any control you want as static member.
static Button *btn1 = 0; // static member should be initialized.
2) in form1.h just before the statement #include <form1.h>
    add #include <form2.h>
now, in form1.h use any static member:
    Form1::btn1 -> Text = S"Test";

GOOD LUCK.
0
 
LVL 1

Expert Comment

by:MOH_R
ID: 12554364
my last sample was
Form1::btn1 -> Text = S"Test";
this is incorrect.    Correct is :
Form2::btn1 -> Text = S"Test";
0
Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

 
LVL 4

Author Comment

by:James Atkin
ID: 12591780
Sorry for the delay...

Both these methods appear to work correctly - are there thoughts on which one should normally be used?
0
 
LVL 6

Expert Comment

by:Svetlin_Panayotov
ID: 12591967
I personally prefer not to use static members - but both are correct and working fine. So it's up to you to decide which to use - I doesn't matter.

Svetlin
0
 
LVL 4

Author Comment

by:James Atkin
ID: 12591987
Fair comment...

Thanks to you both for your input...

James
0
 
LVL 6

Expert Comment

by:Svetlin_Panayotov
ID: 12592431
You're welcome :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month17 days, 23 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question