473,396 Members | 2,036 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.

Referring to controls on other forms

Hi,

Apologies if this is a stupid question - I'm a C# newbie... :-)

I have a Windows app with two form: frmConfigReg and frmNewApp. frmConfigReg
is the startup form. It has several controls, including a GroupBox called
grpApplications which itself contains a ComboBox called cmbApps and a button
called cmdNewApp. When cmdNewApp is clicked, it loads frmNewApp which
contains a text box where users enter the name of a new application to
configure.

However, I need to validate that the contents of this text box do not
already exist as any of the options in cmbApps on the main form.

I've tried using the following code:

Form frmConfigReg = new frmConfigReg();

but that loads a new instance of frmConfigReg, not unsurprisingly.

Can anyone please tell me how to reference controls and their properties on
another open form from within a C# windows application?

Any assistance gratefully received.

Best regards,

Mark Rae
Nov 15 '05 #1
21 2723
Hi Mark

If you are doing this validation in the main form of your application ,
which is frmConfigReg, you don’t have to create a new instance in order to
access the ComboBox cmbApps . you simply use the this pointer .

So you write

this. cmbApps.”the name of the proparty you want to set or get”


--
Mohamed Mossad
Microsoft GTSC Developer support for Middle East
"Mark Rae" <ma**@markrae.co.uk> wrote in message
news:O0**************@TK2MSFTNGP11.phx.gbl...
Hi,

Apologies if this is a stupid question - I'm a C# newbie... :-)

I have a Windows app with two form: frmConfigReg and frmNewApp. frmConfigReg is the startup form. It has several controls, including a GroupBox called
grpApplications which itself contains a ComboBox called cmbApps and a button called cmdNewApp. When cmdNewApp is clicked, it loads frmNewApp which
contains a text box where users enter the name of a new application to
configure.

However, I need to validate that the contents of this text box do not
already exist as any of the options in cmbApps on the main form.

I've tried using the following code:

Form frmConfigReg = new frmConfigReg();

but that loads a new instance of frmConfigReg, not unsurprisingly.

Can anyone please tell me how to reference controls and their properties on another open form from within a C# windows application?

Any assistance gratefully received.

Best regards,

Mark Rae

Nov 15 '05 #2
"Mohamoss" <Mo************@egdsc.Microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

Mohammed
If you are doing this validation in the main form of your application ,
which is frmConfigReg, you don't have to create a new instance in order to
access the ComboBox cmbApps . you simply use the this pointer .

So you write

this. cmbApps."the name of the proparty you want to set or get"


Yes, but I'm not - I need to to the validation on the popped up form.
Basically, I just need to know how to refer to a control on another open
form.

Mark
Nov 15 '05 #3
n!
> Can anyone please tell me how to reference controls and their properties
on
another open form from within a C# windows application?


You create an instance of your frmNewApp class inside your main frmConfigReg
form yes? You can supply a reference to the parent during construction:

ie. frmNewApp = new frmNewApp( this );

Where:

public class frmNewApp : System.Windows.Forms.Form
{
private frmConfigReg parent;

private frmNewApp() // Private constructor to prevent
incorrect instantiation
{
}

public frmNewApp( frmConfigReg parent )
{
this.parent = parent;
}
}

Which allows you to access to the parent object directly when validating
your contents, you can add any necessary support methods to the parent form.
There are also a number of other options such as supplying a 'validation'
delegate or interface instead of a class, but the above should be good for a
start :)

n!
Nov 15 '05 #4
"n!" <nf********@nomailplease.com> wrote in message
news:O3**************@TK2MSFTNGP12.phx.gbl...
You create an instance of your frmNewApp class inside your main frmConfigReg form yes? You can supply a reference to the parent during construction:

ie. frmNewApp = new frmNewApp( this );


That line gives the following error:

'ConfigReg.frmNewApp' denotes a 'class' where a 'variable' was expected.

So I changed it to:

frmNewApp = new frmNewApp( this );

which, in turn, gives the following error:

No overload for method 'frmNewApp' takes '1' arguments
Nov 15 '05 #5
"Mark Rae" <ma**@markrae.co.uk> wrote in message
news:OG**************@TK2MSFTNGP09.phx.gbl...
So I changed it to:

frmNewApp = new frmNewApp( this );


Sorry, that should have been:

Form frmNewApp = new frmNewApp( this );

Apologies... :-)
Nov 15 '05 #6
n!
> So I changed it to:

frmNewApp = new frmNewApp( this );

which, in turn, gives the following error:
No overload for method 'frmNewApp' takes '1' arguments


