Author Topic: VC.net - using ints in a text box.  (Read 2854 times)

Rob

  • New improved. Now with added something...
  • Jackass In Charge
  • Posts: 5959
  • Karma: +86/-149
  • Approaching 60 from the wrong damn direction...
VC.net - using ints in a text box.
« on: July 18, 2008, 03:29:26 PM »
OK, I need some C++ advise. I don't want to post this at CProg - I've seen the way they treat noobs over there!

How the hell do I use (convert?) an int as text.

Code: [Select]
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
static int count;
char *temp;

if (this->button1->Text == "Hello")
{
this->button1->Text = "OK";
count++;
}
else
{
this->button1->Text = "Hello";
count++;

}
this->textBox1->Text += "."; // I want to use count here to write
// in the text box how many times I've clicked the button
}

};

If I wanted to do this in VB I'd just use

Code: [Select]
Textbox.Text = Textbox.Text & Val(count)
Any ideas?

Dumah

  • Jackass IV
  • Posts: 960
  • Karma: +21/-6
Re: VC.net - using ints in a text box.
« Reply #1 on: July 18, 2008, 03:52:15 PM »
There's loads of ways to do this. the std function "atoi" is nice & simple

charlie

  • Jackass In Charge
  • Posts: 7903
  • Karma: +84/-53
Re: VC.net - using ints in a text box.
« Reply #2 on: July 18, 2008, 03:57:12 PM »
I think he wants int to string, not string to int.

I'd use stringstreams in general, but there is probably a better solution in .NET (which I don't use). What type is this->textBox1->Text?

Rob

  • New improved. Now with added something...
  • Jackass In Charge
  • Posts: 5959
  • Karma: +86/-149
  • Approaching 60 from the wrong damn direction...
Re: VC.net - using ints in a text box.
« Reply #3 on: July 18, 2008, 04:02:36 PM »
Correct Charlie.

>>What type is this->textBox1->Text?

A string I think.

charlie

  • Jackass In Charge
  • Posts: 7903
  • Karma: +84/-53
Re: VC.net - using ints in a text box.
« Reply #4 on: July 18, 2008, 04:08:24 PM »
std::string? System::String? char*? wchar_t*?
Code: [Select]
// std::string
std::istringstream istr;
istr << this->textBox1->Text << "." << count;
this->textBox1->Text = istr.str();
Code: [Select]
// System::string
this->textBox1->Text += ".";
this->textBox1->Text += Convert::ToString(count);
Code: [Select]
// char* - assuming it's big enough
sprintf(this->textBox1->Text, "%s.%d", this->textBox1->Text, count);
« Last Edit: July 18, 2008, 04:21:17 PM by charlie »

Rob

  • New improved. Now with added something...
  • Jackass In Charge
  • Posts: 5959
  • Karma: +86/-149
  • Approaching 60 from the wrong damn direction...
Re: VC.net - using ints in a text box.
« Reply #5 on: July 18, 2008, 04:26:47 PM »
Ouch. Dirty hack ahead :)

Code: [Select]
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
static int count;


if (this->button1->Text == "Hello")
{
this->button1->Text = "OK";
count++;
}
else
{
this->button1->Text = "Hello";
count++;

}

this->textBox1->Text = "" + count;
}

};

 :thumbsup:

Rob

  • New improved. Now with added something...
  • Jackass In Charge
  • Posts: 5959
  • Karma: +86/-149
  • Approaching 60 from the wrong damn direction...
Re: VC.net - using ints in a text box.
« Reply #6 on: July 18, 2008, 04:28:56 PM »
std::string? System::String? char*? wchar_t*?
Code: [Select]
// std::string
std::istringstream istr;
istr << this->textBox1->Text << "." << count;
this->textBox1->Text = istr.str();
Code: [Select]
// System::string
this->textBox1->Text += ".";
this->textBox1->Text += Convert::ToString(count);
Code: [Select]
// char* - assuming it's big enough
sprintf(this->textBox1->Text, "%s.%d", this->textBox1->Text, count);


This one...

Code: [Select]
// System::string
this->textBox1->Text += ".";
this->textBox1->Text += Convert::ToString(count);

Cheers Charlie.

Rob

  • New improved. Now with added something...
  • Jackass In Charge
  • Posts: 5959
  • Karma: +86/-149
  • Approaching 60 from the wrong damn direction...
Re: VC.net - using ints in a text box.
« Reply #7 on: July 18, 2008, 04:33:07 PM »
Woot!

Code: [Select]
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
static int count;


if (this->button1->Text == "Hello")
{
this->button1->Text = "OK";
count++;
}
else
{
this->button1->Text = "Hello";
count++;

}

this->textBox1->Text = "You pressed the button " + Convert::ToString(count) + " times!";
}

};