开发者

C# Component Container

I have a windows form and my own class in my project

I have a method in my own class

public object Sample(Form MyForm,string ComponentName)
{

}

I want to get components of the "MyForm" from another class How Can I Make THIs?

form class

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

}

MyOwnClass

public class Sample
{开发者_开发技巧

public object GetComponentMethod(Form form,string ComponentName)
{

////
}

}


Have you tried with:

Control myControl= form.controls.Find(...)?

updated

Sorry but in this case I cannot understand what are you looking for!

updated

you have to create a public property Components! So you can retrieve data you need!


It looks like you are just trying to access members of one object from another object.

If so you need to expose some way of accessing a specific instance of a class.

If you will only ever have one instance (of say your Form1) the simplest way is to expose that single instance via a public static property. This is called a singleton pattern:

public partial class Form1 : Form
{
    public static Form1 Singleton { get; private set; }
    public Form1()
    {
        Form1.Singleton = this;
        InitializeComponent();
    }
}

You can the access your Form1 instance using Form1.Singleton.SomeProperty from anywhere.

I am not promoting any specific Singleton pattern here, as there are too many issues over thread safety, but for your simple example this will do the job. Call the static property "Singleton" or "This" or "SolutionToMyWoes" or whatever you like!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