Open Text File And Sort

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2007
Posts: 9
Reputation: nullified is an unknown quantity at this point 
Solved Threads: 0
nullified nullified is offline Offline
Newbie Poster

Open Text File And Sort

 
0
  #1
Sep 13th, 2007
Hi,

I want to open a text file and read the data then sort it via date.

i found some stuff already that helps but im new to this and if anyone point me in the right direction it would be very appreciated.

this sample app here http://www.c-sharpcorner.com/UploadF...mageFiles.aspx

opens the text file exactly how i want it to, i would just like to have the data sorted by date also. And eventually i would like to add color coding to the IP's and 32 digit numbers.

here is a sample of what the text file would look like....


  1. [08.28.2007 00:47:19] PlayerIP: 62.165.245.133 00f55c74a4dd5b5845a9b870edcff175 ServerIP: 202.60.65.6
  2. [08.28.2007 03:02:29] PlayerIP: 62.165.245.133 00f55c74a4dd5b5845a9b870edcff175 ServerIP: 202.60.65.6
  3. [08.28.2007 03:13:04] PlayerIP: 62.165.245.133 00f55c74a4dd5b5845a9b870edcff175 ServerIP: 202.60.65.6
  4. [08.28.2007 06:36:17] PlayerIP: 62.165.245.133 00f55c74a4dd5b5845a9b870edcff175 ServerIP: 202.60.65.6
  5. [08.29.2007 04:11:11] PlayerIP: 62.165.245.133 00f55c74a4dd5b5845a9b870edcff175 ServerIP: 202.60.65.6
  6. [08.29.2007 10:57:30] PlayerIP: 62.165.245.133 00f55c74a4dd5b5845a9b870edcff175 ServerIP: 202.60.65.6
  7. [08.29.2007 13:19:06] PlayerIP: 62.165.245.133 00f55c74a4dd5b5845a9b870edcff175 ServerIP: 195.149.21.127
  8. [08.30.2007 12:45:50] PlayerIP: 62.165.245.133 00f55c74a4dd5b5845a9b870edcff175 ServerIP: 195.149.21.127
  9. [08.31.2007 13:06:59] PlayerIP: 62.165.245.133 00f55c74a4dd5b5845a9b870edcff175 ServerIP: 195.149.21.127
  10. [09.01.2007 12:38:27] PlayerIP: 62.165.245.133 00f55c74a4dd5b5845a9b870edcff175 ServerIP: 195.149.21.127
  11. [09.02.2007 05:58:45] PlayerIP: 62.165.245.133 00f55c74a4dd5b5845a9b870edcff175 ServerIP: 85.25.130.10
  12. [09.02.2007 07:38:52] PlayerIP: 62.165.245.133 00f55c74a4dd5b5845a9b870edcff175 ServerIP: 195.149.21.127
  13. [09.02.2007 11:01:58] PlayerIP: 62.165.245.133 00f55c74a4dd5b5845a9b870edcff175 ServerIP: 195.149.21.127
  14. [09.02.2007 12:44:47] PlayerIP: 62.165.245.133 00f55c74a4dd5b5845a9b870edcff175 ServerIP: 195.149.21.127

any help is greatly appreciated
Last edited by nullified; Sep 13th, 2007 at 12:54 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 45
Reputation: _r0ckbaer is an unknown quantity at this point 
Solved Threads: 7
_r0ckbaer's Avatar
_r0ckbaer _r0ckbaer is offline Offline
Light Poster

Re: Open Text File And Sort

 
0
  #2
