PC Review
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Compact Framework
How to update a label
Forums
Newsgroups
Microsoft DotNet
Microsoft Dot NET Compact Framework
How to update a label
![]() |
How to update a label |
|
|
Thread Tools | Rate Thread |
|
|
#1 |
|
Guest
Posts: n/a
|
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 |
|
|
|
#2 |
|
Guest
Posts: n/a
|
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; } "Pablo Czyscas" <turton_1@hotmail.com> wrote in message news:%23Ar7dUvnIHA.5472@TK2MSFTNGP03.phx.gbl... > 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 > > |
|
|
|
#3 |
|
Guest
Posts: n/a
|
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... "Chris Tacke, eMVP" <ctacke.at.opennetcf.dot.com> escribió en el mensaje news:eoIy7WvnIHA.4744@TK2MSFTNGP06.phx.gbl... > 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; > } > > "Pablo Czyscas" <turton_1@hotmail.com> wrote in message > news:%23Ar7dUvnIHA.5472@TK2MSFTNGP03.phx.gbl... >> 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 >> >> > > |
|
|
|
#4 |
|
Guest
Posts: n/a
|
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 "Pablo Czyscas" <turton_1@hotmail.com> wrote in message news:%23it7oAwnIHA.4708@TK2MSFTNGP02.phx.gbl... > 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... > > > > "Chris Tacke, eMVP" <ctacke.at.opennetcf.dot.com> escribió en el mensaje > news:eoIy7WvnIHA.4744@TK2MSFTNGP06.phx.gbl... >> 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; >> } >> >> "Pablo Czyscas" <turton_1@hotmail.com> wrote in message >> news:%23Ar7dUvnIHA.5472@TK2MSFTNGP03.phx.gbl... >>> 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 >>> >>> >> >> > > |
|
|
|
#5 |
|
Guest
Posts: n/a
|
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. -- Armando Rocha Mobile Developer http://www.ifthensoftware.com "Chris Tacke, eMVP" <ctacke.at.opennetcf.dot.com> escreveu na mensagem news:e8%23xHVwnIHA.4036@TK2MSFTNGP05.phx.gbl... > 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 > > > "Pablo Czyscas" <turton_1@hotmail.com> wrote in message > news:%23it7oAwnIHA.4708@TK2MSFTNGP02.phx.gbl... >> 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... >> >> >> >> "Chris Tacke, eMVP" <ctacke.at.opennetcf.dot.com> escribió en el mensaje >> news:eoIy7WvnIHA.4744@TK2MSFTNGP06.phx.gbl... >>> 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; >>> } >>> >>> "Pablo Czyscas" <turton_1@hotmail.com> wrote in message >>> news:%23Ar7dUvnIHA.5472@TK2MSFTNGP03.phx.gbl... >>>> 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 >>>> >>>> >>> >>> >> >> > > |
|
![]() |
|
| Thread Tools | |
| Rate This Thread | |
|
|

Main Page 

