如何从另一个类调用FORM类中的方法,但两者都是相同的命名空间

时间:2010-11-30 10:33:51

标签: c# .net-3.5

class Form1 : Form 
{ 
  public void enable() 
  { 
  //1st method which i want to call from another class 
  } 
  public void display() 
  { 
  //2nd method which i want to call from another class 
  } 
} 
class Buffer : signal 
{ 
  protected override Analyse() 
  { 
  //from here i want to call two functions in form class 
  } 
} 

这是我的代码看起来像任何人请回复这个步骤.........

1 个答案:

答案 0 :(得分:1)

创建Buffer类时,必须将引用传递给Form1的实例,然后才使用该实例。示例代码:

class Form1 : Form 
{ 
  public void InitBuffer()
  {
    Buffer b = new Buffer(this);
    ...
  }

  public void enable() 
  { 
  //1st method which i want to call from another class 
  } 
  public void display() 
  { 
  //2nd method which i want to call from another class 
  } 
} 
class Buffer : signal 
{ 
  private Form1 form;

  public Buffer(Form1 parent)
  {
    form = parent;
  }
  protected override Analyse() 
  { 
    form.enable();
    form.display();
  } 
} 

你无法抓住Form1的真实实例。