Accessing form controls

  • Thread starter Thread starter M K
  • Start date Start date
M

M K

I have 2 classes. One where the form resides and I created another one for
all the database stuff.

after i get data from the db i want to be able to update the form. I have
the namespace of where the form code is but how do i get to the controls
themselves.

newbie in the class world.. still trying to figure it all out :)

Thanks for any help...
 
I have 2 classes. One where the form resides and I created another one
for
all the database stuff.

after i get data from the db i want to be able to update the form. I
have
the namespace of where the form code is but how do i get to the controls
themselves.

Well, the "cheesy" way is to simply change the member fields for the
controls so that their access is public instead of private.

But it would be much better to expose the aspects of those controls that
you want to allow access to as properties or in some cases methods.
Those, you make a part of the public API that the form class exposes,
allowing the form class complete control over what client's of the form
class can do, rather than handing over the actual instance to the controls
(which provides implementation details that only the form should know
about).

Pete
 
M K said:
I have 2 classes. One where the form resides and I created another one for
all the database stuff.

after i get data from the db i want to be able to update the form. I have
the namespace of where the form code is but how do i get to the controls
themselves.

newbie in the class world.. still trying to figure it all out :)

Thanks for any help...

The management class needs to have a reference to the Form!
As the management class is supposed to be only once in memory, I would make
a singelton out of it. This provides a static method which returns the
instance of the class.
Once the Form has the reference to the management class it could pass their
own reference to the manager over an property
management class code:
private Form1 _myform;
public Form1 MyForm
{
get
{
return _myform;
}
set
{
if(value!=_myform)
{
_myform = value;
}
}
}

Over this reference the managementclass cann then set public Properties or
call public methods with aproprtiat parameter, to modify the GUI.

If you are running the management class in an seperate thread from the GUI
you have to Invoke those methods.

Hope it helps,

All the best,

Martin
 
ouch.. all those technical terms I dont understand yet...
talk about trying to confuse me.. i thought putting the word 'newbie' would
be a dead give away..

i think the easiest way after tying to decipher your posts is to pass back
the SQLDataReader then I dont have to deciper :)

thanks
 
ok heres same situation...

SqlParameter parReturn = jobdb.AddJob(this); // call AddJob

----------------------------------------------------------

public SqlParameter AddJob(Form frmJobBank) <--- i want to be able to use
the form fields

{

code omitted

// need to be able to get to txtTitle field below as well as all the other
fields...

cmd.Parameters.Add("@JobTitle", SqlDbType.VarChar, 50).Value =
txtTitle.Text; }
 
There are a variety of ways around this issue. Here's the simplest
correct solution (there are simpler incorrect solutions):

class Form1
{
public string Title
{
get { return txtTitle.Text; }
}
}

Then:

SqlParameter parReturn = jobdb.AddJob(this);

where:

// Note: if you are going to use things specific to your form class,
// this should be documented by specifying the form class as the type
// for the parameter. Pass Form1 instead of Form (or whatever the
// class name for the form is)
public SqlParameter AddJob(Form1 frmJobBank)
{
cmd.Parameters.Add("@JobTitle", SqlDbType.VarChar, 50).Value =
frmJobBank.Title;
}
 
cool that worked. seems like a lot of coding for this. I have about 50
fields I need to pass back.

There are a variety of ways around this issue. Here's the simplest
correct solution (there are simpler incorrect solutions):

class Form1
{
public string Title
{
get { return txtTitle.Text; }
}
}

Then:

SqlParameter parReturn = jobdb.AddJob(this);

where:

// Note: if you are going to use things specific to your form class,
// this should be documented by specifying the form class as the type
// for the parameter. Pass Form1 instead of Form (or whatever the
// class name for the form is)
public SqlParameter AddJob(Form1 frmJobBank)
{
cmd.Parameters.Add("@JobTitle", SqlDbType.VarChar, 50).Value =
frmJobBank.Title;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top