Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void convertButton_Click(object sender, EventArgs e)
        {
          
            const int FEET = 5820;
            string label2;
            int result;
            int miles;

            miles = Convert.ToInt32 (textBox1.Text);
            result = miles * FEET;
            

            label2 =  ("The equivalent in feet is. "  + result);

        }
    }
}
Posted
Updated 26-Jan-15 10:42am
v2
Comments
[no name] 26-Jan-15 13:57pm    
What are you missing? To see a result in the form? If yes have a look to your "string label2;"....it is a string and not a Label.
Ramza360 26-Jan-15 14:44pm    
my guess is that whatever is in the textBox1.Text property is not parsable to an int 32, or the convertButton_Click has not been subscribed with the actual button click event. Should show the error however. I take your code out of the block and input a simple digit, miles = 10, runs perfectly.

I would suggest doing int.TryParse on the textbox text still..
Sergey Alexandrovich Kryukov 26-Jan-15 23:23pm    
Inquirer should use int.TryParse instead.
—SA
Ramza360 26-Jan-15 23:25pm    
Agreed

1. If code won't execute first start with the error. It will tell you exactly what the problem is.
2. Just at a quick glance you are trying to set a label to a string. You want to set the .Text of the label2.

C#
label2.Text =  "The equivalent in feet is. "  + result;


or you can do like this:
C#
label2.Text =  String.Format("The equivalent in feet is. {0}", result);
 
Share this answer
 
Comments
[no name] 26-Jan-15 13:59pm    
label2 is declared as string :-)
ZurdoDev 26-Jan-15 14:00pm    
Ah, that it is. That's weird.

I did miss that. Thanks.
Member 11403574 26-Jan-15 14:24pm    
label2.Text = "The equivalent in feet is. " + result;



The word 'Text' is underlined and say's ...'string' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'string' could be found (are you missing or using a directive or an assembly reference?)
I don't know what that means.
ZurdoDev 26-Jan-15 15:11pm    
It means what Bruno said, label2 is not a label, it is a string. My mistake.

So, what is your original error?
Member 11403574 26-Jan-15 16:18pm    
Actually the real problem is I can't get the button to react
Your code doesn't produce observable effects for the user: you are assigning a value to a local string. Try to assign the same value to a control (for instance to a TextBox.Text property)
 
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