473,396 Members | 2,033 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,396 software developers and data experts.

accesing Form1:controls from other class

Hello
I have Form1 : System.Windows.Forms.Form and some public controls in it:
public System.Windows.Forms.ComboBox comboBox1;
public System.Windows.Forms.ComboBox comboBox2;

in the same project i have some other files with some otherclasses.
Can i access any of theese controls from that other classes ?
How ?
Thanx

Nov 15 '05 #1
5 7087
If you make the controls public, yes. But the other classes would need a
reference to Form1 which you can give to the other classes when creating
them (as a parameter in the constructor).

However, a better way would be to make accessible methods or properties in
Form1 which could update the controls. The other classes would then call
these methods or properties to update the controls instead of handling the
controls directly (better OOP practice to let Form1 handle it's own
controls).
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #2
If you make the controls public, yes. But the other classes would need
a reference to Form1 which you can give to the other classes when
creating them (as a parameter in the constructor).

However, a better way would be to make accessible methods or properties
in Form1 which could update the controls. The other classes would then
call these methods or properties to update the controls instead of
handling the controls directly (better OOP practice to let Form1 handle
it's own controls).


ok, but after that how do i have acces to Form1 properties/methods ?
i tried:
mynamespace.Form1.???
where should i seek Form1 properties/method ?

Thanx

Nov 15 '05 #3
Hi,

If you are sure that will create only one instance of Form1 then you can
keep a reference to the instance on a static variable:

class Form1{
public static Form1 Instance;

public Form1()
{
Instance = this;
}
}

Then you can use F.Instance from anywhere in your application without need
to pass a reference.

Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

<us**@domain.invalid> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If you make the controls public, yes. But the other classes would need
a reference to Form1 which you can give to the other classes when
creating them (as a parameter in the constructor).

However, a better way would be to make accessible methods or properties
in Form1 which could update the controls. The other classes would then
call these methods or properties to update the controls instead of
handling the controls directly (better OOP practice to let Form1 handle
it's own controls).


ok, but after that how do i have acces to Form1 properties/methods ?
i tried:
mynamespace.Form1.???
where should i seek Form1 properties/method ?

Thanx

Nov 15 '05 #4
You need to pass a reference to Form1 to your class when creating it.

public class Form1 : System.Windows.Forms.Form
{
private Class1;
public ComboBox ComboBox1;

public string ComboText
{
get
{
return ComboBox1.Text;
}
set
{
if(value.Length <= 15)
ComboBox1.Text = value;
}
}

public Form1()
{
Class1 = new Class1(this);
}

public void ChangeValues()
{
ComboBox1.Text = "Text Sample 2";
}
}

public class Class1
{
private Parent;

public Class1(Form1 f)
{
Parent = f;
}

private void SomeMethod()
{
Parent.ComboBox1.Text = "Text Sample 1";
Parent.ChangeValues();
Parent.ComboText = "Text Sample 3";
}
}

Happy coding!
Morten

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #5
<us**@domain.invalid> wrote:
ok, but after that how do i have acces to Form1 properties/methods ?
i tried:
mynamespace.Form1.???
where should i seek Form1 properties/method ?


See http://www.yoda.arachsys.com/csharp/...rms.parameters

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

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

Similar topics

0
by: Matthias Lohrer | last post by:
Hi, I'm playing around with the possibilities of Page.ParseControl. Thanks to Kirk Allen Evans I got my example running (Posting Juli, 17, "Re: Generating ASP.NET-Controls with XSLT...
6
by: Nathan Sokalski | last post by:
I am trying to dynamically add controls to my page, but am having trouble with controls such as buttons. I have been able to add simple controls such as Label controls, because they can be placed...
2
by: c676228 | last post by:
Hi, I created a group of user controls in an aspx form. which is as follow: <body> <form id="Form1" method="post" runat="server"> <SUBWAY:PEOPLEINFO id="Peopleinfo0" runat="server"...
2
by: kaosyeti | last post by:
hey... i have a form with a cmd button that opens a 2nd form with acdialog as the windowmode. upon opening, if certain fields are filled, i would like to pull those values into some of the...
2
by: R.A.M. | last post by:
Hello, I am learning ASP.NET. On .aspx page I have a data-list with text-box that I would like to read during line modification. <asp:DataList ... OnEditCommand="Sample_Edit" > ......
4
by: Chris | last post by:
I have some code which adds controls to a placeholder. The button and label however always appears above the dynamically added content when the actual tags are below. Why is this. I want them to...
1
by: Suma | last post by:
Hi, i have a problem with the controls .i have to validate Form1 Radio button option is checked or Not in the Form2. even after checking the radio button also in FOrm2 it is always taking as...
1
by: bg_ie | last post by:
Hi, I have my own UserSettings class that I wish to include within my windows application. To include it at the moment, I do something like this - namespace WindowsApplication1 { static...
2
by: Tanzen | last post by:
Here's what I'm trying to do in vb.net 2005. I have a public declared method that sets the value of a control on a form, but that form could change, so I don't want to code its actual name. I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.