- Create Form1 with a label (label1) and a button (button1)
- 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?
- Create Form1 with a textbox (textBox1) and a button (button1)
- 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;
}
}
}