Click here to Skip to main content
15,883,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do i convert a labels text into a number,, this text will only be numbers, this is my code.

C#
private void ShortCut_Load(object sender, EventArgs e)
     {
         string variable = form2size.Text;
         int value;
         int.TryParse(variable, out value);

         this.Size = new Size(this.Size.Width + value, 130);
     }
Posted

Yes it look correct. And what is the problem then ?
Int32.TryParse[^] will return true when convert successed
 
Share this answer
 
Comments
ambarishtv 14-May-11 3:43am    
this is the answer, my 5 :)
Kim Togo 14-May-11 3:50am    
Thanks :-) - But OP, already has the solution. The code will work.
Use Exception Handling to handle the error....

C#
private void ShortCut_Load(object sender, EventArgs e)
{
         int value = 0;
         try
         {
                  value = Convert.ToInt32(form2size.Text);
         }
         catch
         {}
         this.Size = new Size(this.Size.Width + value, 130);
}
 
Share this answer
 
Comments
[no name] 15-May-11 0:31am    
for some reason this code isn't working, on the other form the label.text = "900"; and that made the form2.label = "900"; and then we converted the text in form2 to a value, and then i made it so form2's width plus the value that is in the textbox, so the form2's width should be longer, but its not!?. whats the problem with the code?
String Converted= int.Parse(label1.Text);

Will help you.
 
Share this answer
 
v2
HI U can us this simple code

int value = Convert.ToInt32(label.Text.ToString().Trim());
 
Share this answer
 
v2
Comments
Kim Togo 14-May-11 3:38am    
No you cannot. There is no String.ToInt() method under String class http://msdn.microsoft.com/en-us/library/system.string_methods.aspx.

And there is no point in converting label.Text to string. label.Text is already a string.
abdul wahab.o 14-May-11 3:39am    
Try this still if u have any problm.
I am sure it will work 100 % successfully
Kim Togo 14-May-11 3:44am    
Mush better now :-)
But still. No need to call .ToString() on a label.Text.
label.Text is already a String.

Just do "int value = Convert.ToInt32(label.Text.Trim());"
abdul wahab.o 14-May-11 3:50am    
Thanks dear

i forgot to remove that f***ing thing from my code
sory

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