How do you reference a Form that you pass to a separate class?

  • Thread starter Thread starter forest demon
  • Start date Start date
F

forest demon

all i want is to do is to pass a form reference to a separate class
and be able to manipulate properties/components/controls of said form.
this should be as simple as passing a TextBox, Container object or
something similar, to do the same, but no.

what am i missing, besides my mind?

thanks folks...
 
Without seeing how you pass it, it's hard to tell, but something isn't
right. A form is like any other object, as long as you pass the reference
to the code you want to access it (through a constructor, method, or
property), you can do so.
 
my main form (frmTest) being passed to method TryThis in a separate
class(Form2). something simple like this:

public partial class frmTest : Form
{
private void frmTest_Load(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.TryThis(frmTest);
}
}

public partial class Form2 : Form
{
public void TryThis(Form frm1)
{
//do something with frm1 here
frm1.textbox1.Text = "something";
}
}



Without seeing how you pass it, it's hard to tell, but something isn't
right. A form is like any other object, as long as you pass the reference
to the code you want to access it (through a constructor, method, or
property), you can do so.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




all i want is to do is to pass a form reference to a separate class
and be able to manipulate properties/components/controls of said form.
this should be as simple as passing a TextBox, Container object or
something similar, to do the same, but no.
what am i missing, besides my mind?
thanks folks...- Hide quoted text -

- Show quoted text -
 
so what is not working in this? You cant manipulate controls? Have you made
them public ?

VJ

forest demon said:
my main form (frmTest) being passed to method TryThis in a separate
class(Form2). something simple like this:

public partial class frmTest : Form
{
private void frmTest_Load(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.TryThis(frmTest);
}
}

public partial class Form2 : Form
{
public void TryThis(Form frm1)
{
//do something with frm1 here
frm1.textbox1.Text = "something";
}
}



Without seeing how you pass it, it's hard to tell, but something
isn't
right. A form is like any other object, as long as you pass the
reference
to the code you want to access it (through a constructor, method, or
property), you can do so.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




all i want is to do is to pass a form reference to a separate class
and be able to manipulate properties/components/controls of said form.
this should be as simple as passing a TextBox, Container object or
something similar, to do the same, but no.
what am i missing, besides my mind?
thanks folks...- Hide quoted text -

- Show quoted text -
 
Try this:

public partial class frmTest : Form
{
private void frmTest_Load(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.TryThis(this); // <- pass this istead of frmTest, since
frmTest is only a class, "this" represents the current form
}
}

public partial class Form2 : Form
{
public void TryThis(frmTest frm1) // <- accept frmTest as parameter
type
{
//do something with frm1 here
frm1.textbox1.Text = "something";
}
}


forest demon said:
my main form (frmTest) being passed to method TryThis in a separate
class(Form2). something simple like this:

public partial class frmTest : Form
{
private void frmTest_Load(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.TryThis(frmTest);
}
}

public partial class Form2 : Form
{
public void TryThis(Form frm1)
{
//do something with frm1 here
frm1.textbox1.Text = "something";
}
}



Without seeing how you pass it, it's hard to tell, but something
isn't
right. A form is like any other object, as long as you pass the
reference
to the code you want to access it (through a constructor, method, or
property), you can do so.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




all i want is to do is to pass a form reference to a separate class
and be able to manipulate properties/components/controls of said form.
this should be as simple as passing a TextBox, Container object or
something similar, to do the same, but no.
what am i missing, besides my mind?
thanks folks...- Hide quoted text -

- Show quoted text -
 
forest demon said:
my main form (frmTest) being passed to method TryThis in a separate
class(Form2). something simple like this:

public partial class frmTest : Form
{
private void frmTest_Load(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.TryThis(frmTest);
}
}

That won't compile, as you're using frmTest as if it were a variable,
but it's actually the type name.

If you did:

f2.TryThis(this);

it would help - except that...
public partial class Form2 : Form
{
public void TryThis(Form frm1)
{
//do something with frm1 here
frm1.textbox1.Text = "something";
}
}

Here you're declaring the parameter as just being of type "Form". Form
doesn't have a public property or field called "textbox1" so you'll get
a compilation error. If you want it to take a frmTest instead, you
should declare the parameter as such, eg
public void TryThis(ftmTest frm1)

Note that *nothing* about this is specific to forms.
 
That won't compile, as you're using frmTest as if it were a variable,
but it's actually the type name.

If you did:

f2.TryThis(this);

it would help - except that...


Here you're declaring the parameter as just being of type "Form". Form
doesn't have a public property or field called "textbox1" so you'll get
a compilation error. If you want it to take a frmTest instead, you
should declare the parameter as such, eg
public void TryThis(ftmTest frm1)

Note that *nothing* about this is specific to forms.

Thanks everyone. Jon, that seems to work fine, but my initial problem
was an accessability/visibility issue to the controls(thanks VJ).

cheers all...
 
would it help to use the
System.Windows.Forms.Control.ControlCollection ? that's the
collection of controls on the form, and I think you might have to use
it to manipulate the text box...

so instead of passing a parameter of type Form, pass the form's
ControlCollection
 

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