从不同的类/文件访问表单元素 [英] Access to form elements from a different class / file

查看:65
本文介绍了从不同的类/文件访问表单元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个名为form1的表单,其中有很多复选框

(它们形成一个8x6字段)。我想在一个单独的.cs

文件中创建一个类,它可以控制这些复选框(例如,

使它们全部取消选中)。 />

所以我现在为这个类创建了一个.cs文件,但我无法访问form1上的

checkBox元素。它们通过

方式位于相同的命名空间中。我需要做什么?我试图通过Form1.CheckBox1

和CheckBox1访问它。我没有使用继承,我需要吗?


提前谢谢。


PS:是的我是新手:)

解决方案

Form1.CheckBox1有什么问题? Form1是一个类还是一个对象?它应该是一个对象。


Eliyahu


" Tobias Froehlich" <再******* @ gmx.net>在消息中写道

news:hp ******************************** @ 4ax.com ...



我有一个名为form1的表单,其中有很多复选框
(它们形成一个8x6字段)。我想在一个单独的.cs
文件中创建一个类,它可以控制这些复选框(例如,使它们全部取消选中)。

所以我现在为这个类创建了一个.cs文件,但是我无法访问form1上的
checkBox元素。
方式它们都在同一个命名空间中。我需要做什么?我试图通过Form1.CheckBox1
和CheckBox1访问它。我没有使用继承,我需要吗?

提前致谢。

PS:是的我是新手:)



Form1是VS.NET自动创建的,我认为应该是

正确。


也许我会只需将它全部保存在一个文件中,然后用#region对它进行排序



Hi Tobias,

您需要做一些事情,首先您可以访问其他类的复选框

,您可以通过将它们声明为公开来实现,稍后您需要

将form1对象的引用传递给构造函数或

控制器类的方法,或者如果你只使用form1的一个实例,你可以

声明复选框为静态,然后你不必将

引用传递给对象。

说,我会建议你我认为更好的方式,我不会为
创建一个单独的类s,我会在Form1类中实现该功能

,我会创建一个数组(如果你需要在代码中添加/删除其中一些,则需要一个ArrayList)通过引用

复选框可以将它们作为一个组来处理,而不是按名称指定

。你可以创建UncheckAll的功能,这将是

如下所示:

public void SetCheckBoxStatus(bool checked)

{

foreach(复选框中的CheckBox复选框)

{

checkbox.Checked = checked;

}

}

希望这个帮助,


-

Ignacio Machin,

ignacio.machin AT dot.state.fl.us

Florida Department of Transportation

" Tobias Froehlich" <再******* @ gmx.net>在消息中写道

news:hp ******************************** @ 4ax.com ...



我有一个名为form1的表单,其中有很多复选框
(它们形成一个8x6字段)。我想在一个单独的.cs
文件中创建一个类,它可以控制这些复选框(例如,使它们全部取消选中)。

所以我现在为这个类创建了一个.cs文件,但是我无法访问form1上的
checkBox元素。
方式它们都在同一个命名空间中。我需要做什么?我试图通过Form1.CheckBox1
和CheckBox1访问它。我没有使用继承,我需要吗?

提前致谢。

PS:是的我是新手:)



Hi,

I have a form called form1 on which there are a lot of checkBoxes
(they form a 8x6 field). I''d like to create a class in a seperate .cs
file which gives some control over these checkBoxes (for example to
make them all unchecked).

So I now created a .cs file for this class, but I can''t access the
checkBox elements on form1. They are both in the same namespace by the
way. What do I need to do? I tried to access it via Form1.CheckBox1
and CheckBox1. I did not use inheritance, do i need to?

Thanks in advance.

PS: Yes i am a newbie :)

解决方案

What was the problem with Form1.CheckBox1? Is Form1 a class or an object? It
should be an object.

Eliyahu

"Tobias Froehlich" <re*******@gmx.net> wrote in message
news:hp********************************@4ax.com...

Hi,

I have a form called form1 on which there are a lot of checkBoxes
(they form a 8x6 field). I''d like to create a class in a seperate .cs
file which gives some control over these checkBoxes (for example to
make them all unchecked).

So I now created a .cs file for this class, but I can''t access the
checkBox elements on form1. They are both in the same namespace by the
way. What do I need to do? I tried to access it via Form1.CheckBox1
and CheckBox1. I did not use inheritance, do i need to?

Thanks in advance.

PS: Yes i am a newbie :)



Form1 is what VS.NET created automatically, I think that should be
correct.

Maybe I''ll just leave it all in one file and sort things up a little
with #region.


Hi Tobias,

You need to do some things, first you have make accesible the checkboxes to
the other class, you can do this by declaring them as public, later you need
to pass a reference for the form1 object to the constructor or a method of
the controller class, or if you use only one instance of form1 you can
declare the checkboxes as static and then you do not have to pass the
reference to the object.
Said that, I would suggest you what I think is a better way, I would not
create a separate class for this, I would implement that functionality
inside the Form1 class, I would create an array ( an ArrayList in case you
need to add/remove some of them in code ) with the reference to the
checkboxes to be able to deal with them as a group, instead of specifying
each one by name. You can create then functions as UncheckAll that would
looks like this:

public void SetCheckBoxStatus( bool checked)
{
foreach( CheckBox checkbox in checkboxarray )
{
checkbox.Checked = checked;
}
}
Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Tobias Froehlich" <re*******@gmx.net> wrote in message
news:hp********************************@4ax.com...

Hi,

I have a form called form1 on which there are a lot of checkBoxes
(they form a 8x6 field). I''d like to create a class in a seperate .cs
file which gives some control over these checkBoxes (for example to
make them all unchecked).

So I now created a .cs file for this class, but I can''t access the
checkBox elements on form1. They are both in the same namespace by the
way. What do I need to do? I tried to access it via Form1.CheckBox1
and CheckBox1. I did not use inheritance, do i need to?

Thanks in advance.

PS: Yes i am a newbie :)



这篇关于从不同的类/文件访问表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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