Donovan
Donovan

Reputation: 81

Change Label text on button click in a different form

I've 3 buttons, for this case call them button 1, 2 and 3. Then I have a different form in the same solution, with an label. What I want to have is when each button is pressed, the label.text needs to change on the other form.

Like for an example: When: - Button 1 clicked: Label Text = 1 - Button 2 clicked: Label Text = 2 - Button 3 clicked: Label Text = 3

Upvotes: 1

Views: 2562

Answers (3)

Steve
Steve

Reputation: 216353

You can use the Application.OpenForms collection and add a simple method to each of your forms that requires the change

In the button click add

foreach (Form f in Application.OpenForms)
{
    if(f Is Form1)
        (f as Form1).ChangeLabel("NewText 1");
    else if (f is Form2)
        (f as Form2).ChangeLabel("NewText 2");
    else if (f is Form3))
        (f as Form3).ChangeLabel("NewText 3");
}

In each form that you want to change add (example for Form1)

.... other code inside your Form1,Form2,Form3 classes...
public void ChangeLabel(string newText)
{
    this.Label1.Text = newText;
}
.... other code inside your Form1,Form2,Form3 classes...

(Of course you need to change Form1,Form2,Form3 to the real names of the classes that implements your forms and the Label1 to the exact name of the label to change)

You can also make your labels public and use a syntax like

(f as Form1).Label1 = "New Text 1";

but I usally prefer to leave the controls hosted by the form not visible from external code.

EDIT

Looking at the comment below I understand that you have one forms with three buttons and one form with one label. And you want to change the label when you click on these buttons

Then the code would be the following (Set each button Click event to the same event handler using the designer. In this example I will call it ChangeLabel_Click

protected void ChangeLabel_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    SecondFormType f = Application.OpenForms.OfType<SecondFormType>().FirstOrDefault();
    if(f != null)
    {
        if(btn.Name == "Button1")
           f.ChangeLabel("NewText 1");
        else if(btn.Name == "Button2")
           f.ChangeLabel("NewText 2");
        else if(btn.Name == "Button3")
           f.ChangeLabel("NewText 3");
    }
}

EDIT 2

The code above takes the first form of the SecondType so, if you have more than one instance opened of the form SecondType you need a loop to change every form instance

protected void ChangeLabel_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    foreach(SecondFormType f in Application.OpenForms.OfType<SecondFormType>())
    {
        if(btn.Name == "Button1")
           f.ChangeLabel("NewText 1");
        else if(btn.Name == "Button2")
           f.ChangeLabel("NewText 2");
        else if(btn.Name == "Button3")
           f.ChangeLabel("NewText 3");
    }
}

Upvotes: 3

user5747953
user5747953

Reputation:

you can use a delegate event like this :

in your Form with your Buttons create a DelegateEvent

//Create a delegate
    public delegate void ButtonClickToOtherForm(object sender, EventArgs e);

    public partial class Form2 : Form
    {
        //Your own event based on created delegate
        public event ButtonClickToMainForm ButtonClickedToMainForm;

        public Form2()
        {
            InitializeComponent();
        }

       //This method will invoke your event
        private void OnButtonClickedToOtherForm(object sender, EventArgs e)
        {
            ButtonClickedToOtherForm?.Invoke(this, e);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //On button1_Click this will fire the event on the other form
            OnButtonClickedToMainForm(this, e);
        }

and in your Form with the Labels subscribe to that event

public Form1()
        {
            InitializeComponent();
            //Subscribe to event from your other Form
            Form2.ButtonClickedToOtherForm += Form2_ButtonClickedToOtherForm;
        }

        //Button on Form2 has been clicked
        private void Form2_ButtonClickedToMainForm(object sender, EventArgs e)
        {
            //change your Label Text here...
        }

Upvotes: 0

Peter
Peter

Reputation: 1

I would use some jQuery for this

$("#button1").on('click', function(){
   $("#label").empty().append("1");
});

$("#button2").on('click', function(){
   $("#label").empty().append("2");
});

$("#button3").on('click', function(){
   $("#label").empty().append("3");
});

and your label in html should be something like

<label id="label">Text</label>

probably missed something but given the lack of language i hope it gives an idea about what you want to do :)

Upvotes: 0

Related Questions