loop

how can i loop this?
1
2
3
textBox1->Text = "1";
textBox1->Text = "2";
textBox1->Text = "3";

this is just 3 lines. i plan on making over 100 lines loop.
i just want it to loop every 100 milliseconds or so.
I would use a well known function that coverts int to string.

1
2
3
4
5
6
7
8
template <typename TYPE>
std::string to_string(TYPE value)
{
	std::ostringstream s;
	s << value;

	return s.str();
}


and use it to assign objects:

1
2
3
4
for(int i = 0; i < 100; i++)
{
        textBox1->Text = to_string(i);
}



This should work for ANSI strings, if you need functionality for UNICODE strings here are 2 more functions overloads that convert ANSI to UNICODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
std::wstring to_wide(std::string param)
{
	const size_t in_size = strlen(param.c_str()) + 1;

	size_t out_size;
	std::wstring wc(in_size, L'#');

	mbstowcs_s(&out_size, &wc[0], in_size, param.c_str(), in_size - 1);

	return wc.c_str();
}

std::wstring to_wide(const char* param)
{
	const size_t in_size = strlen(param) + 1;

	size_t out_size;
	std::wstring wc(in_size, L'#');

	mbstowcs_s(&out_size, &wc[0], in_size, param, in_size - 1);

	return wc.c_str();
}


which you would use if textBox::Text is wchar_t:
1
2
3
4
for(int i = 0; i < 100; i++)
{
        textBox1->Text = to_wide(to_string(i));
}
a timer didn't work for me. it would stay at textBox1->Text = "3" instead of looping 1-3 but ill try hammerman's idea
You need to have a variable in your class, maybe called count, initialized to 0 in the constructor.
When the timer fires you increment it and set the textbox to the new value.

but ill try hammerman's idea
It has nothing to do with the problem of incrementing the number, but also you can't assign a widestring to a System::String.
In C++ / CLR it's best to have an amnesia and forget std::string. System::String has everything you need and more.
i also need to do this with letters, not just numbers
Same priciple. You can either have a string and use a number as index or convert your int to a char.
timer should definitely be better approach, since my suggestion is probably too much overhead for fast redraw.

it's worth noting that timer usage isn't straightforward, there are multiple methods of using a timer.
okay i got it working now
i got it to increment, but i have no idea on how to make it stop incrementing when textBox1->Text equals textBox2->Text;
i want it something like
1
2
3
4
5
6
7
8
9
10
int count = 0;
textBox1 = random_number;
if (!textBox1->Text->Equals(textBox2->Text))
{
textBox1->Text = Convert::ToString(count++);
}
if (textBox1->Text->Equals(textBox2->Text))
{
stop count++;
}
Last edited on
here's the actual code i have
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
		String^ buffer = web.DownloadString(//pastebin link);
		String^ sep = ",";
		cli::array<String^> ^lines = buffer->Split(sep->ToCharArray());
		int count = 0;
	private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
		String^MyString = gcnew String(lib->getString().c_str());
		textBox1->Text = MyString;//MyString is a random string
		if (!textBox1->Text->Equals(textBox2->Text))
		{
			textBox2->Text = Convert::ToString(lines[count++]);
		}
		if (textBox1->Text->Equals(textBox2->Text))
		{
			if (checkBox2->Checked)
			{
				//do something
			}
		}
	}
1
2
3
4
if (textBox1->Text->Equals(textBox2->Text))
{
stop count++;
}

You could just disable the timer
oh wow what a simple solution, thanks again
for some reason it keeps incrementing
1
2
3
4
5
6
7
8
9
10
11
12
13
int count = 0;
private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
		String^MyString = gcnew String(lib->getIGN().c_str());
		textBox1->Text = MyString;
		textBox2->Text = lines[count];
		if (textBox1->Text->Equals(textBox2->Text))
		{
			timer2->Enabled = false;
		}
	}
	private: System::Void timer2_Tick(System::Object^  sender, System::EventArgs^  e) {
		count++;
	}

do you know the problem?
1
2
3
4
5
if (textBox1->Text == textBox2->Text)
		{
			timer2->Enabled = false;
			textBox3->Text = "test";
		}

i ran a check to see if textBox3->Text has "test" in it when textBox1->Text equals textBox2->Text
it didn't have test and count++ just kept going.
this is basically what the code is doing
if random number = count
timer2->Enabled = false
(let's say the random number is 5)
count: 1 2 3 4 5 6
count keeps going and the textbox ignores the 5 so the timer isn't being disabled. any solution for this?
Topic archived. No new replies allowed.