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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:53:27+00:00 2026-05-11T16:53:27+00:00

I always set the modifiers on my form to private ,I don’t like internal

  • 0

I always set the modifiers on my form to private,I don’t like internal nor public.

Till now I used to Invoke like this:

public string Addtext
{
    if(InvokeRequired)
    {
         Invoke((MethodInvoker)delegate
         {
             textbox.text = value;
         });
    }
    else
        textbox.text = value;
}

But adding such property for every member on my form is just not Object Orientated at all.

I want to create a function that will Invoke the parameter(action).I tried my best,but I failed – it requires the form members to be public or internal 🙁

    public void PerformActionOnForm(Action<FormMain> action)
    {
        var form = Form.ActiveForm as FormMain;
        object s = action.Clone();
        if (form != null)
        {
            form.PerformAction(action);
        }
    }

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

two problems in my code:

It requires the property I’d like to change to be != private 🙁

Doesn’t work if the form is not on focus.

  • 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-11T16:53:27+00:00Added an answer on May 11, 2026 at 4:53 pm

    In what way is adding properties for data that needs to be accessed or set outside of the scope of the form “not object oriented at all?” This is really your only option. Code in an anonymous delegate (or any delegate, for that matter) executes in the context in which it was declared. The only facility for getting around visibility issues is reflection, and that is big smelly code smell. Create your properties and use them as appropriate.

    As for your second option, I’m assuming that you want to execute this on your “main form”. You have two options here: assume that there is only one instance and keep that as a static property on the class, assigning it in the instance constructor.

    public partial class MainForm : Form
    {
        private static MainForm singletonInstance;
    
        public static MainForm SingletonInstance
        {
            get { return singletonInstance; }
        }
    
        public MainForm() : base()
        {
            InitializeComponent();
    
            singletonInstance = this;
        }
    }
    
    public void PerformActionOnForm(Action<FormMain> action)
    {
        var form = MainForm.SingletonInstance;
        // object s = action.Clone(); What was this for?
        if (form != null)
        {
            form.PerformAction(action);
        }
    }
    

    The other only works if all of your forms are properly “owned” and the only form with no owner is your main form. In that instance you could do this:

    public void PerformActionOnForm(Action<FormMain> action)
    {
        var form = Form.ActiveForm.TopLevelControl as FormMain;
        // object s = action.Clone(); What was this for?
        if (form != null)
        {
            form.PerformAction(action);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I always read that it is recommended to set objects to nothing, once I
My emacs (on Windows) always launches with a set size, which is rather small,
A simple stupid UPDATE table SET something=another WHERE (always true) in accident will easily
I've always taken the approach of first deploying the database with a minimal set
I'd like to create an internal auto-property: internal bool IP { get; protected internal
I'm making a jquery clone for C#. Right now I've got it set up
I'm getting dates formatted like this: 2011-04-14 15:00:00, and want to set them to
Always was interested why are Array.Sort() and Array.IndexOf() methods made static and similar ArrayList.Sort()
Always when I run java application it will display in Windows Task Manager is
I always believe they did, but seeing some answers here make me doubt... Can

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.