How to update a label

P

Pablo Czyscas

Hi, my question is how to update tha value in a labe (label1)l when i click
in a button ( button1 or button2 ), like this code:
public partial class Form1 : Form

{

private static string variable;

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

label1.Text = variable;

}

private void button1_Click(object sender, EventArgs e)

{

variable = "a";

}

private void button2_Click(object sender, EventArgs e)

{

variable = "b";

}

private void button3_Click(object sender, EventArgs e)

{

MessageBox.Show(variable);

}

}



thanks a lot!

Pablo
 
C

Chris Tacke, eMVP

You have to tell it to update it - you can't just set the variable.
Something like this maybe?

private void button1_Click(object sender, EventArgs e)
{
variable = "a";
UpdateLabel();
}

private void button2_Click(object sender, EventArgs e)
{
variable = "b";
UpdateLabel()
}

private void UpdateLabel()
{
label1.Text = variable;
}
 
P

Pablo Czyscas

Thanks Chris, but what about if the label1 is in another form ? Let me give
you and example: i have 2 forms, form1 and form2...form1 show form2 and when
i click the button1 that is in form2, i´d like to update the label1 that is
in form1 ?

Thanks a lot in advance, and sorry for my small knowledge...
 
C

Chris Tacke, eMVP

This is a basic OOP concept. You need to call to the label through the
FOrm1 instance. I recommend you go pick up an intro C# book - preferrably
one that covers WinForms - and walk through it as it will cover this.


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Giving back to the embedded community
http://community.OpenNETCF.com
 
A

Armando Rocha

Hi,
You can update label using "global settings" in your solution.

like this:
public class cAppSettings {
//Declare your variables
public static string variable {get; set;}
}


in form1 (where you have de label) you can put a event handler

this.Activated += new EventHandler(form1_Activated);

void form1_Activated(object sender, EventArgs e)
{
UpdateLabel();
}

private void UpdateLabel()
{
label1.Text = cAppSettings.variable;
}


in form2 when you click button2

private void button2_Click(object sender, EventArgs e)
{
cAppSettings.variable = "b";
}


when you close form2 the form1 run void rm1_Activated and Update your label.

Hi hope this help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top