0

I've been trying to figure out how to have a user input a string into a text box in Form1 and then hit a "Done" button and display Form2 with a Label containing the text from the text box in Form1.

I had come across this question which almost solved my problem (Send values from one form to another form) but the answer to the above question showed you how to hit a button, display Form2, then input a string, and hit another button returning to form 1 where the label displayed the text inputted in Form2.

I want to be able to input the string into a text box in Form 1 and when you hit a button display Form 2 with the label containing the string from the text box in Form 1.

I've spent a good hour or so trying to deconstruct the tiny bit of code from the linked question and get it to work how I want, but I just can't seem to understand it. If theres anyone who could either explain how to get a label in Form2 to contain a string from a text box in Form1 or show me the code necessary so I may look over it and understand it, it'd be greatly appreciated. As I currently cannot seem to get a grasp of how the code works in the previous question I linked.

This is the bit of code I used from the above linked question

// Form 1
// inside the button click event
using(Form2 form2 = new Form2()) 
{
    if(form2.ShowDialog() == DialogResult.OK) 
    {
        someControlOnForm1.Text = form2.TheValue;
    }
}

And...

// Inside Form2
// Create a public property to serve the value
public string TheValue 
{
    get { return someTextBoxOnForm2.Text; }
}
Miguel M
  • 3
  • 2
  • Huh? What do you mean "without returning to Form1"? Please edit your question and illustrate what you mean with a code sample (note that this should be as text, not an image). – ProgrammingLlama Feb 19 '20 at 02:44
  • I've updated it to try to make it clearer. I'm using the exact code from the linked question but as I mentioned in the original question, it only allows you to open a new form, input the text into that form, and then after hitting a button on that second form, it closes it out and Form 1 has a label containing the text. I'm essentially trying to reverse that where you hit a button in form 1 and form 2 gets the string, not the other way around. – Miguel M Feb 19 '20 at 02:50
  • Make `TheValue` a Get/Set property (it could also be a public method), so you can set it when you create a new instance of `Form2` in `Form1`. Set the Property, which in turn will set the internal `Label.Text`, then Show `Form2`. The `using` block may be required if you show `Form2` as modal (`ShowDialog()`). Remove it if you use `Form2.Show()` or `Form2.Show(this)` (otherwise you'll dispose of it right after). – Jimi Feb 19 '20 at 02:56
  • you want to set Text from Form2 to Form1 without return to Form1? – VietDD Feb 19 '20 at 03:16
  • From Form 1 to Form 2 without returning to the Form 1 window. It's essentially a customization window that comes up first where the user can input a name and when you hit a button to move onto Form 2, it shows the name you chose. It's a text based adventure I'm designing as a project for school. – Miguel M Feb 19 '20 at 03:23
  • check my answer bellow – VietDD Feb 19 '20 at 03:33
  • my example to 2 things : when open Form2, display text on Form1. Then on Form2, if you change text, display text on Form1 will also change – VietDD Feb 19 '20 at 03:37

3 Answers3

0

In m understanding you want to get around with the data across forms?

If you want to get the data and retrieve it forms you can try this

Create a class name called Globals

public class Globals
{
private static string _myData;
public static string MyData
{
    get
    {
        // Reads are usually simple
        return _myData;
    }
    set
    {
        _myData = value;
    }
 }

}

in your Form1 set a value in a global variable

Globals.myData = "someText"; //set the value

in your Form2 get the value

//example we have a lable
label1.Text = Globals.myData //this returns the value "someText"
Cyrille Con Morales
  • 918
  • 1
  • 6
  • 21
0

You can try something like this

In Form2

public void SetText(string text)
    {
        this.label1.Text = text;
    }

And In Form1

private void Form1Button1_Click(object sender, EventArgs e)
    {
        using (Form2 form2 = new Form2())
        {
            form2.SetText(this.textBox1.Text);
            form2.ShowDialog();
        }
    }

If you don't want a modal dialog then change the Form1 code like below

Form2 form2 = new Form2();
    public Form1()
    {
        InitializeComponent();
    }
private void Form1Button1_Click(object sender, EventArgs e)
    {
        form2.SetText(this.textBox1.Text);
        form2.Show();
    }
AJITH
  • 1,145
  • 7
  • 8
0
  1. Create Form1 with a label (label1) and a button (button1)
  2. Create Form2 with a textbox (textBox1)

Form1.cs

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 Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(this.SetTextFromForm2, label1.Text);
            form2.ShowDialog();
        }

        private void SetTextFromForm2(string str)
        {
            label1.Text = str;
        }
    }
}

Explain :

Form2 form2 = new Form2(this.SetTextFromForm2, label1.Text);

Send 2 informations to Form2 : method to change Text of label on Form1, and current text of that label

Form2.cs

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;

public delegate void SetTextToForm1(string str);

namespace WindowsFormsApp1
{

    public partial class Form2 : Form
    {
        public SetTextToForm1 setTextToForm1 { get; set; }

        public Form2(SetTextToForm1 SetTextMethod, string strTextFromForm1)
        {
            InitializeComponent();

            this.setTextToForm1 = SetTextMethod;
            this.textBox1.Text = strTextFromForm1;
        }

        private void TextBox1_TextChanged(object sender, EventArgs e)
        {
            setTextToForm1(textBox1.Text);
        }
    }
}

Explain :

public delegate void SetTextToForm1(string str);

"A delegate is a reference type variable that holds the reference to a method"

setTextToForm1(textBox1.Text);

Call Form1's method to change text of label on Form1


Answer your question :

This bit of code did basically exactly what I'm looking for, but do you know if theres any way to essentially reverse it? I see when you input text into the textBox in Form 2 it does put that into the label, so it works perfectly. But is there a way to have the text box in Form 1 and when you hit the button just have Form 2 pop up with the label containing the text from the text box in Form 1?

  1. Create Form1 with a textbox (textBox1) and a button (button1)
  2. Create Form2 with a label (label1)

Form1.cs

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;

public delegate void SetTextToForm2(string str);

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        Form2 form2 = new Form2();

        public SetTextToForm2 setTextToForm2 { get; set; }

        public Form1()
        {
            InitializeComponent();

            setTextToForm2 = form2.SetTextFromForm1;
        }

        private void TextBox1_TextChanged(object sender, EventArgs e)
        {
            setTextToForm2(textBox1.Text);
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            form2.Visible = !form2.Visible;
        }
    }
}

Form2.cs

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 Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public void SetTextFromForm1(string str)
        {
            label1.Text = str;
        }
    }
}
VietDD
  • 1,048
  • 2
  • 12
  • 12
  • This bit of code did basically exactly what I'm looking for, but do you know if theres any way to essentially reverse it? I see when you input text into the textBox in Form 2 it does put that into the label, so it works perfectly. But is there a way to have the text box in Form 1 and when you hit the button just have Form 2 pop up with the label containing the text from the text box in Form 1? – Miguel M Feb 19 '20 at 04:57
  • check my answer above – VietDD Feb 19 '20 at 06:01