0

I have just a little problem with reading text from label, but...

I have two forms. Form_1, and Form_2.

Form_1 is sending (on demand) text from "label_one" directly to "label_two" in Form_2.

But in Form_2 i have another label called "label_reader" that need to show any changes done in "label_two" text.

I must (dynamically?) read any changes from "label_two" and show it in label_reader.

Never had a similar problem, and have no idea how to do that. It can't be done with the use of a button.

Any help will be greatly appreciated!

Martin
  • 79
  • 10
  • 1
    You want to register to the `TextChanged` **event** of `label_two`. – Rotem May 30 '16 at 14:47
  • Possible duplicate of [Passing Data Between Forms](http://stackoverflow.com/questions/7464625/passing-data-between-forms) – ASh May 30 '16 at 14:49
  • You should add some sample code to show what you are currently doing. But it sounds like you are updating label_two directly from Form_1. Instead, you should post the text from Label_one to a property on Form_2 - That way, you can update both labels in one operation. – Obsidian Phoenix May 30 '16 at 14:49
  • @ASh don't think it's a duplicate, as this is about two labels in the _same_ form. – René Vogt May 30 '16 at 14:50

1 Answers1

3

You can use the TextChanged event of label_two. Subscribe to that event (most likely in the constructor of your Form_2) and set the text of label_reader when the event is raised:

public partial class Form_2 : Form
{
   //...

   public Form_2()
   {
       InitializeComponent();
       // your other code

       label_two.TextChanged += label_two_TextChanged;
   }

   // the event handler
   private void label_two_TextChanged(object sender, EventArgs e)
   {
       label_reader.Text = label_two.Text; // or what ever you want to do
   }
}
René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • Thank you René, you just made my day :) so crazy simple. – Martin May 30 '16 at 14:52
  • +1; `label_two.TextChanged += (_s, _e) => label_reader.Text = label_two.Text;` is shorter and may be useful since it can't be (erroneously) removed. – Dmitry Bychenko May 30 '16 at 15:00
  • 1
    @DmitryBychenko Yes I like that one, too. But as I wasn't sure if OP is going to do some more inside the handler and how experienced s/he is, I wanted to keep things simple and not introduce other technologies all at once. – René Vogt May 30 '16 at 15:02