PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Compact Framework How to update a label

Reply

How to update a label

 
Thread Tools Rate Thread
Old 15-04-2008, 01:35 PM   #1
Pablo Czyscas
Guest
 
Posts: n/a
Default How to update a label


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


  Reply With Quote
Old 15-04-2008, 01:39 PM   #2
Chris Tacke, eMVP
Guest
 
Posts: n/a
Default Re: How to update a label

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
>
>



  Reply With Quote
Old 15-04-2008, 02:54 PM   #3
Pablo Czyscas
Guest
 
Posts: n/a
Default Re: How to update a label

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
>>
>>

>
>



  Reply With Quote
Old 15-04-2008, 03:30 PM   #4
Chris Tacke, eMVP
Guest
 
Posts: n/a
Default Re: How to update a label

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
>>>
>>>

>>
>>

>
>



  Reply With Quote
Old 15-04-2008, 04:36 PM   #5
Armando Rocha
Guest
 
Posts: n/a
Default Re: How to update a label

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
>>>>
>>>>
>>>
>>>

>>
>>

>
>


  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off