从类访问控件 [英] accessing controls from classes

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

问题描述

您好,很抱歉提出这样一个简单的问题,如何从类中访问位于form_上的panel _?

Hi,sorry to ask such a simple question,How can I access a panel_which is on my form_ from a class?

推荐答案

枚举控件"中的所有控件"表格的集合.检查面板"的类型.如果直接后代中有多个面板,则可能需要某种方式来标识面板.通常,使用名称"属性标识每个控件.
Enumerate all controls in "Controls" collection of the form. Check type for "Panel". You may need some way to identify the panel if there are more than one panel in the direct descendents. Normally, identify each control using the "Name" property.


最好的方法是将控件的引用传递给所涉及的类,如下所示:
The best way to do that is to pass a reference for the control to the class in question, like this:
public class MyClass
{
    public void DoSomething(ref ListBox listbox)
    {
        ... do something with the listbox contents
    }
}


并以您的形式:


And in your form:

myClass.DoSomethingWith(ref listBox1);


当然,如果您根本不想修改控件,则不需要ref关键字.


Of course, if you don''t want to modify the control at all, you don''t need the ref key word.


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

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