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 9123563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:22:15+00:00 2026-06-17T06:22:15+00:00

I have a Visual C# 2010 application, and it has one main form called

  • 0

I have a Visual C# 2010 application, and it has one main form called MainWnd with other tool windows and dialogs. I want the other tool windows to be able to ‘talk’ to the main form, and call its methods. But that requires an instance of MainWnd, and since there will only be one of these forms created at any given time there is no reason while I should enumerate through all instances of MainWnd or look for the first one. So I want my main application form MainWnd to be a singleton so other windows can easily call code from it.

Here is the code of the main form that I would like to make a singleton:

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace MyLittleApp
{
    public partial class MainWnd : Form
    {
        public MainWnd()
        {
            InitializeComponent();
        }

        public void SayHello()
        {
            MessageBox.Show("Hello World!");

            // In reality, code that manipulates controls on the form
            // would go here. So this method cannot simply be made static.
        }
    }
}

I am looking to be able to call SayHello() from another form, simply by writing:

MainWnd.SayHello();

How could I accomplish this?

  • 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-17T06:22:16+00:00Added an answer on June 17, 2026 at 6:22 am

    You could probably find a way to make the main window a singleton, however that’s not the best way to achieve the outcome you want, nor is it really an appropriate situation in which to use the singleton pattern.

    If all of the other tool windows/ dialogs are encapsulated within the main window, then a much better pattern to use for communication would be events.

    Have the inner windows/dialogs raise events to represent a ‘request’ for the main window to do something. Have the main window subscribe to these events, and do the work via the event handlers.

    By avoiding the singleton approach, you avoid the difficulties of testing the singleton, as well as avoiding extensive explicit circular references, where not only does the main window have references to the encapsulated windows/dialogs, but they in turn have explicit references back to the main window.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# console application in Visual Studio 2010. It has a Main()
Visual Studio 2010 Express, Windows Forms. Have made myself my first little application which
I have created a Visual Studio 2010 ASP.NET Web Application. With it I have
I have created a Visual Studio 2010 ASP.NET Web Application. I've noticed that if
Visual Studio 2010 RC -> Silverlight Application We have a library of images that
I have a C# application, and a Visual Studio (2010) Setup Project with it.
Background - Visual Studio 2010, C#, .Net Framework 4.0 application I have a web
I have a large-ish Visual Studio 2010 solution (~110 C# projects). One project (let's
I have a web application solution existing under Visual Studio 2010. Part of this
I'm using Visual Studio 2010 on a Windows XP SP3 machine and I have

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.