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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:26:07+00:00 2026-05-14T02:26:07+00:00

I have a simple forms program that I have been fighting with for a

  • 0

I have a simple forms program that I have been fighting with for a while now. I simply want to be able to call a method from a different class file (when a certain step is triggered in the code in that class file) in order to insert a string in the listBox.

Here is my main method, pretty standard:

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());
    }
}

Here is the function which resides in my MainForm.cs file, which I can call just fine from that class file (via ‘TextToBox(myString);’):

public partial class MainForm : Form
{
    ...
    // Function to output results to main Listbox window        
    public void TextToBox(string aString)
    {            
        // Place messages in Main Display list box window
        this.listBox1.Items.Insert(0, aString);            
    }
    ...
}

But my problem is when I am in another class and I want to call ‘TextToBox(myString);‘. If I create another object reference of the MainForm, the code compiles fine but nothing will show up in the listBox. How do I do this? I cannot simply make TextToBox() static. I know I must create the object reference but I can’t figure out how to reference the ORIGINAL MainForm, the one that was created in the Main method. Thanks in advance…

  • 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-05-14T02:26:08+00:00Added an answer on May 14, 2026 at 2:26 am

    This will work, but only when you have one instans of MainForm.

    public class MainForm : Form
    {
        public MainForm()
        {
            Instance = this;
        }
    
        public static MainForm Instance { get; private set; }
    
        // Function to output results to main Listbox window        
        public void TextToBox(string aString)
        {
            // Place messages in Main Display list box window
            this.listBox1.Items.Insert(0, aString);
        }
    
    }
    public class Other
    {
        public void AddTextToListBox()
        {
            MainForm.Instance.TextToBox("Test");
        }
    }
    

    …Edit…
    Alternative:

    class Program
    {
        public static MainForm MainFormInstance;
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            MainFormInstance = new MainForm();
            Application.Run(MainFormInstance);
        }
    }
    
    public class Other
    {
        public void AddTextToListBox()
        {
            Program.MainFormInstance.TextToBox("Test");
        }
    }
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple model forms, that is being rendered through the following jinja2
I have a couple of simple forms that send an html-only email. Most clients
I have a requirement to create a simple windows forms application that allows an
I have a simple asp.net program with forms authentication, session management etc. This application
Coming from C++, I'm used to be able to build simple forms of compile-time
I have to write a simple program that will draw something in a Windows
I have a simple console app written in C#. I want to be able
I have to build a program that will allow me to track from a
So I have been modifying the chat program found here: http://www.dreamincode.net/forums/topic/259777-a-simple-chat-program-with-clientserver-gui-optional/ and I am
On Windows 7, VB.NET Express, I have developed a simple Forms application. I don't

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.