1

I'm trying to change the writing of a label in Form 2, when a button press occurs in Form 1.

I've tried doing this by creating an event handler, and well it works, but in the opposite way, such that when I press a button in Form 2 it changes the text of label 1.

Is this method possible in the opposite way ?

public partial class Form2 : Form
{
    Form1 myoutputform;
    public Form2(Form1 outputString)
    {
        InitializeComponent();
        myoutputform = outputString;
        textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
        button1.Click += new EventHandler(button1_Click); //create new even hadler for button pressed 
        button2.Click += new EventHandler(button2_Click);
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        myoutputform.textBox1.Text = textBox1.Text;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        myoutputform.label1.Text = button1.Text;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        myoutputform.label1.Text = button2.Text;
    }
}



namespace Form2ControlForm1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            new Form2(this).Show();
        }
    }
}
8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198
  • why don't you create 1 event handler and point button1 and button2 to the same event.. then inside of the event create a `switch() { } ` case statement based on the `sender` name a very simple approach..or create a simple Delagate, very simple as well I can post an example if you are still having issues. – MethodMan Dec 10 '17 at 14:14
  • Sorry MethodMan, I'm a bit of a noob a this. Would please post an example for me ? Thankss – Curtis Moses Dec 10 '17 at 14:37
  • let us know if the solution works for you and if you are having any more issues. take a look at some of these examples here as well for more advanced way doing it with lambda expressions https://stackoverflow.com/questions/2465040/using-lambda-expressions-for-event-handlers – MethodMan Dec 10 '17 at 14:55

0 Answers0