Writing to textbox really slow.

Hi, I'm new here. Sorry if this is the wrong forum.

ok the below code works but is extremely slow. takes 15 seconds to execute... Why is it going so slow? I have many more textBoxes to write to then the ones in the code. I have tried various ways of reading from a text file. Some would not read white space characters and others would shows numbers and not text. this is a as close as I got.
Thanks for your time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
        int z;

	if (myfile.good()) {
		/*Read data using streambuffer iterators.*/
		vector<char> buf((std::istreambuf_iterator<char>(myfile)), (std::istreambuf_iterator<char>()));

		/*str_buf holds all the data including whitespaces and newline .*/
		string str_buf(buf.begin(), buf.end());

		string str = str_buf;

		for (int x = 0; x < str_buf.length()-40; x++)
		{
			
			if (str[x] == 0x0A) { str.insert(x, "\r"); x++; }
		
		}
		String^ str2 = gcnew String(str.c_str());


		for (z = 0; str2[z] != '/'; z++)
		{

			textBox2->Text = textBox2->Text + str2[z].ToString();
		}

		for (z; str2[z+1] != '/'; z++)
		{

			textBox3->Text = textBox3->Text + str2[z + 1].ToString();
		}

		for (z; str2[z + 2] != '/'; z++)
		{

			textBox4->Text = textBox4->Text + str2[z + 2].ToString();
		}

		for (z; str2[z + 3] != '/'; z++)
		{

			textBox5->Text = textBox5->Text + str2[z + 3].ToString();
		}

		for (z; str2[z + 4] != '/'; z++)
		{

			textBox6->Text = textBox6->Text + str2[z + 4].ToString();
		}
	}
Last edited on
That's kinda bonkers, whatever it is--all those possibly unbounded loops.

Perhaps if you tell us what your trying to achieve, that's save us the pain of trying to work out what that is.
it looks like it mostly copies the string, removing the /s as a delimiter, is that correct?
you can do the work using c++ string tools (string, stringstream, string view trio) instead of home grown looping, but that isnt' the problem.

I believe from my windows experience the problem is the live updating.
you are saying textbox = value over and over in a loop. Don't do that. Update a temp value in the loops, and set each text box only once. The windows subsystems has to do a lot of graphics to do all that updating!

it is the wrong forum, but this is a small community and since you got the code tags we can let it slide just this once :P
Next time put windows code in the windows forum section, is all.
Last edited on
Thanks for your reply. Please bear with me, I will try to explain.

I have a text file that contains text that I want to display in my textBoxes control on a windows form.

I am reading/writing to those textBoxes. Using delimiter of '/' to use as a counter so the correct text is loaded to the specified textBox.
i'm so sorry. I had no idea where to post it. Can you move this topic to the correct forum? What is considered Windows Code? I thought it was a mix of C++ plus windows CLI or something. Thanks again. :)