You *did* add a constructor that takes a reference to your parent form, as I
showed? :)

n!
Nov 15 '05 #7
Hi Mark,

Why don't you create a public ArrayList in frmNewApp form and fill it with cmbApps values. This has to be done before showing the frmNewApp form. Then you simply validate the textbox value with values in ArrayList.
Nov 15 '05 #8
"n!" <nf********@nomailplease.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
So I changed it to:

frmNewApp = new frmNewApp( this );

which, in turn, gives the following error:
No overload for method 'frmNewApp' takes '1' arguments
You *did* add a constructor that takes a reference to your parent form, as

I showed? :)


Yep - I pretty much copied and pasted your code in directly...
Nov 15 '05 #9
"Dinesh Priyankara" <di****@dineshpriyankara.com> wrote in message
news:50**********************************@microsof t.com...

Dinesh.
Why don't you create a public ArrayList in frmNewApp form and fill it with cmbApps values.This has to be done before showing the frmNewApp form. Then you simply validate the textboxvalue with values in ArrayList.


Because I don't want to. I simply want to know how to reference the
properties of controls on one open form in a C# Windows application from
another open form in the same C# Windows application - no more, no less.

I can't believe this is proving to be so tricky...

Mark
Nov 15 '05 #10
n!
> > > frmNewApp = new frmNewApp( this );

Oh I see, I was supposed to have typed:

frmNewApp child = new frmNewApp( this );

sorry.
Yep - I pretty much copied and pasted your code in directly...


it compiles on my machine fine as I typed it.

n!
Nov 15 '05 #11
n!
In case you want it, this is the code from my machine:

namespace temptest
{
using System;
using System.Windows.Forms;

/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmConfigReg : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public frmConfigReg()
{
// Required for Windows Form Designer support
InitializeComponent();

// Create child, passing a reference to ourself....
frmNewApp child = new frmNewApp( this );
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmConfigReg());
}
}

public class frmNewApp : System.Windows.Forms.Form
{
private frmConfigReg parent;

/// <summary>
/// Private constructor to prevent incorrect instantiation.
/// </summary>
private frmNewApp()
{
}

public frmNewApp( frmConfigReg parent )
{
this.parent = parent;
}
}
}
Nov 15 '05 #12
Hi Mark

Ok you can do this :

1- Make the ComboBox cmbApps public instead of Private member of the
frmConfigReg Form.

2- Include a private member of type frmConfigReg in the second form you
have , I don’t remember its name so lets name it F2 , and lets say name
this member “themain”

private Form1 themain;

3- In the constructor of F2 , change it to take a parameter of type
frmConfigReg From … then assign this input parameter to the themain member
that you have now

public F2(Form1 caller)

{

//

themain =caller;

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after
InitializeComponent call

//

}

4- Back to the first form you are using, when you instantiate
frmConfigReg pass to it (this) which will be used as a reference to the
parent.

F2 second = new Form2(this);

second.Show();

5- Then you can use the mainform object in the second form F2

As F2.mainform. cmbApps.”what ever”

If this is not so clear pls confirm .

Also , notice this is not the best practice since you are making some kind
of cycle in the call stack

The best practice in such scenario is to use MDI

--
Mohamed Mossad
Microsoft GTSC Developer support for Middle East

"Mark Rae" <ma**@markrae.co.uk> wrote in message
news:eI*************@TK2MSFTNGP11.phx.gbl...
"Mohamoss" <Mo************@egdsc.Microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

Mohammed
If you are doing this validation in the main form of your application , which is frmConfigReg, you don't have to create a new instance in order to access the ComboBox cmbApps . you simply use the this pointer .

So you write

this. cmbApps."the name of the proparty you want to set or get"


Yes, but I'm not - I need to to the validation on the popped up form.
Basically, I just need to know how to refer to a control on another open
form.

Mark

Nov 15 '05 #13
"n!" <nf********@nomailplease.com> wrote in message
news:uG**************@TK2MSFTNGP10.phx.gbl...
In case you want it, this is the code from my machine:


Thanks.

Let's say, for the sake of argument, that we have a C# Windows application
with two forms: form1 and form2. Neither of these forms is the child or
parent of the other, and both have been opened by the app when it is
launched.

form1 has a button on it called button1

form2 has a combobox on it called comboBox1

The purpose of button1 on form1 is to add a new option to comboBox1 on form2

Can anyone please tell me how to do this?
Nov 15 '05 #14
"Mohamoss" <Mo************@egdsc.Microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
If this is not so clear pls confirm . The best practice in such scenario is to use MDI