Sep 13th, 2007
Hi, if every line starts with the date, this should do the trick:
  1. List<string> lines = new List<string>();
  2. using (StreamReader r = new StreamReader(new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\data.txt", FileMode.Open)))
  3. {
  4. string line;
  5. while ((line = r.ReadLine()) != null)
  6. {
  7. lines.Add(line);
  8. }
  9.  
  10. }
  11. lines.Sort();
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 9
Reputation: nullified is an unknown quantity at this point 
Solved Threads: 0
nullified nullified is offline Offline
Newbie Poster

Re: Open Text File And Sort

 
0
  #3
Sep 13th, 2007
where exactly would i add this....im assuming this file somewhere;;;

  1. using System;
  2. using System.Drawing;
  3. using System.ComponentModel;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using System.Resources;
  7.  
  8. namespace FileDisplay
  9. {
  10. //windows_forms inherits from the Form class
  11. public class windows_forms : Form //class in System.Windows.Forms
  12. {
  13. // Container is the class in System.ComponentModel namespace
  14. private Container components;
  15.  
  16. //MenuItem is the class in System.Windows.Forms namespace
  17. private MenuItem file;
  18. private MenuItem openfile;
  19. private MenuItem openTextfile;
  20. private MenuItem openImagefile;
  21. private MenuItem closefile;
  22. private MenuItem exit;
  23. private MenuItem help;
  24. private MenuItem abouthelp;
  25.  
  26. //MainMenu is the class in System.Windows.Forms namespace
  27. private MainMenu mainMenu1;
  28.  
  29. //RichTextBox, PictureBox, Panel are the classes in System.Windows.Forms namespace
  30. private RichTextBox fileLoadArea;
  31. private PictureBox pictureBox1 ;
  32. private Panel mainPanel;
  33.  
  34.  
  35. public windows_forms() //constructor
  36. {
  37. InitializeComponent();
  38.  
  39. }
  40.  
  41. private void InitializeComponent()
  42. {
  43. //initializing the classes
  44. this.mainMenu1 = new MainMenu();
  45.  
  46. this.file = new MenuItem();
  47. this.openfile = new MenuItem();
  48. this.openTextfile = new MenuItem();
  49. this.openImagefile = new MenuItem();
  50. this.closefile = new MenuItem();
  51. this.exit = new MenuItem();
  52.  
  53. this.help = new MenuItem();
  54. this.abouthelp= new MenuItem();
  55.  
  56. this.fileLoadArea = new RichTextBox();
  57. this.pictureBox1 = new PictureBox();
  58.  
  59. this.mainPanel = new Panel();
  60.  
  61. this.SuspendLayout();
  62.  
  63.  
  64. // mainMenu1
  65. this.mainMenu1.MenuItems.AddRange(new MenuItem[]
  66. {
  67. this.file,
  68. this.help
  69. });
  70. // file
  71. this.file.Index = 0;
  72. this.file.MenuItems.AddRange(new MenuItem[]
  73. {
  74. this.openfile,
  75. this.closefile,
  76. this.exit
  77. });
  78. this.file.Text = "File";
  79.  
  80. // openfile
  81. this.openfile.Index = 0;
  82. this.openfile.MenuItems.AddRange(new MenuItem[]
  83. {
  84. this.openTextfile,
  85. this.openImagefile
  86. });
  87. this.openfile.Text = "OpenFile";
  88.  
  89. // openTextfile
  90. this.openTextfile.Index = 0;
  91. this.openTextfile.Text = "OpenTextFile...";
  92. this.openTextfile.Click += new System.EventHandler(this.onFileOpen);
  93.  
  94.  
  95. // openImagefile
  96. this.openImagefile.Index = 1;
  97. this.openImagefile.Text = "&OpenImageFile...";
  98. this.openImagefile.Click += new System.EventHandler(this.onImageOpen);
  99.  
  100. // closefile
  101. this.closefile.Index = 1;
  102. this.closefile.Text = "CloseFile";
  103. this.closefile.Click += new System.EventHandler(this.onFileClose);
  104.  
  105. // exit
  106. this.exit.Index = 2;
  107. this.exit.Text = "exit";
  108. this.exit.Click += new System.EventHandler(this.onWindowClose);
  109.  
  110. // help
  111. this.help.Index = 1;
  112. this.help.MenuItems.AddRange(new MenuItem[]
  113. {
  114. this.abouthelp
  115. });
  116. this.help.Text = "Help";
  117.  
  118. // abouthelp
  119. this.abouthelp.Index = 0;
  120. this.abouthelp.Text = "Learning .NET";
  121.  
  122. // fileLoadArea
  123. this.fileLoadArea.Dock = DockStyle.Fill;
  124. this.fileLoadArea.Name = "fileLoadArea";
  125. this.fileLoadArea.Size = new Size(600, 400);
  126. this.fileLoadArea.Text = "";
  127.  
  128. // pictureBox1
  129. this.pictureBox1.Location = new Point(32, 40);
  130. this.pictureBox1.Name = "pictureBox1";
  131. this.pictureBox1.Size = new Size(600,400);
  132.  
  133.  
  134. // mainPanel
  135. //Control class is in System.Windows.Forms namespace
  136. this.mainPanel.Controls.AddRange(new Control[]
  137. {
  138. this.fileLoadArea,
  139. this.pictureBox1
  140. });
  141. this.mainPanel.Dock = DockStyle.Fill;
  142. this.mainPanel.Name = "mainPanel";
  143. this.mainPanel.Size = new Size(600, 400);
  144.  
  145. // windows_forms
  146. this.ClientSize = new System.Drawing.Size(600, 500);
  147. this.Controls.AddRange(new Control[]
  148. {
  149. this.mainPanel
  150. });
  151. this.Menu = this.mainMenu1;
  152. this.Name = "windows_forms";
  153. this.Text = "Learning Windows Forms";
  154. mainPanel.Hide();
  155. this.ResumeLayout();
  156. }
  157.  
  158.  
  159. // Handler for the TextFileOpen command
  160. private void onFileOpen (object sender, EventArgs e)
  161. {
  162. if( pictureBox1 !=null)
  163. pictureBox1.Hide();
  164.  
  165. fileLoadArea.Text ="";
  166. mainPanel.Show();
  167. fileLoadArea.Show();
  168.  
  169. OpenFileDialog openFileDialog1 = new OpenFileDialog();
  170. openFileDialog1.InitialDirectory = "d:\\" ;
  171. openFileDialog1.RestoreDirectory = true ;
  172. openFileDialog1.Filter =
  173. "Text Files (*.txt)|*.txt|Rich Text Format (*.rtf)|*.rtf)|"+
  174. " All Files (*.*)|*.*";
  175.  
  176. if (openFileDialog1.ShowDialog () == DialogResult.OK)
  177. {
  178. String fileName = openFileDialog1.FileName;
  179. if (fileName.Length != 0)
  180. {
  181. try
  182. {
  183. ReadFileInfo(fileName);
  184. }
  185. catch
  186. {
  187. MessageBox.Show (String.Format ("{0} is not " +
  188. "a valid image file", fileName), "Error",
  189. MessageBoxButtons.OK , MessageBoxIcon.Error);
  190. }
  191. }
  192. }
  193. }
  194.  
  195. private void ReadFileInfo(String filename)
  196. {
  197. try
  198. {
  199. FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
  200. FileInfo fInfo = new FileInfo(filename);
  201. string fext = fInfo.Extension.ToUpper();
  202. if (fext.Equals(".RTF"))
  203. fileLoadArea.LoadFile(fs, RichTextBoxStreamType.RichText);
  204. else
  205. fileLoadArea.LoadFile(fs, RichTextBoxStreamType.PlainText);
  206. fs.Close();
  207. }
  208. catch(Exception e)
  209. {
  210. Console.WriteLine("Exception"+e.StackTrace);
  211. }
  212. }
  213.  
  214.  
  215. // Handler for the ImageFileOpen command
  216. private void onImageOpen (object sender, EventArgs e)
  217. {
  218. if (fileLoadArea !=null)
  219. {
  220. fileLoadArea.Hide();
  221. }
  222. mainPanel.Show();
  223.  
  224. OpenFileDialog ofd = new OpenFileDialog ();
  225. ofd.Filter = "Image Files (*.bmp)|*.bmp|JPEG Files (*.jpeg)|*.jpeg|"+
  226. " All Files (*.*)|*.*";
  227. if (ofd.ShowDialog () == DialogResult.OK)
  228. {
  229. String fileName = ofd.FileName;
  230. if (fileName.Length != 0)
  231. {
  232. try
  233. {
  234. pictureBox1.BackgroundImage = new Bitmap(fileName);
  235. pictureBox1.Show();
  236. }
  237. catch
  238. {
  239. MessageBox.Show (String.Format ("{0} is not " +
  240. "a valid image file", fileName), "Error",
  241. MessageBoxButtons.OK , MessageBoxIcon.Error);
  242. }
  243. }
  244. }
  245. }
  246.  
  247.  
  248. // method to drive the File/Close button
  249. private void onFileClose (object sender, System.EventArgs e)
  250. {
  251. mainPanel.Hide();
  252. }
  253.  
  254.  
  255. // method to drive the Window/Close button
  256. private void onWindowClose (object sender, System.EventArgs e)
  257. {
  258. // Handler for the Close command
  259. Close ();
  260. }
  261.  
  262. public static void Main(string[] args)
  263. {
  264. Application.Run(new windows_forms());
  265. }
  266.  
  267. protected override void Dispose( bool disposing )
  268. {
  269. if( disposing )
  270. {
  271. if (components != null)
  272. {
  273. components.Dispose();
  274. }
  275.  
  276. }
  277. base.Dispose( disposing );
  278. }
  279.  
  280. }
  281. }

sorry im very noobish at this....
Last edited by nullified; Sep 13th, 2007 at 6:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 9
Reputation: nullified is an unknown quantity at this point 
Solved Threads: 0
nullified nullified is offline Offline
Newbie Poster

Re: Open Text File And Sort

 
0
  #4
Sep 22nd, 2007
Anyone got any ideas on this one
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Open Text File And Sort

 
0
  #5
Sep 23rd, 2007
I think _r0ckbaer answered you!!
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 9
Reputation: nullified is an unknown quantity at this point 
Solved Threads: 0
nullified nullified is offline Offline
Newbie Poster

Re: Open Text File And Sort

 
0
  #6
Sep 23rd, 2007
i also have a second question

its after his answer in case you missed it.....
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Open Text File And Sort

 
0
  #7
Sep 23rd, 2007
>if every line starts with the date

Not entirely sure if that would work all the time, especially seeing as some dates can be written as DD-MM-YYYY or YYYY-MM-DD etc. A cast iron solution would be to convert the date to a date object and use the sort methods associated with that.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 9
Reputation: nullified is an unknown quantity at this point 
Solved Threads: 0
nullified nullified is offline Offline
Newbie Poster

Re: Open Text File And Sort

 
0
  #8
Sep 23rd, 2007
the date format is always the same for what i am doing...

e.g.

MM-DD-YYYY
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Open Text File And Sort

 
0
  #9
Sep 24th, 2007
Originally Posted by nullified View Post
the date format is always the same for what i am doing...

e.g.

MM-DD-YYYY
in that case _r0ckbaer's solution will fail.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 9
Reputation: nullified is an unknown quantity at this point 
Solved Threads: 0
nullified nullified is offline Offline
Newbie Poster

Re: Open Text File And Sort

 
0
  #10
Sep 24th, 2007
I am so close its not funny........

ive done what you suggested and when i build the application and try to open a text file it says this is not a valid image.the only message i get in express is;

Field 'FileDisplay.windows_forms.components' is never assigned to, and will always have its default value null

Ill inlcude the code to show you what i have

  1.  
  2. using System;
  3. using System.Drawing;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.IO;
  7. using System.Resources;
  8. using System.Collections.Generic;
  9.  
  10. namespace FileDisplay
  11. {
  12.  
  13.  
  14.  
  15. // create a class to compare lines by date
  16. public class LineComparer : IComparer<string>
  17. {
  18.  
  19. public int Compare(string s1, string s2)
  20. {
  21. string dateFormat = "MM.dd.yyyy HH:mm:ss";
  22. DateTime dt1 = DateTime.ParseExact(s1.Substring(1, 19), dateFormat, null);
  23. DateTime dt2 = DateTime.ParseExact(s2.Substring(1, 19), dateFormat, null);
  24. if (dt1 == dt2)
  25. return 0;
  26. else if (dt1 < dt2)
  27. return -1;
  28. else
  29. return 1;
  30. }
  31. }
  32. //windows_forms inherits from the Form class
  33. public class windows_forms : Form //class in System.Windows.Forms
  34. {
  35. // Container is the class in System.ComponentModel namespace
  36. private Container components;
  37.  
  38. //MenuItem is the class in System.Windows.Forms namespace
  39. private MenuItem file;
  40. private MenuItem openfile;
  41. private MenuItem openTextfile;
  42. private MenuItem openImagefile;
  43. private MenuItem closefile;
  44. private MenuItem exit;
  45. private MenuItem help;
  46. private MenuItem abouthelp;
  47.  
  48. //MainMenu is the class in System.Windows.Forms namespace
  49. private MainMenu mainMenu1;
  50.  
  51. //RichTextBox, PictureBox, Panel are the classes in System.Windows.Forms namespace
  52. private RichTextBox fileLoadArea;
  53. private PictureBox pictureBox1 ;
  54. private Panel mainPanel;
  55.  
  56.  
  57. public windows_forms() //constructor
  58. {
  59. InitializeComponent();
  60.  
  61. }
  62.  
  63. private void InitializeComponent()
  64. {
  65. //initializing the classes
  66. this.mainMenu1 = new MainMenu();
  67.  
  68. this.file = new MenuItem();
  69. this.openfile = new MenuItem();
  70. this.openTextfile = new MenuItem();
  71. this.openImagefile = new MenuItem();
  72. this.closefile = new MenuItem();
  73. this.exit = new MenuItem();
  74.  
  75. this.help = new MenuItem();
  76. this.abouthelp= new MenuItem();
  77.  
  78. this.fileLoadArea = new RichTextBox();
  79. this.pictureBox1 = new PictureBox();
  80.  
  81. this.mainPanel = new Panel();
  82.  
  83. this.SuspendLayout();
  84.  
  85.  
  86. // mainMenu1
  87. this.mainMenu1.MenuItems.AddRange(new MenuItem[]
  88. {
  89. this.file,
  90. this.help
  91. });
  92. // file
  93. this.file.Index = 0;
  94. this.file.MenuItems.AddRange(new MenuItem[]
  95. {
  96. this.openfile,
  97. this.closefile,
  98. this.exit
  99. });
  100. this.file.Text = "File";
  101.  
  102. // openfile
  103. this.openfile.Index = 0;
  104. this.openfile.MenuItems.AddRange(new MenuItem[]
  105. {
  106. this.openTextfile,
  107. this.openImagefile
  108. });
  109. this.openfile.Text = "OpenFile";
  110.  
  111. // openTextfile
  112. this.openTextfile.Index = 0;
  113. this.openTextfile.Text = "OpenTextFile...";
  114. this.openTextfile.Click += new System.EventHandler(this.onFileOpen);
  115.  
  116. // openImagefile
  117. this.openImagefile.Index = 1;
  118. this.openImagefile.Text = "&OpenImageFile...";
  119. this.openImagefile.Click += new System.EventHandler(this.onImageOpen);
  120.  
  121. // closefile
  122. this.closefile.Index = 1;
  123. this.closefile.Text = "CloseFile";
  124. this.closefile.Click += new System.EventHandler(this.onFileClose);
  125.  
  126. // exit
  127. this.exit.Index = 2;
  128. this.exit.Text = "exit";
  129. this.exit.Click += new System.EventHandler(this.onWindowClose);
  130.  
  131. // help
  132. this.help.Index = 1;
  133. this.help.MenuItems.AddRange(new MenuItem[]
  134. {
  135. this.abouthelp
  136. });
  137. this.help.Text = "Help";
  138.  
  139. // abouthelp
  140. this.abouthelp.Index = 0;
  141. this.abouthelp.Text = "Learning .NET";
  142.  
  143. // fileLoadArea
  144. this.fileLoadArea.Dock = DockStyle.Fill;
  145. this.fileLoadArea.Name = "fileLoadArea";
  146. this.fileLoadArea.Size = new Size(600, 400);
  147. this.fileLoadArea.Text = "";
  148.  
  149. // pictureBox1
  150. this.pictureBox1.Location = new Point(32, 40);
  151. this.pictureBox1.Name = "pictureBox1";
  152. this.pictureBox1.Size = new Size(600,400);
  153.  
  154.  
  155. // mainPanel
  156. //Control class is in System.Windows.Forms namespace
  157. this.mainPanel.Controls.AddRange(new Control[]
  158. {
  159. this.fileLoadArea,
  160. this.pictureBox1
  161. });
  162. this.mainPanel.Dock = DockStyle.Fill;
  163. this.mainPanel.Name = "mainPanel";
  164. this.mainPanel.Size = new Size(600, 400);
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. // windows_forms
  172. this.ClientSize = new System.Drawing.Size(600, 500);
  173. this.Controls.AddRange(new Control[]
  174. {
  175. this.mainPanel
  176. });
  177. this.Menu = this.mainMenu1;
  178. this.Name = "windows_forms";
  179. this.Text = "Learning Windows Forms";
  180. mainPanel.Hide();
  181. this.ResumeLayout();
  182. }
  183.  
  184.  
  185. // Handler for the TextFileOpen command
  186. private void onFileOpen (object sender, EventArgs e)
  187. {
  188. if( pictureBox1 !=null)
  189. pictureBox1.Hide();
  190.  
  191. fileLoadArea.Text ="";
  192. mainPanel.Show();
  193. fileLoadArea.Show();
  194.  
  195. OpenFileDialog openFileDialog1 = new OpenFileDialog();
  196. openFileDialog1.InitialDirectory = "d:\\" ;
  197. openFileDialog1.RestoreDirectory = true ;
  198. openFileDialog1.Filter =
  199. "Text Files (*.txt)|*.txt|Rich Text Format (*.rtf)|*.rtf)|"+
  200. " All Files (*.*)|*.*";
  201.  
  202. if (openFileDialog1.ShowDialog () == DialogResult.OK)
  203. {
  204. String fileName = openFileDialog1.FileName;
  205. if (fileName.Length != 0)
  206. {
  207. try
  208. {
  209. string[] lines = File.ReadAllLines("fileName");
  210. LineComparer comparer = new LineComparer();
  211. Array.Sort(lines, comparer);
  212. //ReadFileInfo(fileName);
  213. }
  214. catch
  215. {
  216. MessageBox.Show (String.Format ("{0} is not " +
  217. "a valid image file", fileName), "Error",
  218. MessageBoxButtons.OK , MessageBoxIcon.Error);
  219. }
  220. }
  221. }
  222. }
  223.  
  224. private void ReadFileInfo(String filename)
  225. {
  226. try
  227. {
  228. FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
  229. FileInfo fInfo = new FileInfo(filename);
  230. string fext = fInfo.Extension.ToUpper();
  231. if (fext.Equals(".RTF"))
  232. fileLoadArea.LoadFile(fs, RichTextBoxStreamType.RichText);
  233. else
  234. fileLoadArea.LoadFile(fs, RichTextBoxStreamType.PlainText);
  235. fs.Close();
  236. }
  237. catch(Exception e)
  238. {
  239. Console.WriteLine("Exception"+e.StackTrace);
  240. }
  241. }
  242.  
  243.  
  244. // Handler for the ImageFileOpen command
  245. private void onImageOpen (object sender, EventArgs e)
  246. {
  247. if (fileLoadArea !=null)
  248. {
  249. fileLoadArea.Hide();
  250. }
  251. mainPanel.Show();
  252.  
  253. OpenFileDialog ofd = new OpenFileDialog ();
  254. ofd.Filter = "Image Files (*.bmp)|*.bmp|JPEG Files (*.jpeg)|*.jpeg|"+
  255. " All Files (*.*)|*.*";
  256. if (ofd.ShowDialog () == DialogResult.OK)
  257. {
  258. String fileName = ofd.FileName;
  259. if (fileName.Length != 0)
  260. {
  261. try
  262. {
  263. pictureBox1.BackgroundImage = new Bitmap(fileName);
  264. pictureBox1.Show();
  265. }
  266. catch
  267. {
  268. MessageBox.Show (String.Format ("{0} is not " +
  269. "a valid image file", fileName), "Error",
  270. MessageBoxButtons.OK , MessageBoxIcon.Error);
  271. }
  272. }
  273. }
  274. }
  275.  
  276.  
  277. // method to drive the File/Close button
  278. private void onFileClose (object sender, System.EventArgs e)
  279. {
  280. mainPanel.Hide();
  281. }
  282.  
  283.  
  284. // method to drive the Window/Close button
  285. private void onWindowClose (object sender, System.EventArgs e)
  286. {
  287. // Handler for the Close command
  288. Close ();
  289. }
  290.  
  291. public static void Main(string[] args)
  292. {
  293. Application.Run(new windows_forms());
  294. }
  295.  
  296. protected override void Dispose( bool disposing )
  297. {
  298. if( disposing )
  299. {
  300. if (components != null)
  301. {
  302. components.Dispose();
  303. }
  304.  
  305. }
  306. base.Dispose( disposing );
  307. }
  308.  
  309. }
  310. }


Thanks again..
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC