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

  • Home
  • SEARCH
  • 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 5848627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:53:09+00:00 2026-05-22T12:53:09+00:00

I am developing a win form app and I found myself constantly requiring to

  • 0

I am developing a win form app and I found myself constantly requiring to access methods in my parent form say Form1 from another class be it a form class or just a class. I have some initializers in the constructor of my form 1 and hence I am not able to create an instance of the Form1. So I am not able to access the methods of Form1.

So I feel like this is a bad practice. However, there are certain instances that I don’t know what else to do for instance consider this scenario. I have a class called ProcessData in which I have a method which receives a file, reads it line by line and process the data. Now I’m calling this method as a thread from my main form Form1. My requirement is as the data process I want to show the line currently under process in a multiline textbox in the main form Form1.

Previously what I did have I had everything in the same Form1 so I used a delegate, like

delegate void SetTextCallback(string text, Control ctrl);
private void SetText(string text, Control ctrl)
    {
        if (ctrl.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(SetText);
            this.Invoke(d, new object[] { text, ctrl });
        }
        else
        {
            if (ctrl.GetType() == typeof(Label))
            {
                ctrl.Text = text;
            }
            else
            {
                ctrl.Text += Environment.NewLine + text;
            }
        }
    }

and I invoked this like SetText(“text”,Label1);

but if I am calling this from another class to refer Label1 I will need an instance of Form1 but I will not be able to create it, so what is the best practice to do it?

(I know I can just pass the text to SetText and handle the control there but I am going to use this same thing for a variety of textbox and label controls invoked from different classes)

  • 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-22T12:53:10+00:00Added an answer on May 22, 2026 at 12:53 pm

    The way that I normally do this is to have the child form expose events corresponding to the logical actions and events on that form, for example:

    /// <summary>
    /// Occurrs when an item is selected in the form
    /// </summary>
    public event EventHandler<ItemSelectedEventArgs> ItemSelected;
    
    /// <summary>
    /// Fires the <see cref="ItemSelected" /> event
    /// </summary>
    protected void OnItemSelected(MyItem item) 
    {
        var handler = this.ItemSelected;
        if (handler != null)
        {
            ItemSelectedEventArgs args = new ItemSelectedEventArgs();
            args.Item = item; // For example
            handler(this, args);
        }
    }
    

    The idea is that the logic of your parent form should respond to actions on your child form, rather than actions on your child form driving actions on the parent form – you should try to encapsulate the forms logic as much as possible (aka separation of concerns).

    Also as a pattern it should be the parent / calling form that handles marshalling the calls across to the correct thread via InvokeRequired etc… rather than the child form – this will be unneccessary anyway unless you are doing work on background threads.

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

Sidebar

Related Questions

I am developing a c# winform app which controls a website using web browser.
I am developing a C# application (.Net 3.5, Win Forms) which is run on
I'm developing with SDK 1.6.2. My app uses the camera to capture and save
Background: I'm developing a WinForms application using C# with an OpenFileDialog and FileBrowserDialog that
As I am working on .Net windows form application, we're using MongoDB for back-end
I am developing a winform application. I want to play a sound file, till
I'm developing Silverlight OOB application and I need to show web pages in it
When developing Rails project using Textmate , there are some related modes. When pressing
I am developing a java application and deploy it using batch file. At work,
I have an application I am developing that uses both jar libraries and native

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.