473,320 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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...

Mar 20 '07 #1
7 1686
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]
- mv*@spam.guard.caspershouse.com

"forest demon" <me********@gmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
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...

Mar 20 '07 #2
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";
}
}

On Mar 20, 9:52 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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]
- m...@spam.guard.caspershouse.com

"forest demon" <mete.ha...@gmail.comwrote in message

news:11**********************@y80g2000hsf.googlegr oups.com...
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 -

Mar 20 '07 #3
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" <me********@gmail.comha scritto nel messaggio
news:11**********************@l75g2000hse.googlegr oups.com...
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";
}
}

On Mar 20, 9:52 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
> 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]
- m...@spam.guard.caspershouse.com

"forest demon" <mete.ha...@gmail.comwrote in message

news:11**********************@y80g2000hsf.googleg roups.com...
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 -


Mar 20 '07 #4
VJ
so what is not working in this? You cant manipulate controls? Have you made
them public ?

VJ

"forest demon" <me********@gmail.comwrote in message
news:11**********************@l75g2000hse.googlegr oups.com...
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";
}
}

On Mar 20, 9:52 am, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
> 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]
- m...@spam.guard.caspershouse.com

"forest demon" <mete.ha...@gmail.comwrote in message

news:11**********************@y80g2000hsf.googleg roups.com...
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 -


Mar 20 '07 #5
forest demon <me********@gmail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 20 '07 #6
On Mar 20, 12:51 pm, Jon Skeet [C# MVP] <s...@pobox.comwrote:
forest demon <mete.ha...@gmail.comwrote:
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.

--
Jon Skeet - <s...@pobox.com>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Thanks everyone. Jon, that seems to work fine, but my initial problem
was an accessability/visibility issue to the controls(thanks VJ).

cheers all...

Mar 20 '07 #7
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
On Mar 20, 11:25 am, "forest demon" <mete.ha...@gmail.comwrote:
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...

Mar 22 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jay Douglas | last post by:
Hello, I am needing to pass a class object (this) by reference to a method in a different class. When I do the following code I get the error (Cannot pass '<this>' as a ref or out argument because...
1
by: James Dean | last post by:
I am trying to add controls(labels) to the main form from another class without having the main form as a base class......how do i do this? *** Sent via Developersdex...
7
by: Doug Thews | last post by:
I'm writing some sample threading code, and have already got many examples working that look a lot similar to the ones mentioned here. What I'm seeing is that most people put their method that...
28
by: kfrost | last post by:
I know this is probably simple but I have a C# form and the class for the form is called sbaSynch. I have a textbox name txtServerName. I'm creating a class to manipulate XML functions so I...
3
by: Jane-Wolf | last post by:
Hi, I have this function and use it for get id of my user company: public string get_company_from_cookie() { string cookieName = FormsAuthentication.FormsCookieName; HttpCookie authCookie =...
5
by: cha | last post by:
Hi there, I wonder what the best way would be to extract some basic functions into a separate class/namespace and make them accessible for a ASP.NET-Page (aspx) and a NET-WebService (asmx). Both...
2
by: EstroJen | last post by:
Hey folks, I'm trying to reference a control on a main form from a separate class. For simplicity, i'll show you a small example snippet. Everything compiles fine, but nothing actually...
4
by: MikeJ | last post by:
how can i reference a form from a class processing recordset to update the form.... im still new MJ
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.