| View previous topic :: View next topic |
| Author |
Message |
sotaukko Cheater
Reputation: 0
Joined: 23 Oct 2007 Posts: 32
|
Posted: Wed Apr 16, 2008 1:42 pm Post subject: Question about C# |
|
|
This is my question: I have label1 with number 1, label2 with number 1 and label3 in my form. The thing i want to do is that label3 shows how much is label1.text + label2.text so label3 should show 2 becouse the text of label1 is 1 and text of label 2 is 1. I tried google but didn't found answer. I also tried microsofts developer center but didn't found answer.
P.S
i tried:
| Code: | int i = label1.text
int s = label2.text
label3.text = i+s |
but i got error that type string cannot be converted to type int
|
|
| Back to top |
|
 |
Anden100 Grandmaster Cheater
Reputation: 0
Joined: 20 Apr 2007 Posts: 668
|
Posted: Wed Apr 16, 2008 1:59 pm Post subject: |
|
|
| As far as i know, labels store their values in a string format, and since you want integer to add the two numbers together, you will need to convert the labels, dont know how you do this in C#...
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Wed Apr 16, 2008 2:10 pm Post subject: |
|
|
There are several ways in getting the number from text. Here is an example
| Code: |
int i = int.Parse(label1.Text);
int s = int.Parse(label2.Text);
int total = i + s;
label3.Text = total.ToString();
|
You could also use Convert.ToInt32().
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Wed Apr 16, 2008 2:11 pm Post subject: |
|
|
Ok, I'm assuming that what you want to do is take the number of characters in labels 1 and 2, and show that in label 3.
| Code: |
int length = label1.Text.Length + label2.Text.Length;
label3.Text = length.ToString();
|
_________________
|
|
| Back to top |
|
 |
Estx Expert Cheater
Reputation: 0
Joined: 04 Mar 2008 Posts: 172
|
Posted: Wed Apr 16, 2008 8:41 pm Post subject: |
|
|
What killersamurai said was correct (samuri25404, he wants to add the values from label1 and label2, not the length - I could be wrong though).
Unless you're going to use the variables again, do this instead: | Code: | | label3.Text = (int.Parse(label1.Text) + int.Parse(label2.Text)).ToString(); |
|
|
| Back to top |
|
 |
ssaccount Master Cheater
Reputation: 0
Joined: 29 Aug 2007 Posts: 391 Location: Finland
|
Posted: Wed Apr 16, 2008 11:55 pm Post subject: |
|
|
those works for me so they have to work for you
_________________
Programming in C++
(Or at least trying to ) |
|
| Back to top |
|
 |
|