1

I have a main Windows Form (From1) which has a TextBox in it. I've also created another Windows Form (FindReplaceForm) which I'm going to use for implementing a form of find and replace dialog. I need to fully access all the properties and methods of my textbox in From1 from FindReplaceForm window.

Although I set the Modifiers property of my TextBox to Public, in FindReplaceForm window I can't access the text in it. enter image description here

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
wiki
  • 1,877
  • 2
  • 31
  • 47
  • 2
    You can create Form1 instance like this `Form1 form = new Form1();` This will create your Form1 and then access it components `form.TextBox_value.text` – Markiian Benovskyi Apr 06 '16 at 06:25
  • Possible duplicate of [How to access a control in one window form from other form](http://stackoverflow.com/questions/5125235/how-to-access-a-control-in-one-window-form-from-other-form) – default Apr 06 '16 at 06:42

5 Answers5

4

You need to pass a reference to the control or the form to your constructor so that you can reference the instance of the class. Add an argument of the same type as the calling form to the constructor:

private Form1 calling_form;

public FindReplaceForm (Form1 calling_form) 

{
    this.InitializeComponent()
    this.calling_form = calling_form;
}

Then in your button call you can say:

calling_form.TextBox_value_text.SelectedText = "";
Kateract
  • 822
  • 6
  • 15
4

You can access the the owner form in the child using:

((Form1)Owner).textBox1.Text = "blah";

Assuming you have called your the child form using:

Form2 form = new Form2();
form.Show(this);
1

In your code, Form1 is a CLASS, not a variable. When you show your FindReplaceForm you should specify the Owner (just use this). Now you can the Owner property on FindReplaceForm to get access to Form1.

Showing FindReplaceForm:

FindReplaceForm.Show(this);

In your button click event:

void Buttton1_Click(object sender, EventArgs e)
{
  ((Form1)this.Owner).TextBox_value_text.SelectedText = "";
}
Poul Bak
  • 10,450
  • 5
  • 32
  • 57
0

Even if you set the textbox access modifier back to private, you can simply pass a reference to the textbox in the second form's constructor, thus enabling the second form to manipulate any property of the textbox:

first form:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2(this.textBox1);
        frm.Show();
    }
}

second form:

public partial class Form2 : Form
{
    private TextBox _OwnerTextBox;

    public Form2(TextBox ownerTextBox)
    {
        InitializeComponent();
        _OwnerTextBox = ownerTextBox;
        this.textBox1.Text = _OwnerTextBox.Text;
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        _OwnerTextBox.Text = this.textBox1.Text;
    }
}
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
0

In your FindReplaceForm.button1_Click function you want to control an object of class Form1. The error in your code explains that you don't call a function of an object of class Form1, but a function of class Form1 itself. The latter can only be done on static functions

You describe that you have an existing Form1 object and that your FindReplaceForm needs to change Form1 by calling functions in Form1. Probably there might be more than 1 Form1 object. Therefor your FindReplaceForm instance somehow needs to know which Form1 object it needs to control. Someone needs to tell this to your FindReplaceForm. This is usually done using the constructor of the FindReplaceForm or using a property.

public class FindReplaceForm
{
    private Form1 formToControl = null;

    public FindReplaceForm(Form1 formToControl)
    {
        this.formToControl = formToControl;
    }

    private void OnButton1_Click(...)
    {
        this.formToControl.TextBox_Value_Text.SelectedText = ...
    }

Another method would be not to put the formToControl in the constructor, but as a property that you can set separately. Both methods have their advantages:

  • via constructor: your FindReplaceForm knows for sure there is alway a formToControl. The only place you have to check if formToControl really exists is in the constructor.
  • Using a default constructor with a separate FormToControl property may cause run time errors if people forget to set the FormToControl.
  • If you have a lot of properties, or some properties may have default values, or may be changed later, then the property method is more flexible.
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116