I have a (I thought:confused:) relatively simple converter project I am writing with Visual Studio 2010 C++ Windows Form App. So to be forthwith I am completely stumped as to how I would get the user input from the Amount box (1234.12 for example) and the Rate box (1.04108 for example) to call this math method Total = (1/ 1.04108) * 1234.12 and display the results in the Total box…?? This whole project is just to learn more about C++ and Windows Forms Apps, any guidance or inspiration to an aspiring programmer would be greatly welcomed. I also attached a screen shot of the application design. (If it helps)

Thank you for reading

Anatake

You have text representations of the numbers for each of the boxes. Use the Double::TryParse to get the value. Look up the syntax of it as it uses an "out" variable to return the double.

Everything in .NET GUI programming is governed by events. Can you think of a means to signal that the calculation should be done (like how would you do the equivalent on a pocket calculator)?

Take a run at it and post back with any questions.

Edited 6 Years Ago by jonsca: n/a

Thank you jonsca for your insightful response! I researched the Double::TryParse and I believe I understand how it works. Although I am unable to figure out how to implement it?

I should have also stated this was is my attempt at writing a "Forms App" and it's confusing the heck out of me. :)

As for your question on how I would signal the conversion to begin; I assumed I could use the "Convert" button to call the event?
Thank you again for your assistance!

Thank you jonsca for your insightful response! I researched the Double::TryParse and I believe I understand how it works. Although I am unable to figure out how to implement it?

Double::Parse will throw an exception if there is no valid conversion between the string and a double. TryParse returns true if the value converts. I misspoke earlier calling the parameter an out variable (which is what it is considered in C#). Basically create a double variable and pass that in as the second parameter: Double::TryParse(stringtoconvert,doubletoacceptvalue) and you can put it in an if statement to make sure you are really getting your double.

So in your case the string to convert will be textBoxWhatever.Text

I should have also stated this was is my attempt at writing a "Forms App" and it's confusing the heck out of me. :)

As for your question on how I would signal the conversion to begin; I assumed I could use the "Convert" button to call the event?
Thank you again for your assistance!

No worries. It takes some getting used to. Luckily a lot of the dirty work is taken care of behind the scenes.

Yes, you're correct, so your formula will go into that ConvertButton_click event (or whatever the name of your button is).

Edited 6 Years Ago by jonsca: smiley problems

Comments
Thank you for helping a newcomer!

Thank you again for assisting me. I thought I had it at one point, but I am still unable to figure this out. Below is my crazy code for converting.

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
        // Converting Method
	float rate, result;
	double value;
        char total;

	switch(total)
    {
    case '0':
	 rate = 1.04108;
         result = (1 / rate) * value;
         break;

    case '1':
	 rate = 12.704;
         result = (1 / rate) * value;
         break;

    case '2':
	 rate = 0.66992;
         result = (1 / rate) * value;
         break;

    case '3':
	 rate = 89.4664;
         result = (1 / rate) * value;
         break;

    case '4':
	 rate = 0.813405;
         result = (1 / rate) * value;
         break;

    case '5':
	 rate = 30.8224;
         result = (1 / rate) * value;
         break;

    default:
	 rate = 0.0;
         return 0;
   }
		 }
public:
	// Variable for converting string to integer
	double value;
	Double::TryParse(textBox1.Text,value)

Could you recommend anything that I could get a good beginners look at or reading on creating Windows Forms Apps?
I have been working on this for the last four days non-stop and find myself drawing blanks... Perhaps I should go back to C# or Ruby for this.

Thank you again jonsca!

Anatake

I have heard (I have not read it) that Ivor Horton's Beginning Visual C++ 2010 (or the 2008 edition) has a fair amount of C++/CLI. I've never found anything satisfactory in the tutorial department for CLR Winforms (I've adapted my knowledge from C#).

One thing I see in the above code is that total is undefined when you get to the switch statment.

You don't need to put "value" as public (and that version would be masked by the one in the click handler anyway) unless you need it elsewhere in your code, in which case only declare it as a variable of the Form1 class. The TryParse should be called right in the click handler too. You don't need to calculate result within each case of the switch statement, you can postpone it until the end.

A consideration: I would use all doubles instead of making the other two floats. You run into danger of losing some precision in the conversion.

Edited 6 Years Ago by jonsca: n/a

Thank you jonsca!! I finally got this little App to work thanks to your guidance; I must also thank you for not simply giving me the answer. One truly does learn more by figuring it out themselves. :) I would like to ask if you would mind reviewing my following code for any errors or discrepancies. Additionally, how would I go about making the textbox3 output have a dollar sign($) added before the result?

private: System::Void textBox3_TextChanged(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void textBox2_TextChanged(System::Object^  sender, System::EventArgs^  e) {
		 }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			 //Signal Conversion
			 double value, rate, result;
			 value = System::Double::Parse(textBox1->Text); // Convert to a double from a string
			 rate = System::Double::Parse(textBox2->Text); // Convert to a double from a string
			 result = (1 / rate) * value;
			 textBox3->Text = result.ToString(); // Convert back to a string
		 }
private:
	// Variable for converting string to integer
	double value;
};
}

You're very welcome.

You don't need the TextChanged handlers for the 3 textboxes as they are not doing anything at present. TextChanged means the method will run with each character you are typing for example (or when you backspace, etc).

Have value be either a private member variable of your class -or- a local variable to your method, but not both. If you need "value" elsewhere in your class keep the private member variable. If not, just have it be local like rate and result.

To place the dollar sign in front just do textBox3->Text = "$" + result.ToString(); The only other thing is Parse, but I explained that one already. If you type bob into the textbox the program is going to throw an exception.

I guess I don't understand the Double::TryParse after all. When using this code below, I apparently am not getting the right double back as you stated. Using the above code works great until a character is input as you also stated. :) So how do I go about getting my proper "value" back?

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			 //Signal Conversion
			 double rate, result;
			 value = System::Double::TryParse(textBox1->Text, value); // Convert to a double from a string
			 rate = System::Double::Parse(textBox2->Text); // Convert to a double from a string
			 result = (1 / rate) * value;
			 textBox3->Text = "$" + result.ToString(); // Convert back to a string
		 }

Thank you

Anatake

TryParse returns a bool as to whether or not the conversion was successful it might look something like this

using namespace System; //up at the top
if (Double::TryParse(textBox1->Text,value) && Double::TryParse(textBox2->Text, rate))
{
  result = (1/rate) * value;
  textBox3->Text = "$"+result.ToString();
}
else
  textBox3->Text = "Unable to convert parameters -- ERROR";

Or something similar to that (e.g. instead of the error message, set value to 0 and rate to 1 and print a message out in the debug)

This question has already been answered. Start a new discussion instead.