Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8731297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:12:16+00:00 2026-06-13T09:12:16+00:00

Inside my Main method I’m instantiating the UpdateDialog class inside which based on if

  • 0

Inside my Main method I’m instantiating the UpdateDialog class inside which based on if the user presses a button or not I need to call function1() from Main. Here is the code:

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
  }
}

What should I do to be able to call function1() from Main inside the partial class UpdateDialog?

LE: although the method suggested by Styxxy seems right it doesn’t work well in my app because of cross-thread invalid operation so I ended up using the delegate workaround suggested by Cuong Le.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:12:18+00:00Added an answer on June 13, 2026 at 9:12 am

    You’ll have to have an instance of the Main form in your UpdateDialog form. As you say that UpdateDialog is a child form of your Main form, I guess that you create the UpdateDialog in your Main form and do a show there. Before showing that form, you could assign the 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();
    

    You get the error ArgumentException: Top-level control cannot be added to a control.. Now this can be solved in two ways.

    1. You can set the TopLevel property to false on your Main form (I’m not a huge fan of this).
    2. You can use the Owner property to your Main form (this). Below two ways of doing it.

    You can set the Owner manually:

    updateDialog.Owner = this;
    

    Or you can add this as parameter to the Show(owner) or ShowDialog(owner) methods; this way, the Owner is also being set.

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

    “Full” code makes 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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The easiest implementation is when we call from single class main method other classes
I'm trying to call a class (main method) from command line (Windows) with Java.
Why we put main() method always inside the class in C# while in c++
In C++ why don't we ever place the main method inside a class (like
in this code : public class Main{ private void method() { System.out.println(inside method); }
What's the main point behind putting variables and method signatures inside ApplicationDelegate.h in Objective-C
I have two MAIN DIVs inside a DIV which is a PART of the
I have two layouts, one is the main layout inside which I am including
Say I have an array: array[2][4] , and inside the main method, I have
I have one clarification, class Foo { } main() { Foo obj; //1 method(obj);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.