Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
i have two labels in different form. form 4 and form6,
which contains two int value, i want to add that int value and show up in the form4,
so i have done this in the sender form which is form6,
this code is correct and it is not generating any error, so why not the label is not updating in the form4
this code is in the button click event
<pre>private void button2_Click(object sender, EventArgs e)
     {

         Form4 fl = new Form4();
         int f;
         f = Convert.ToInt32(labelofform6.Text);
         int g;
         g = Convert.ToInt32(fl.labelofform4.Text);
        fl.label1.Text= f.ToString() + g.ToString() ;
     }
;
Posted
Updated 13-Dec-12 18:47pm
v4
Comments
[no name] 14-Dec-12 0:38am    
Can you tell us where is your label1 placed?
shaikh-adil 14-Dec-12 0:39am    
it is in the sender form which is form6
sariqkhan 14-Dec-12 0:39am    
i do not know the answer but, your label cant contain int value, it must be string value which you are saving and it must be previous int value,
shaikh-adil 14-Dec-12 0:40am    
yes you are right
Jibesh 14-Dec-12 0:44am    
please post the complete code from where the form is invoked for better answer.

fl.label1.Text= f.ToString() + g.ToString() ;

this line wont perform the mathematical addition its string addition
for eg:
C#
int f = 10;
int g = 20;

f1.label1.Text = f.ToString() + g.ToString() ;

o/p will be 1020 and not 30

For mathematical addition just add two int variable as below
C#
int f = 10;
int g = 20;

f1.label1.Text = Convert.ToString(f + g);


/EDIT


In your case Form4 f1 = new Form4() is a local variable which has an empty value or a default value for the member labelofform4.Text because you are not updating it.

second you said its not working I dont see any code to display the form4 was is not included for any purpose?

/EDIT
 
Share this answer
 
v3
Comments
shaikh-adil 14-Dec-12 0:48am    
so how to the mathematical addition?
sorry that will be a silly mistake but i am a student now, trying for learning .net
and BTW it is not giving 1020 also in the form4's label
Jibesh 14-Dec-12 0:59am    
there are lot of error in your code. You must understand what is local variable and what is member variable.

In your case Form4 f1 = new Form4() is a local variable which has an empty value or a default value for the member labelofform4.Text because you are not updating it.

second you said its not working I dont see any code to display the form4 was is not included for any purpose?

come with your default values and exepected result. do read your code once again!!!
Jibesh 14-Dec-12 0:52am    
check my updated solution. if that didnt satisfies your answer write the expected result and what result your getting.
shaikh-adil 14-Dec-12 1:02am    
thanks that was my minor fault, i was calling another object previously the calculation was done
C#
int f = 10;
int g = 20;
int sum=0;
sum=f+g;
f1.label1.Text = Sum.ToString();

//u r trying to add string f1.label1.Text = f.ToString() + g.ToString() ;
//not the actual value
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900