Click here to Skip to main content
14,329,627 members
Rate this:
Please Sign up or sign in to vote.
See more:
    public partial class Form1 : Form
        public Form1()

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

Updated 26-Jan-15 10:42am
0x01AA 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;" 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.
Ramza360 26-Jan-15 23:25pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.

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

or you can do like this:
label2.Text =  String.Format("The equivalent in feet is. {0}", result);
0x01AA 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
ZurdoDev 26-Jan-15 16:37pm
I don't understand what that means. You need to be very clear about things since we can't see anything you are doing.

For example, you said it would not execute, that could mean there is an error or it could mean it is executing but not doing what you want.

All you have to do is put a breakpoint in the code and then step through the code and watch exactly what happens line by line.
Rate this:
Please Sign up or sign in to vote.

Solution 3

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)

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100