Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't change textBox.text in form from other classes - C# - VS 2010

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

like image 581
RonaDona Avatar asked Nov 24 '25 02:11

RonaDona


2 Answers

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.

like image 161
Sepster Avatar answered Nov 25 '25 16:11

Sepster


I foud an easy way to do this. First Creat a TextBox in your Class

class class1
   {
   public static TextBox txt1=new TextBox();
   public static void Hello()
     {
      txt1.Text="Hello";
     }
   }

There is a button(button1) and a TextBox(textBox1) on form1. ok,now Copy the address of Your Desired TextBox Must be changed I tested it,it work properly

public partial class Form1 : Form
{
 public Form1()
    {
     InitializeComponent();  
    }
 private void button1_Click(object sender, EventArgs e)
     {
      class1.txt1=textBox1;
      class1.Hello();
     }
}
like image 35
Arash Mohamadzadeh Avatar answered Nov 25 '25 16:11

Arash Mohamadzadeh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!