2012-10-24 41 views
3

在我的Main方法里面我实例化了UpdateDialog类,其中基于用户是否按下按钮,我需要从Main调用function1()。下面是代码:从子类中调用公共父功能

public partial class Main : Form 
{ 
    public void function1() 
    { 
    doing_stuff_here(); 
    } 

    private void button1_Click(Object sender, EventArgs e) 
    { 
    var update = new UpdateDialog(); 
    update.ShowDialog(); 
    } 
} 

public partial class UpdateDialog : Form 
{ 
    private void button2_Click(object sender, EventArgs e) 
    { 
    //call here function1() from Main 
    } 
} 

我应该怎么做才能够调用function1()Main部分类UpdateDialog里面?

LE:尽管Styxxy建议的方法看起来不错,但由于cross-thread invalid operation的原因,在我的应用中效果不好,所以我最终使用了Cuong Le建议的delegate workaround

回答

15

您必须在UpdateDialog表单中有Main表单的实例。正如你所说的,UpdateDialog是你的Main窗体的一个子窗体,我猜你在你的Main窗体中创建了UpdateDialog并在那里进行了显示。在显示该表单之前,您可以指定Parent property

var updateDialog = new UpdateDialog(); 
// Or use "UpdateDialog updateDialog = new UpdateDialog();" as people like Andreas Johansson don't like the "var" keyword 
// Do other stuff here as well 
updateDialog.Parent = this; 
// Or use Show() for non modal window 
updateDialog.ShowDialog(); 

您会收到错误ArgumentException: Top-level control cannot be added to a control.。现在可以通过两种方式来解决这个问题。

  1. 您可以设置主窗体上的TopLevel属性false(我不是这方面的一个巨大的风扇)。
  2. 您可以将Owner属性用于主窗体(this)。以下两种方式。

您可以将Owner手动设置:

updateDialog.Owner = this; 

也可以添加this作为参数传递给Show(owner)ShowDialog(owner)方法;这样,Owner也正在设置。

updateDialog.Show(this); 
// or 
updateDialog.ShowDialog(this); 

“完全” 的代码使这个:

var updateDialog = new UpdateDialog(); 
// Do other stuff here as well 
updateDialog.Owner= this; 
updateDialog.ShowDialog(); // or use .Show() 
// or 
updateDialog.ShowDialog(this); // or use .Show(this) 
+1

-1代表'var'。我们知道这里的对象的类型。没有一点使用'var'。 – 2012-10-24 07:25:00

+3

@AndreasJohansson:为什么我们不能在这里使用'var'? –

+3

不同意。 var是首选**,因为**我们可以看到什么对象正在实例化。声明类型将是重复。 – GazTheDestroyer

-1

方法#1

您需要创建Main类的一个实例。

Main foo = new Main(); 
foo.function1(); 

方法2

您需要声明的方法为静态。

public static function1(){ ... } 
.... 
Main.function1(); 
3

Main类的实例,以您的更新表格,并将其存储在实例变量 -

Main mainWindow = null; 
public UpdateDialog(Main mainForm) 
{ 
    mainWindow = mainForm; 
} 

private void button2_Click(object sender, EventArgs e) 
{ 
    mainWindow.function1(); 
} 

而且从Main方法 -

private void button1_Click(Object sender, EventArgs e) 
{ 
    var update = new UpdateDialog(this); 
    update.ShowDialog(); 
} 
0

能通过主类等的引用。

例如:

public partial class Main : Form 
{ 
    //make it internal, if UpdateDialog in the same assembly, and it only one that  would use it. In other words hide it for outside world. 
    internal void function1() 
    { 
     doing_stuff_here(); 
    } 
.... 
} 


public partial class UpdateDialog : Form   
{ 
     private MainForm _main = null; 
     public UpdateDialog (MainForm main) { //Accept only MainForm type, _not_ just a Form 
     _main = main; 
     } 

     private void button2_Click(object sender, EventArgs e) 
     { 
      _main.function1(); //CALL 
     } 
} 

这样的事情。您可以根据自己的具体要求更改此累加值,但这是一个普遍的想法。

4

我建议你在UpdateDialog中创建一个事件,然后在Main类中创建一个实例后订阅它。这样你可以更好地分离这两个类。

public partial class Main 
{ 
    public void function1() 
    { 
     doing_stuff_here(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     var update = new UpdateDialog(); 
     update.OnButton2Click += OnUpdateDialogButton2Click; 

     update.ShowDialog(); 
    } 

    void OnUpdateDialogButton2Click(object sender, EventArgs e) 
    { 
     function1(); 
    } 
} 

public partial class UpdateDialog 
{ 
    public event EventHandler<EventArgs> OnButton2Click; 

    private void button2_Click(object sender, EventArgs e) 
    { 
     //call here function1() from Main 

     if (OnButton2Click != null) 
     { 
      this.OnButton2Click(this, e); 
     } 
    } 
} 
2

您可以将其关闭,让主表单听取来自UpdateDialog的点击。

在主营:

private void button1_Click(Object sender, EventArgs e) 
{ 
    var update = new UpdateDialog(); 
    update.OnSomethingClicked += function1; 
    update.ShowDialog(); 
} 

void form_OnSomethingHappened(object sender, EventArgs e) 
{ 
    // Do the stuff you want 
} 

在UpdateDialog:

public event EventHandler OnSomethingHappened; 

private void button2_Click(object sender, EventArgs e) 
{ 
    EventHandler handler = OnSomethingHappened; 
    if (handler != null) handler(this, e); 
} 
-1

你可以让你的功能1分部方法和这种方式,您可以在您的所有部分类使用它。

部分方法允许将方法的定义放置在一个文件中,并将方法的正文定义在另一个文件中。它们只能在部分类中使用,并作为C#3.0和Visual Basic 9.0中的语言功能引入,这些版本随.NET Framework 3.5和Visual Studio 2008提供。

因此,您可以做的就是像这样修改

public partial class Main : Form 

     { 
      public partial void function1() 
      { 
       doing_stuff_here(); 
      } 

      private void button1_Click(object sender, EventArgs e) 
      { 
       var update = new UpdateDialog(); 
       update.ShowDialog(); 
      } 
     } 

public partial class UpdateDialog : Form 
     { 
      public partial void function1(); 
      private void button2_Click(object sender, EventArgs e) 
      { 
      function1(); 
      } 
     }