0

I created a usercontrol that contains many buttons and in the main form I have a textbox. I add the usercontrol to the main form and I want to click any button on the usercontrol and have the textbox in the main form shows the button text. The question is how to pass the string of the button in usercontrol to the textbox in the main form? This is what I'm trying to do

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();

    }
    public string a ;




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

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

    private void button3_Click(object sender, EventArgs e)
    {
        a = button3.Text;


    }

and the main form code is :

 private void textBox1_TextChanged(object sender, EventArgs e)
    {
        textBox1.Text = usrCtrl.a;
        // usrCtrl come from : Usercontrol1 usrCtrl = new Usercontrol1();
    }

and it shows nothing in the textbox.

StephenTG
  • 2,579
  • 6
  • 26
  • 36
elie mardelly
  • 79
  • 2
  • 10

2 Answers2

2

refer to this answer, you need to create a property changed event.

UserControl.cs class;

public partial class UserControl1 : UserControl
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public UserControl1()
        {
            InitializeComponent();
        }

        private string stringA;

        public string a
        {
            get { return stringA; }
            set
            {
                if (value != stringA)
                {
                    stringA = value;
                    if (PropertyChanged!= null)
                    {
                       PropertyChanged(this, new PropertyChangedEventArgs(a));
                    }
                }
            }
        }

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

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

        private void button3_Click(object sender, EventArgs e)
        {
            a = button3.Text;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            a = button4.Text;
        }
    }

On Form's Load we need to define the event,

 private void Form1_Load(object sender, EventArgs e)
        {
            cntr.PropertyChanged += Cntr_PropertyChanged; // press tab + tab after += and it will generate the following method automatically.
        }

Here is Event;

 private void Cntr_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            textBox1.Text = cntr.a.ToString(); //cntr is the instance of UserControl1

        }

Hope helps,

Community
  • 1
  • 1
Berkay Yaylacı
  • 4,383
  • 2
  • 20
  • 37
  • For something like this it really wouldn't be appropriate to create a general "any property changed" event, but rather what he wants is a "the `a` property has changed" event. The caller is interested in observing only that property changing, and the class doesn't necessarily need to signal changes to all other properties it might have. It also simplifies the code a decent bit. – Servy Sep 09 '16 at 18:49
  • @Servy I guess OnPropertyChanged, an If statement will solve it. `if (propertyName == "a")`. What do you think? – Berkay Yaylacı Sep 09 '16 at 18:54
  • 1
    It would work, but the point is it's a lot of overhead that's simply unnecessary. It's more work on the caller, the user control, other properties that don't actually need to be observed, etc. There's simply no need for it here. Everything will be much simpler and easier if you don't try to overgeneralize. – Servy Sep 09 '16 at 18:56
  • thanks man ... i dnt understand it well .. but its worked :D so thank u :) – elie mardelly Sep 09 '16 at 19:10
  • @Servy Thanks for the points, tried to simplify. Really helpful thanks. – Berkay Yaylacı Sep 09 '16 at 19:14
  • @eliemardelly You need to have a event to notice when the "a property" has changed. Because In that situation you have usercontrol and a form, usercontrol sets the property but form doesn't know it. So I add a event and that event will tell form that this property got changes. (Please update your code, I updated the answer and please don't forget to mark the answer If it is the solution) – Berkay Yaylacı Sep 09 '16 at 19:17
0

Your code to change the textBox1.Text value is in the wrong event handler.

The textBox1_TextChanged event handler only fires when text in that field changes.

What you need to do is put the line:

textBox1.Text = a;

in the click event handlers.

Jay
  • 128
  • 3