I can't figure out how to convert my int value back to a string for use in a textbox. I already pulled it out converted it to int, did the equation now I just want to flip it back to display in a textbox.

    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

                int cost = Convert::ToInt32(costbuy->Text);
                int amount = Convert::ToInt32(amountb->Text);
                int costnow = Convert::ToInt32(nowcost->Text);
                int frontcost = cost*amount;
                int endcost = costnow*amount;
                int makebreak = endcost-frontcost;
                string makebreak1 = makebreak.ToString();
                ;
                end->Text = makebreak;

             }

It's a stock value calculator if that helps make sense of the code.

Dani AI

Generated

, nailed the core issue: you are in C++/CLI, so int is System::Int32 and ToString() returns a managed System::String^, which is exactly what a WinForms TextBox needs. The simplest fix is to assign the formatted value straight to the control:

end->Text = makebreak.ToString();

That compiles because TextBox::Text is a string property. See the WinForms TextBox overview for context: TextBox control overview.

To make your code resilient, avoid Convert::ToInt32(...) on user input because it throws on invalid text. Use Int32::TryParse and fail fast if any field is not a whole number:

int cost, amount, costnow;
if (!Int32::TryParse(costbuy->Text, cost) ||
    !Int32::TryParse(amountb->Text, amount) ||
    !Int32::TryParse(nowcost->Text, costnow)) {
    end->Text = "Enter valid whole numbers.";
    return;
}

int makebreak = (costnow * amount) - (cost * amount);
end->Text = makebreak.ToString();

Docs: Int32.TryParse.

For a nicer display, format the result:

end->Text = makebreak.ToString("C", System::Globalization::CultureInfo::CurrentCulture);  // currency
// or:
end->Text = makebreak.ToString("N0");  // thousands separators, no decimals

Format specifiers reference: Standard numeric format strings.

One more tip for stock prices: if you later allow cents, switch cost/nowcost to System::Decimal (and parse with Decimal::TryParse or Convert::ToDecimal) to avoid binary floating-point rounding; Decimal is designed for financial calculations. Reference: System.Decimal.

Recommended Answers

All 3 Replies

Looking at this page in MSDN I believe you want to use either Convert::ToString(makebreak) or the equivalent Int32::ToString(makebreak).

It is probably a simply typo. The string conversion produces a .NET string of type System::String^, not a C++ string of type std::string. If you use the lower-case name string, my guess is that it refers to std::string, which is not the same as System::String^. This should work:

System::String^ makebreak1 = makebreak.ToString();

If you want a C++ string, then you will have to marshal the .NET String.

It is probably a simply typo. The string conversion produces a .NET string of type System::String^, not a C++ string of type std::string. If you use the lower-case name string, my guess is that it refers to std::string, which is not the same as System::String^. This should work:

System::String^ makebreak1 = makebreak.ToString();

If you want a C++ string, then you will have to marshal the .NET String.

Absolutely beautiful. Thanks mike, and everyone else who contributed as well! Helping me understand more and more.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.