textBox & String

J

John. S.

Hi,

1) How can I convert a Text property (textBox1->Text) or a String to an
integer?
(There is a "ToInt32()" member function but I couldn't
use it. int i = textBox1->Text.ToInt32(???);

2) I'd like to display in a loop:
abc 0 def
abc 1 def
abc 2 def
abc 3 def
abc 4 def

What I've found is:
for(int i = 0; i < 5; i++)
MessageBox::Show(String::Concat("abc ", i.ToString(), " def");

Is there an easier way to display that?
(I mean the Borland way: ShowMessage("abc " + IntToStr(i) + " def"); )

3)I'd like to add multiple lines to a textBox at runtime. The MultiLine
property of the textBox is true.
Line0
Line1
Line2
Line3...
How do I use the textBox1->Lines or textBox1->Lines->Item for this
purpose ?

Thanks...
 
B

Ben Voigt

pvdg42 said:
Addressing your first question,

http://msdn2.microsoft.com/en-us/library/sf1aw27b.aspx

Note that you must pass the string to be converted as the argument and
that the return type is int.

Oh yuck. What's the point of having namespaces and classes if you throw
everything into one huge "Convert" mess.

Use either System::Int::parse or System::Int::TryParse:

int i;
try {
i = System::Int::parse(textBox1->Text);
}
catch (FormatException^ e) {
// not a valid number
}

or

int i;
if (!System::Int::TryParse(textBox1->Text, i)) {
// not a valid number
}

The second one is more efficient unless the frequency of bad inputs is very
very very low.
 
J

John. S.

Use either System::Int::parse or System::Int::TryParse:
int i;
try {
i = System::Int::parse(textBox1->Text);
}
catch (FormatException^ e) {
// not a valid number
}

or

int i;
if (!System::Int::TryParse(textBox1->Text, i)) {
// not a valid number
}

The second one is more efficient unless the frequency of bad inputs is
very very very low.

calling
System::Int::parse(textBox1->text);
gives a compile error:
error C2039: 'Int' : is not a member of 'System'

???
 
B

Ben Voigt

John. S. said:
calling
System::Int::parse(textBox1->text);
gives a compile error:
error C2039: 'Int' : is not a member of 'System'

Sorry, it's Int32 (or Int16 or Int64 or UInt32 or ..., but Int32 corresponds
to C++'s int).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top