Jump to content

I finished watching all the Coding 101 episodes, and jumped into making a GUI program


G+_J. Peter Haliburton
 Share

Recommended Posts

I'm stuck. Trying to figure out how to read the variable from one text box and then write the calculation result back into another box. It doesn't seem to like anything I did to make it work in the console.

 

Quit button is working, but no solutions on the page refresh yet. I'll pound away at it a bit longer before officially asking for help.

Link to comment
Share on other sites

Double click on the calculate button to get up that section of code. Here I am going to assume the box that says length is called TextBox1.

 

The text boxs can only be strings so you need some conversions so play something similar to this.

 

string lengthInput = TextBox1.Text;

(This takes the text thats in the text box and puts it in to a string for you)

 

int lengthInt = Int32.Parse(lengthInput);

(change this to a float or decimal if needed of course)

 

Now you have your number so also add your calculation code in the same button section youre currently working in. I now assume you have a total value which I will assume is an int called total. Im also assuming calculated textbox is called TextBox2. So you need to change the total to a string and output it to the box like this:

 

TextBox2.Text = total.ToString();

(This simply says what ever is in the second text box equals your total int but in string format.

 

So when you click the calculate button it will take the text from the first box, convert it to an int, float or decimal depending on what your calculation is. Then runs your calculation which you should have a total value which you then convert to a string and it displays in the second box.

Link to comment
Share on other sites

Ah I see where youve went wrong. Youve did the conversion of whats in the text box from within the text box code rather than as part of the button function. I will put up my code and you can compare them. You were not far off.

Link to comment
Share on other sites

Yeah there were some text change area put in there thats usually put in if you double click on the text boxes. I just removed those and then put all the code within the button click. 

 

I see if kind of like an if statement. If button is clicked then run this code. If button is clicked, take text from box, change to double, do calculation, change to string and put in other text box.

 

Now try and think what you can add to your program for your next challenge :)

Link to comment
Share on other sites

Thanks to Dean T, my program is well underway. It now also checks the input, and writes an error message, if the entry is not what is expected.

 

        private void Do_Calculation(object sender, RoutedEventArgs e)

        {

            // lwl is a double for use in the sqrt function

            double lwl;

            // Check that provided entry is a decimal number

            bool testIn = double.TryParse(lwlIn_Box.Text, out lwl);

            if (testIn == false)

            {

                error_Alert.Text = ("That was not an acceptable number! Try again.");

            }

 

            // Calculate the maximum speed of the boat rounded to 2 decimal places.

            double speed = Math.Round(1.34 * Math.Sqrt(lwl), 2);

 

            // Write to speed_Box the calculated speed in a string format

            speed_Box.Text = speed.ToString();

        }

 

        private void Refresh_Screen(object sender, RoutedEventArgs e)

        {

            lwlIn_Box.Clear();

            speed_Box.Clear();

            error_Alert.Text = ("");

        }

        

        private void Quit_Application(object sender, RoutedEventArgs e)

        {

            this.Close();

        }

Link to comment
Share on other sites

 Share

×
×
  • Create New...