wiki
wiki

Reputation: 2063

Fully access a control from another Form

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

Upvotes: 1

Views: 9728

Answers (5)

Poul Bak
Poul Bak

Reputation: 10940

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 = "";
}

Upvotes: 1

Harald Coppoolse
Harald Coppoolse

Reputation: 30512

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.

Upvotes: 0

Zohar Peled
Zohar Peled

Reputation: 82544

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;
    }
}

Upvotes: 0

Mayura Vivekananda
Mayura Vivekananda

Reputation: 674

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);

Upvotes: 4

Kateract
Kateract

Reputation: 852

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 = "";

Upvotes: 4

Related Questions