And thanks for the help. It is now working instantly! Below is the working version of the code. I am now only writing to the textBox once as you suggested.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
int z; // The position in the str buffer

	if (myfile.good()) {
		/*Read data using streambuffer iterators.*/
		vector<char> buf((std::istreambuf_iterator<char>(myfile)), (std::istreambuf_iterator<char>()));

		/*str_buf holds all the data including whitespaces and newline .*/
		string str_buf(buf.begin(), buf.end());

		string str = str_buf;

		for (int x = 0; x < str_buf.length(); x++) // Add '\r' to all lines in the loaded text.
		{
			
			if (str[x] == 0x0A) { str.insert(x, "\r"); x++; }
		
		}
		String^ str2 = gcnew String(str.c_str());
		String^ str3;
		String^ str4;
		String^ str5;
		String^ str6;

		for (z = 0; str2[z] != '/'; z++)
		{
			str3 = str3 + str2[z].ToString();

		}
		textBox2->Text = str3;

		for (z; str2[z+1] != '/'; z++)
		{
			str4 = str4 + str2[z+1].ToString();
		}
		textBox3->Text = str4;

		for (z; str2[z + 2] != '/'; z++)
		{
			str5= str5 + str2[z + 2].ToString();
		}
		textBox4->Text = str5;

		for (z; str2[z + 3] != '/'; z++)
		{
			str6 = str6 + str2[z + 3].ToString();
		}
		textBox5->Text = str6;
Its MS C++/cli
it can't be moved and its not a problem at all, I would not even have said anything except you asked in your original if it was the right place. Glad the suggestion cleared it up for you!


String^ str2 = gcnew String(str.c_str()); /// this is not c++. its an extension to the language, regular c++ would choke on it as you can't xor on the left side of an assignment, there is no such thing as String, there is no such thing as gcnew...
Last edited on
We'd normally use strtok, or some modern equivalent to break up the original string into sections that go in each TextBox.

If you were going to test your code with a variety of inputs (both good and bad), how would you write that code?
I am trying to do what you suggested. how do you convert a token into a string for the TextBox? I am struggling to understand. I get "this variable is unsafe consider using strtok_S instead"

edit. I found this one. It compiles fine but I don't know how to pass the token to the textbox.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
include <cstring>
#include <iostream>
	using namespace std;

	
		char str[] = "Remember me when you look at the moon!";
		char delim[] = " ";

		cout << "The tokens are:" << endl;

		// tokenize str in accordance with delim
		char* token = strtok(str, delim);

		// loop until strtok() returns NULL
		while (token) {

			// print token
			cout << token << endl;
			

			// take subsequent tokens
			token = strtok(NULL, delim);






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0;
}
Last edited on
"this variable is unsafe consider using strtok_S instead"

these are warnings to try to get you to use 'safer' versions that prevent buffer overflow type security attacks. It is only a warning, and does not matter unless your program is vulnerable to attacks (is commercial grade software rather than learning / at home stuff) but you may as well learn to use it.

char * is how C does strings, a "c-string" as it is called by programmers. std::string can be constructed from one, or accept them in various assignment statements or cast operations and you can go backwards with string.cstr() or if very carful about what you do, &string[0] is a char*

but, lets back up. strtok is a C function for a C data type. Lets not do that to ourself; re-read above you you will see: use strtok, or some modern equivalent ...

give this a read, and consider using a stringstream instead:
https://www.geeksforgeeks.org/tokenizing-a-string-cpp/

strtok is an important function (you may frequently use it on command line argument passing if nowhere else): it is efficient and it gets the job done. But its rather messy, and if you are unfamiliar with C style string work, dangerous (bug prone). Use it if you want, but brush up on C string processing if you are not well versed.

regx is something you should keep in your back pocket if you need it, but its a very large hammer for this problem which just needs a gentle tap :)
Last edited on
One thing to note about strtok....it is destructive to the C string it is operating on, it modifies the C string by writing '\0' characters to the string. It also can't be used on string literals.

strtok can be used on a copy of a C string without a problem. Or a copy to a C string of std::string's c_str().

As jonnin points out one of the better C++ ways to tokenize a string is with a stringstream.
I have some code from the link you provided. It compiles fine... Seems like it should work. But it outputs using << cout.
How do I write it to the texboxes?

I really am trying. Spent a few hours on this, searching google.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include <string>  
#include <iostream> 
#include <sstream>   
			
		string line = "GeeksForGeeks is a must try";

		// Vector of string to save tokens
		vector <string> tokens;

		// stringstream class check1
		stringstream check1(line);

		string intermediate;

		// Tokenizing w.r.t. space ' '
		while (getline(check1, intermediate, ' '))
		{
			tokens.push_back(intermediate);
		}

		// Printing the token vector
		for (int i = 0; i < tokens.size(); i++)
		{
					
			cout << tokens[i] << '\n';			

		}



This code does convert cout. Just not with the the above code. And it uses gcnew which you suggested I shouldn't use.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 #include <sstream>
    
 . . .
    
 using namespace std;
 stringstream cout;
 const char* endl = "\r\n";
    
 // Your current code that uses 'cout':
 cout << "pixels intensities =" << image1 << endl;
 cout << "image rows=" << image1.rows << endl;
 . . . etc.
    
 textBox1->Text = gcnew String( cout.str( ).c_str( ) );







Last edited on
1
2
3
4
5
6
7
8
9
10
11
#include <windows.h>

int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance,
                    _In_ PWSTR szCmdLine, _In_ int iCmdShow)
{
   PCWSTR hello { L"Hello Windows!" };

   MessageBoxW(NULL, hello, hello, MB_OK);

   return 0;
}
gcnew is part of the modern windows library; you will probably be compelled to use it some if you use that tool.
Its fine; its just not standard c++. Most real code uses libraries and extensions; it would be nice if the libraries had an interface that was pure standard C++ but for a variety of reasons, they don't always.
gcnew is part of MS C++/cli and C++/cx and is part of .net. It allocates memory which is then automatically deallocated when its reference count goes to zero, which occurs after the last copy of the reference has gone out of scope. In terms of C++, it's similar to std::shared_memory - but for the managed .net runtime.
@Cyclone I like the way std::getline is used to tokenize the string.

Regarding gcnew, as said above, it's part of .NET compatibility. And if you're using .NET, that's fine. But we can't comment beyond that as we don't have a type for your textboxes.
It’s just a standard text box. I’m using windows forms. Visual studio.

I got it working now.

Thanks for the help... problem solved.
Topic archived. No new replies allowed.