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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:50:00+00:00 2026-05-24T15:50:00+00:00

I came to the same question again and again. I need to use the

  • 0

I came to the same question again and again. I need to use the user entered values after a button event, or a doubleclick, or anything. when I do it with the designer, it passes automatically the txt control and its value to the whole program, and I can use it anywhere. But programatically I couldn’t solve it.
here’s a little example:

        private void Form1_Load(object sender, EventArgs e)
    {
        string blabla = "anything";

        Button btn = new Button();
        btn.Location = new Point(10, 40);
        btn.Text = "Click me";
        btn.Click += new EventHandler(btn_Click);
        this.Controls.Add(btn);

    }
    void btn_Click(object sender, EventArgs e)
    {
        MessageBox.Show(blabla);
    }

this doesn’t work, so I added a “public” and the script goes:

    public string blabla;
    private void Form1_Load(object sender, EventArgs e)
    {
        blabla = "anything";

        Button btn = new Button();
        btn.Location = new Point(10, 40);
        btn.Text = "Click me";
        btn.Click += new EventHandler(btn_Click);
        this.Controls.Add(btn);

    }
    void btn_Click(object sender, EventArgs e)
    {
        MessageBox.Show(blabla);
    }

And so I can use my variable with the changed values. This goes well with controls too.
This works, but this makes thousands of public variables in a bigger application. How can I increase the readability by losing these publics? Is there a way to use “ref”? I saw it on the automatic “extract method”, I just don’t know, how can I use that with events.

Maybe I am on the wrong track in this, if there is a shortcut or other solution, please help.

  • 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-24T15:50:01+00:00Added an answer on May 24, 2026 at 3:50 pm

    The important change between the two snippets wasn’t the fact that you made the variable public – it’s that you changed it from a local variable in the Form1_Load method into an instance variable. It can still be a private instance variable, if you’re handling it in the same class.

    However, another alternative is to keep it as a local variable but use an anonymous function to handle the event:

    private void Form1_Load(object sender, EventArgs e)
    {
        string blabla = "anything";
    
        Button btn = new Button();
        btn.Location = new Point(10, 40);
        btn.Text = "Click me";
        btn.Click += (sender, args) => {
            MessageBox.Show(blabla);
            // Other code here, but hopefully not too much...
        };
        this.Controls.Add(btn);
    }
    

    (As noted, you don’t want to make the anonymous function too big, for the sake of readability – but it can always call another method with all the appropriate state.)

    EDIT: As you’re using VS2005, you’re only using C# 2 so you can’t use lambda expressions. You can use anonymous methods though. The code would then be:

    private void Form1_Load(object sender, EventArgs e)
    {
        string blabla = "anything";
    
        Button btn = new Button();
        btn.Location = new Point(10, 40);
        btn.Text = "Click me";
        btn.Click += delegate {
            MessageBox.Show(blabla);
            // Other code here, but hopefully not too much...
        };
        this.Controls.Add(btn);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I already asked same question here after that i came up with this code
Well this question came after this one The reasons are pretty much the same,
In trying to answer this question for myself I came across this nugget, after
I want to use _forward() in preDispatch after checking if the user is logged
Is there any way (just out of curiosity because I came across multiple same-value
I came in this morning, fired up my app in the same way as
(Came up with this question in the course of trying to answer this other
This question came about as a result of some mixed-language programming. I had a
this time I got a more general question. Should I use multiple views rather
Lately I came up here with a similar question. I want to color special

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.