参考其他形式的控制 [英] Referring to controls on other forms

查看:57
本文介绍了参考其他形式的控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




道歉,如果这是一个愚蠢的问题 - 我是C#新手...... :-)


我有一个Windows应用程序有两种形式:frmConfigReg和frmNewApp。 frmConfigReg

是启动形式。它有几个控件,包括一个名为

grpApplications的GroupBox,它本身包含一个名为cmbApps的ComboBox和一个名为cmdNewApp的按钮

。单击cmdNewApp时,它会加载frmNewApp,其中

包含一个文本框,用户可以在其中输入新应用程序的名称

configure。


但是,我需要验证此文本框的内容是否已经作为主窗体上cmbApps中的任何选项而存在。


我尝试过使用以下代码:


表单frmConfigReg = new frmConfigReg();


但是会加载一个新实例frmConfigReg,并不奇怪。


任何人都可以告诉我如何在C#windows应用程序中引用控件及其属性

另一个打开的表单?


非常感谢您的任何帮助。


致敬,

Mark Rae

解决方案

您好Mark


如果您在申请表的主要表格中进行此验证,

这是frmConfigReg,你不是哈我要创建一个新的实例,以便
访问ComboBox cmbApps。你只需使用这个指针。


所以你写这个是b $ b。 cmbApps.?你要设置或获得的proparty的名称?


-

Mohamed Mossad

Microsoft GTSC开发人员对中东的支持

" Mark Rae" <毫安** @ markrae.co.uk>在消息中写道

新闻:O0 ************** @ TK2MSFTNGP11.phx.gbl ...

如果这是一个愚蠢的问题,请道歉 - 我是C#newbie ... :-)
我有一个Windows应用程序有两种形式:frmConfigReg和frmNewApp。
frmConfigReg是启动表单。它有几个控件,包括一个名为
grpApplications的GroupBox,它本身包含一个名为cmbApps的ComboBox和一个名为cmdNewApp的
按钮。单击cmdNewApp时,它会加载frmNewApp,其中包含一个文本框,用户可以在其中输入要配置的新应用程序的名称。

但是,我需要验证内容这个文本框的内容并不像主表单上的cmbApps中的任何选项那样存在。

我尝试过使用以下代码:

表单frmConfigReg = new frmConfigReg();

但是加载了一个新的frmConfigReg实例,并不奇怪。

任何人都可以告诉我如何引用控件及其属性$ b在C#windows应用程序中的另一个打开表单上的$ b?

感谢所有的帮助。

致以最诚挚的问候,

Mark Rae



" Mohamoss" <莫************ @ egdsc.Microsoft.com>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP10.phx.gbl ...


Mohammed

如果您在应用程序的主要形式中进行此验证,
即frmConfigReg,您不必按顺序创建新实例
访问ComboBox cmbApps。你只需使用这个指针。

所以你写了这个

。 cmbApps。你要设置或获得的proparty的名称




是的,但我不是 - 我需要对popped进行验证表格。

基本上,我只需要知道如何在另一个开放的

表格中引用一个控件。


Mark


>谁能告诉我如何在C#windows应用程序中的

另一个打开的表单中引用控件及其属性



您在主frmConfigReg中创建frmNewApp类的实例

表单是吗?您可以在施工期间提供对父母的参考:


ie。 frmNewApp = new frmNewApp(this);


其中:


公共类frmNewApp:System.Windows.Forms.Form

{

private frmConfigReg parent;


private frmNewApp()//私有构造函数防止

不正确的实例化

{

}


public frmNewApp(frmConfigReg parent)

{

this.parent = parent;

}

}


允许您在验证时直接访问父对象

您的内容,您可以向父表单添加任何必要的支持方法。

还有许多其他选项,例如提供''验证''

委托或界面而不是一个类,但上面应该对

开始好:)


n!


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

解决方案

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



"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


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


这篇关于参考其他形式的控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