Let's say, for the sake of argument, that we have a C# Windows application
with two forms: form1 and form2. Neither of these forms is the child or
parent of the other, and both have been opened by the app when it is
launched. It's not an MDI app, just a regular common or garden app with two
forms.

form1 has a button on it called button1

form2 has a combobox on it called comboBox1

The purpose of button1 on form1 is to add a new option to comboBox1 on form2

Can anyone please tell me how to do this?
Nov 15 '05 #15
Dear Mark

If you load frmNewApp form with below code in cmdNewApp's click event

frmNewApp form2 = new frmNewApp()
form2.ShowDialog(this)

you can access controls in frmConfigReg form like below

//if you know the index i
ComboBox cb = (ComboBox) ((frmConfigReg )Owner).Controls[1]

//O

ControlCollection cc = (ControlCollection) ((frmConfigReg)Owner).Controls
foreach(Control c in cc

if (c.Name=="cmbApps"

//get values and validat

hope this is what you want
Nov 15 '05 #16
n!
> Let's say, for the sake of argument, that we have a C# Windows application
with two forms: form1 and form2. Neither of these forms is the child or
parent of the other, and both have been opened by the app when it is
launched.

form1 has a button on it called button1

form2 has a combobox on it called comboBox1

The purpose of button1 on form1 is to add a new option to comboBox1 on form2
Can anyone please tell me how to do this?


There are many ways to achieve this, the typical way is for the main form to
respond and manage the communication between the two child forms, this
provides a more flexible architecture as you can then alter the
functionality of the form without modifying the controls (making them
reusable).

If you *really* wanted the button to communicate directly to the combo box,
then you could expose a property on each child form similar to:

[In the button1 class]

using System.Windows.Forms;

class MyButton : Button
{
private ComboBox comboTarget = null;

public ComboBox Target
{
get
{
return comboTarget;
}
set
{
comboTarget = value;
}
}

protected override void OnClick( System.EventArgs e )
{
if ( null != comboTarget )
{
// Add new options to combo box...
}

base.OnClick( e );
}
}

In the parent form, once both button and combo box have been created, you
provide your button object with the target combo box.

n!
Nov 15 '05 #17
"n!" <nf********@nomailplease.com> wrote in message
news:u2**************@TK2MSFTNGP10.phx.gbl...
There are many ways to achieve this, the typical way is for the main form to respond and manage the communication between the two child forms, this
provides a more flexible architecture as you can then alter the
functionality of the form without modifying the controls (making them
reusable).
That's all very well, and no doubt very elegant. However, in this case,
there is no main form, nor are there any child form. There is one Windows
application with two forms, neither the parent nor the child of the other.
If you *really* wanted the button to communicate directly to the combo box, then you could expose a property on each child form similar to:


Is there *really* no easier way of achieving what seems, to me at least, to
be an incredibly simple task...?
Nov 15 '05 #18
n!
> That's all very well, and no doubt very elegant. However, in this case,
there is no main form, nor are there any child form. There is one Windows
application with two forms, neither the parent nor the child of the other.
Then replace 'MainForm' with 'MainApplication' or whatever the class is that
creates these two forms :) The point being you need some sort of 'root' code
in order to create your forms, that is typically where the event handling
and communication is managed. Otherwise the forms become too specialized for
a specific task and create more work later on.
If you *really* wanted the button to communicate directly to the combo

box,
then you could expose a property on each child form similar to:


Is there *really* no easier way of achieving what seems, to me at least,

to be an incredibly simple task...?


The above *is* incredibly simple. If you mean 'can I just do this with no
programming whatsoever' then I'm afraid not, you need to specify the
relationships and how\what they communicate so a bit of code is required.

In what way would you expect to be able to do the above, in less code?
Perhaps I'm looking at the problem from the wrong angle? :)

n!
Nov 15 '05 #19
When you launch form1, pass the reference to the parent to the constructor
of form1.

public class Mother
{
private form2 f2;

public Mother()
{
form1 f1 = new form1(this);
f2 = new form();
}

public void PassAlong(string option)
{
f2.AddNewOption(option);
}
}

public class form1
{
private Mother parent;

public form1(Mother caller)
{
parent = caller;
}

button1_Click(...)
{
parent.PassAlong("somenewvalue");
}
}

public class form2
{
public form2()
{
{

public void AddNewOption(string newOption)
{
comboBox1.Items.Add(newOption);
}
}

Parent holds reference to form2, and form1 holds a reference to the parent.
I don't particularly like all the public methods though, but this will do
what you need without exposing the controls themselves.
--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
For a laugh, try web browsing with Opera's User Mode and Nostalgia enabled
Nov 15 '05 #20
"Dinesh Priyankara" <di****@dineshpriyankara.com> wrote in message
news:E3**********************************@microsof t.com...

Dinesh,
If you load frmNewApp form with below code in cmdNewApp's click event;

frmNewApp form2 = new frmNewApp();
form2.ShowDialog(this);

you can access controls in frmConfigReg form like below.

//if you know the index id
ComboBox cb = (ComboBox) ((frmConfigReg )Owner).Controls[1];

//OR

ControlCollection cc = (ControlCollection) ((frmConfigReg)Owner).Controls;
foreach(Control c in cc)
{
if (c.Name=="cmbApps")
{
//get values and validate
}
}

hope this is what you want


That has certainly worked - thanks very much. I'm quite surprised, though,
that it is necessary to reference controls in the ControlCollection by their
index rather than their name - can the FindControl method be used here...?

Best,

Mark
Nov 15 '05 #21
Hi Mark,

One simple way to accomplish this is to create a constructor for
the frmNewApp form that accepts as a parameter a reference to the
cmbApps object.

Add an internal ComboBox that has a private scope into frmNewApp
(objComboBoxNewToThisForm)
frmNewApp (ref ComboBox objComboReferencedFromMainForm):this()
{
this.objComboBoxNewToThisForm = objComboReferencedFromMainForm;
}

Now you can work with the objComboBoxNewToThisForm as normal -- it
references the ComboBox passed in objReferencedFromMainForm.

When creating a new form in your button click event -- open the
constructor indicated above rather than just new frmNewApp().

Hope this helps.
--
~~~~~~~~~~~~~
Tommie Carter
--
"Mark Rae" <ma**@markrae.co.uk> wrote in message news:<O0**************@TK2MSFTNGP11.phx.gbl>...
Hi,

Apologies if this is a stupid question - I'm a C# newbie... :-)

I have a Windows app with two form: frmConfigReg and frmNewApp. frmConfigReg
is the startup form. It has several controls, including a GroupBox called
grpApplications which itself contains a ComboBox called cmbApps and a button
called cmdNewApp. When cmdNewApp is clicked, it loads frmNewApp which
contains a text box where users enter the name of a new application to
configure.

However, I need to validate that the contents of this text box do not
already exist as any of the options in cmbApps on the main form.

I've tried using the following code:

Form frmConfigReg = new frmConfigReg();

but that loads a new instance of frmConfigReg, not unsurprisingly.

Can anyone please tell me how to reference controls and their properties on
another open form from within a C# windows application?

Any assistance gratefully received.

Best regards,

Mark Rae

Nov 15 '05 #22

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

Similar topics

5
by: HB | last post by:
Can I refer to a controls property using a string much like I can a Control. I want to simplify the following code: forms!frmname!ctrlnm.visible = true forms!frmname!ctrlnm.enabled= true...
4
by: MLH | last post by:
Somehow, I seem to be able to refer to combo-box controls in 2 distinctly different ways. A combo-box on an Access 97 form named TagCountyChooserBox seems can be referenced these ways... ...
3
by: Chris | last post by:
Hi, I'm trying to append text from another class to a generic richTextBox that I've added to a Windows form. I can't seem to figure out how to expose the richTextBox to append text to it. ...
11
by: VJ | last post by:
I am looking to have 2 Tab Controls on a Windows Forms.. One with Tabs on Top and another with Tabs on the side... I want them both to occupy the entire area of the form, but I need the Tab...
9
by: Dimsion | last post by:
Hi, How do i expose all my forms and it controls to other form in the project? I want to be able to add a form and some control on it, this then be available to all other forms. form1 click...
2
by: Sugan | last post by:
Hi all, I'm converting a VB 6 exe project to a VB 2005 project. One main functionality in VB 6 that gives errors in VB 2005 project is as follows: In VB6, i have a initial form called...
8
by: Ryan | last post by:
Ok.. I have a form with lots of stuff on it; a tool strip panel, menu strip, data binding elements (dataset, binding source, table adapter), tab control with 7 tab pages, each page contains a...
6
NeoPa
by: NeoPa | last post by:
Introduction The first thing to understand about Sub-Forms is that, to add a form onto another form takes a special Subform control. This Subform control acts as a container for the form that you...
2
by: csolomon | last post by:
Hello: I would like to use information on one form to populate another form. There are 2 controls I would like to transfer; a list box control and a text box control. both are unbound controls...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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.