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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:06:35+00:00 2026-06-12T19:06:35+00:00

I have a form called Form1. In it, I have a button called Encrypt.

  • 0

I have a form called Form1. In it, I have a button called Encrypt. When I hit that button I invoke a method in a different class. I want that method to change the value of textBox1.text but nothing happens when I use this code

in Form1 class

public string txtbox1
    {
        get
        {
            return textBox1.Text;
        }

        set
        {
            textBox1.Text = value;
        }
    }

in a method in the other class

Form1 textboxes = new Form1();//creating object of the Form1 class
        textboxes.txtbox1= "whatever";

Nothing changes in the first text box. It’s like I don’t press anything at all!!!

Any help would be very appreciated

  • 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-06-12T19:06:36+00:00Added an answer on June 12, 2026 at 7:06 pm

    In your other class, you need a reference to the Form on which the button was clicked (and that has your existing text-boxes), not a new form.

    This new form you’re instantiating isn’t the one that you’re looking at on the screen where you clicked your button.

    (I’m assuming your event handler exists within the Form1 class, and that it then “forwards” information out to other class’s method as required? If not… it should!)

    The button reference will be obtainable through the sender object and the event args passed to your event handler. You can pass a reference to the current Form1 instance by passing the this keyword to your other class’s method. Or you could pass the sender if that’s useful to you, or just pass an explicit reference to the specific text box through to your other method.

    eg, to pass a reference to the form to your other method:

    // Event handler on your form
    private void button1_Click(object sender, EventArgs e)
    {
        ButtonWasPressedOnForm(this);   
    }
    
    // Other method in your other class
    public void ButtonWasPressedOnForm(Form formWhereButtonPressed)
    {
        // To act on the text box directly:
        TextBox textBoxToUpdate = (TextBox)formWhereButtonPressed.Controls.Find("textBox1");
        textBoxToUpdate.Text = "whatever";
    
        // Or, using the Form1.txtBox1 property.
        formWhereButtonPressed.txtBox1 = "whatever";  
    }
    

    eg, to pass a reference to explicit text box to your other method:

    // Event handler on your form
    private void button1_Click(object sender, EventArgs e)
    {
        ButtonWasPressedOnForm(textBox1);   
    }
    
    // Other method in your other class
    public void ButtonWasPressedOnForm(TextBox textBoxToUpdate)
    {
        textBoxToUpdate.Text = "whatever";
    }
    

    eg, to pass the event object to your other method:

    // Event handler on your form
    private void button1_Click(object sender, EventArgs e)
    {
        Button clickedButton = (Button)sender;
        ButtonWasPressedOnForm(clickedButton);  
    }
    
    // Other method in your other class
    public void ButtonWasPressedOnForm(Button clickedButton)
    {
        Form theParentForm = clickedButton.FindForm();
    
        // To act on the text box directly:
        TextBox textBoxToUpdate = (TextBox)theParentForm.Controls.Find("textBox1");
        textBoxToUpdate.Text = "whatever";
    
        // Or, To act on the text box, via the form's property:
        theParentForm.txtBox1 = "whatever";
    }
    

    Also, stick a break point on your “other method” to ensure this code is even being fired. If not, go back to your event-handler, ensure that’s being fired. If not, check your event wire-up.


    Although in all cases you need to be careful of the protection level on the control you want to update… you’ll need to make it public, internal, or protected depending on the relationship between your form and the other class, if you want to update it from outside your Form1 class.

    A better OO approach would be to have a method on Form1 that allows other classes to tell Form1 to update the control for them (eg updateTextBox(string newText)). Because it’s not OO best-practice allow external objects to act on the members of your classes directly (as this requires knowledge of the internal structure of your class… which should be encapsulated so that your implementation can change without breaking the interface that exists between your class and the outside world).

    EDIT:
    Actually, on re-reading your question, you do already encapsulate your text boxes using get/set properties. Nice. So you should pass the reference to your Form to your other method, and then update the form’s text via the property. Have added this method to the examples above.

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

Sidebar

Related Questions

I have a method, -(void)validateFormInput:(UITextField *)textField that gets called when the form's submit button
I have a form validation method that's called on submit. It executes a get
I have a form called Form1.cs, which calls a class which we will refer
I have a form with a submit button. I have called a function on
Assume we have a single Windows form with a button called SimpleButton1. The following
I have a WinForm project that contains a form called MainUI. You can see
We have a C# win Form app called Installer that silently installs various 3rd
I have a windows form app, and a class which is called Game. Let's
I have a base and derived class called Node which form elements of a
I have got a textbox on a form with a method being called from

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.