Access a control on a different Form

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
LVL 4
James AtkinSenior Principle Software EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Svetlin_PanayotovCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MOH_RCommented:
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
MOH_RCommented:
my last sample was
Form1::btn1 -> Text = S"Test";
this is incorrect.    Correct is :
Form2::btn1 -> Text = S"Test";
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

James AtkinSenior Principle Software EngineerAuthor Commented:
Sorry for the delay...

Both these methods appear to work correctly - are there thoughts on which one should normally be used?
0
Svetlin_PanayotovCommented:
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
James AtkinSenior Principle Software EngineerAuthor Commented:
Fair comment...

Thanks to you both for your input...

James
0
Svetlin_PanayotovCommented:
You're welcome :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual C++.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.