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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:12:55+00:00 2026-05-11T22:12:55+00:00

In my C# project,I used the following code to access my form outside its

  • 0

In my C# project,I used the following code to access my form outside its own class.

public static FormMain singletonInstance { get; private set; }

    public static void PFA(Action<FormMain> action) //PFA = PerForm Action
    {
        var form = FormMain.singletonInstance;
        if (form != null)
        {

            form.PerformAction(action);
        }
    }

    public void PerformAction(Action<FormMain> action)
    {
        if (InvokeRequired)
            Invoke(action, this);
        else
            action(this);
    }

And the constructor in “FormMain” is:

    public FormMain()
    {
        InitializeComponent();
        singletonInstance = this;
    }

I don’t know how to do it in Delphi.

Question(Delphi): How do I change something on form(for example Edit1.Text) when Im outside its class,Invoke?

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-11T22:12:56+00:00Added an answer on May 11, 2026 at 10:12 pm

    The need to Invoke() an action has nothing to do with the calling code being inside or outside its class, it’s necessary to make sure that methods operating on controls are called only in the context of the thread the control was created in. Controls have a thread affinity, which is nothing specific to C# or .NET, it is inherent in the way Windows works. Messages for a control / window can be handled in the thread it was created in only. This holds true for Delphi as well, and the whole VCL is not thread-safe either.

    The closest thing to Invoke() the Delphi VCL has is the Synchronize() method. It is used within secondary threads to schedule code to be executed in the context of the main VCL thread, the user interface thread in which all VCL controls need to be created, in which consequently all Windows messages for those controls are handled, and which is the only one really safe for calling any of the control methods.

    A Delphi program does not create any secondary threads on its own, you will have to do this yourself. But unless you do, everything happens in the main thread context and there is no need to call Synchronize(). You can simply call the method you need to call.

    To stay with your example: You set new text for an edit control like so:

    Form1.Edit1.Text := 'foo bar baz';
    

    This assumes that the unit containing the TForm1 class contains a variable Form1, which it usually does. This is similar to the singleton in your C# code. It works because all controls dropped onto the form will have public visibility.

    Off-topic-remark:

    Note that while this is fairly common code it violates the Law of Demeter. Changing the controls in the form will make it necessary to subsequently change all code that accesses them in this way. But this is another discussion entirely.

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

Sidebar

Related Questions

I used the wiki in a project hosted at Google Code. With this wiki
I'm working on a project that is used to input information about a product
I am looking for a reference describing precisely the XML project file format used
While I was working on an University project, I used a project-internal profiler made
I used jQuery exclusively on my last project to do all my AJAX calls.
I used windows installer (msi project) and actually I have the msi file after
I used to be able to right click Folders in my project and click
I used to have a Tests folders in my main project where a unit
Has anyone used Rhino igloo in a non-trivial project? I am curious if it's
In a recent project I have nearly completed we used an architecture that as

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.