Harvey Wilson

Harvey Wilson

  • NA
  • 23
  • 9k

Import .srt file to datagrid

Jun 11 2014 9:34 PM
Hi,
 
This has been driving me mad all week, so finally I gave in an am posting for some help. I am a budding c# programmer and have made lots of simple programs. Every time I decide to try something new, something crops up that I can't yet solve.
 
I am trying to make an editor for syncing srt subtitle files. I can load the subtitle file, I can send it to a textbox if i want to, i can trim and split the time line and send them to different places, but I am having difficulty processing it as a whole.
 
I practiced splitting and trimming the time line when i had the text string in a textbox just to make it simple.
 
The srt file is structured like:
 
1
00:00:01,151 --> 00:00:03,432
Subtitle Line 1
2
00:00:05,651 --> 00:00:07,238
Subtitle Line 1 sub 2
Subtitle Line 2 sub 2
3
00:00:08,751 --> 00:00:10,065
Subtitle Line 1 sub 3
etc etc etc 
 
I placed the "00:00:01,151 --> 00:00:03,432" line in textBox1 and using the code:
 
(sorry couldn't find the code tags)
 
//splits the string and keeps the text before the -->
string startTime = textBox1.Text;
string result = startTime.Substring(0, theString.IndexOf("-->")).Trim();
textBox4.Text = result;
//takes the text after the delimiter
int index = textBox1.Text.IndexOf('>');
string result2 = textBox1.Text.Substring(index + 2); 
textBox5.Text = result2;
This code puts each part into separate text boxes and works as i want.
 
I have a good handle on logic code and can understand what i need to do, but not how to do it.
 
- I need to read the line and if it contains only an integer, send it to column 1.
-I need to read the line and if it contains '-->' use the above code or similar and send it to cols 2 and 3.
- i need to read the next line and send it to col 4 (1st subtitle text line, will always contain text) 
- i need to read the line and if it is blank, make a new row, else append the text to the text in column 4 and then make new row. Then start over.
 
Logically, i know what to do, but i am having trouble with looping through and processing each line.
 
Does anyone have any tips that could help me please? Thanks