I've asked a similar question to this before but I'm still having trouble. Suspose I have a text file that has:
001 This is line 1
002 This is line 2
003 This is line 3
And a Form with a richTextBox (called richTextBox1) and a button. When the button is clicked, a line from this text is then displayed into the richTextBox.
So far, I have the code for the button when it's clicked:
1 2 3 4 5
StreamReader^ sr = gcnew StreamReader("C:\\Test.txt");
String^ line;
line = sr->ReadLine();
richTextBox1->Text = line->Substring(4);
sr->Close();
This only displays line one but doesn't display the first 4 characters (because of Substring(4))...
How do I display a specific line into a richTextBox? E.g. line 'n'.I've done this using getline etc in C++, but not for a windows form.
The codes for the includes, and introduction to bool 'isWanted' remains the same. However the button1_Click event needs the following:
1 2 3 4 5 6 7 8 9 10 11
bool isWanted(const std::string & str);
String^ str2;
ifstream fin("C:\\Test.txt");
string str;
while (getline(fin, str)){
if (isWanted(str))
str2 = gcnew String(str.substr(4).c_str()); // convert std:string to System::String^ and
//substr(4) is used here instead of str2->Substring(4) in the next line
richTextBox1->Text = str2; //displays truncated line of text}
fin.close();
I'm not sure why but the program works if substr(4) is applied to the std::string rather than using ->Substring(4) after std::string has been converted to System::String^.... At least, this is my interpretation of what is going on..