1

I want to expose the Text property of a textbox on Form1 to Form2 so Form2 can set the text in the textbox on Form1. I've read how to do it but it doesn't work so I must be doing something wrong.

Here's the code for Form1 including the declaration of the public property (TextInputText is the property, txtInput is the textbox):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public string TextInputText
        {
            get => txtInput.Text;
            set => txtInput.Text = value;
        }

        public Form1()
        {
            InitializeComponent();                       
        }

        private void txtInput_KeyDown(object sender, KeyEventArgs e)
        {
            // If enter is pressed clear the textbox, but update() the history first

            if (e.KeyCode == Keys.Enter)
            {
                TextHistory.Update(txtInput.Text);
                txtInput.Text = "";
            }
        }

        private void HistoryButton_Click(object sender, EventArgs e)
        {
            Form2 HistoryForm = new Form2();
            HistoryForm.Show();
        }
    }
}

The problem is Form2 still can't see the property, or I don't know how to access it, what am I doing wrong?

  • Hard to say what you're doing wrong without seeing how you're trying to access it. – Klaycon Dec 18 '19 at 16:01
  • Don't see how Form1 and Form2 are interacting. Are you following the design pattern from either of these posts? https://stackoverflow.com/questions/39026518/passing-values-from-one-form-to-another-form-in-a-button-click https://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form – mjung Dec 18 '19 at 16:03
  • I've tried using FormName.TextInputText, I thought that's how you access it. – Glass Wizzard Dec 18 '19 at 16:05
  • You need to give Form2 a reference to Form1. How else could Form2 access anything about Form1? – Christopher Dec 18 '19 at 16:06
  • Please share a code example of trying to access it. This code looks ok to me so the problem is probably there. – Klaycon Dec 18 '19 at 16:07
  • I don't know how to access it, that's the problem. – Glass Wizzard Dec 18 '19 at 16:10
  • Form2 has a listbox, and I want to assign the clicked on text to the textbox in form1 when a listbox item is clicked on and I guess the place to do this is in the listbox's SelectedIndexChanged event handler. – Glass Wizzard Dec 18 '19 at 16:12

2 Answers2

2

Either inject Form2 with a reference to Form1 when you create it:

private void HistoryButton_Click(object sender, EventArgs e)
{
    Form2 HistoryForm = new Form2(this);
    HistoryForm.Show();
}

This requires you to define a custom constructor in Form2 that accepts a Form1 reference. You can then use this reference to access the property:

private readonly Form1 _form1;
public Form2(Form1 form1)
{
    InitializeComponent();
    _form1 = form1;

    string text = _form1.TextInputText;
}

Another approach is to use the Application.OpenForms property to get a reference to Form1 in Form2:

var form1 = Application.OpenForms.OfType<Form1>().FirstOrDefault();
string text = form1.TextInputText;
mm8
  • 163,881
  • 10
  • 57
  • 88
1

You do not give Form2 a reference to the Form1 instance:

Form2 HistoryForm = new Form2();

How could you access a Instance Function, Property or Value, without a Instance? A static property would not make sense. So the most likely option is to give Form2 a constructor that takes a Form1 reference as Argument. Store that reference somewhere in Form2. Then call the constructor like this:

Form2 HistoryForm = new Form2(this);

Christopher
  • 9,634
  • 2
  • 17
  • 31
  • That sounds just like what I need. Could you tell me how I make a constructor for Form2 please, I know all about constructors, I've just never made one for a form before. – Glass Wizzard Dec 18 '19 at 16:15
  • @GlassWizzard: Please see my answer for an example of a constructor. – mm8 Dec 18 '19 at 16:17