Reading from Notepad

M

Matthew Wells

Hello all,

This is not about reading a text file. I have a C# Windows Form with a
datagridview on the left side listing some text files. On the right side, I
have embedded notepad in a panel that displays the selected file. I am
using notepad instead of a richtextbox because the users need the
functionality.

When a user clicks a different file, the new file appears. No problem. But
if a user makes changes to a file and does not manually do File->Save before
clicking a new item in the grid, the changes are lost.

I would like to be able to read the contents of the notepad in the form both
when the file is loaded and BEFORE loading the next file so I can do a
comparison and prompt the user to save. I don't want to change the file's
modified date unless an actual change is made. Also, there's a lot of other
things that happen if a file changes so just saving everything is not an
option.

Thank you.

Matthew Wells
(e-mail address removed)
 
B

bradbury9

El martes, 25 de septiembre de 2012 17:16:21 UTC+2, Matthew Wells escribió:
Hello all,



This is not about reading a text file. I have a C# Windows Form with a

datagridview on the left side listing some text files. On the right side, I

have embedded notepad in a panel that displays the selected file. I am

using notepad instead of a richtextbox because the users need the

functionality.



When a user clicks a different file, the new file appears. No problem. But

if a user makes changes to a file and does not manually do File->Save before

clicking a new item in the grid, the changes are lost.



I would like to be able to read the contents of the notepad in the form both

when the file is loaded and BEFORE loading the next file so I can do a

comparison and prompt the user to save. I don't want to change the file's

modified date unless an actual change is made. Also, there's a lot of other

things that happen if a file changes so just saving everything is not an

option.



Thank you.



Matthew Wells

How did you implement the Notepad in the System.Windows.Forms? Did you use a custom control? If you did, you could check if that control's events for a Content_Changed, File_Saved or similar event.

Without providing more concise info on how you implemented that is hard to give advice.
 
J

Jeff Johnson

I am using notepad instead of a richtextbox because the users need the
functionality.

I have to pile on here: WHAT FUNCTIONALITY?

The ability to open/save files? Search and replace? Insert current
date/time? Printing?
 
M

Matthew Wells

Good Morning All,

Users need to use the find, replace, and save features of notepad. I'd
rather not rewrite the wheel if I can avoid it. Users select a file from
the grid, make changes via find and replace, then save the file. If a user
clicks on a new file from the grid on the left, I need to see if the file
changed so the changes won't be lost. I don't know if I can save the file
myself, but I don't think so. At least not by way of (pseudo) notepad.save.
So my question is, can I read the contents of my process?


Here's how I embedded Notepad:

private Process mProc = new Process();
private ProcessStartInfo mStartInfo = new ProcessStartInfo("notepad.exe");

mStartInfo.Arguments = psFile;
mStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
try { mProc.Kill(); }
catch { }
mProc.StartInfo = mStartInfo;
mProc.Start();
mProc.WaitForInputIdle();
mHnd = mProc.MainWindowHandle;
string sFile = FB.Mid(msFile, msFile.LastIndexOf(@"\") + 2);
mHnd = FBWindowFunctions.FindWindow(null, sFile + " - Notepad");
FBWindowFunctions.SetParent(mHnd, pnlNotePad.Handle);
FBWindowFunctions.SetWindowLong(mHnd, GWL_STYLE,
(int)WindowStyles.WS_VISIBLE); // (int)WindowStyles.WS_MAXIMIZE);
FBWindowFunctions.MoveWindow(mHnd, 0, 0,
splitPM.Panel2.Width - 30,
splitPM.Panel2.Height - 30, true);
FBWindowFunctions.ShowWindow(mHnd, 5);

FBWindowFunctions is a static class of APIs

[DllImport("user32.dll")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr
hWndNewParent);
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int
dwNewLong);
[DllImport("user32.dll")]
public static extern bool MoveWindow(IntPtr Handle, int x, int y, int w, int
h, bool repaint);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern bool EnableWindow(IntPtr hwnd, bool enabled);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string
lpWindowName);



"bradbury9" wrote in message

El martes, 25 de septiembre de 2012 17:16:21 UTC+2, Matthew Wells escribió:
Hello all,



This is not about reading a text file. I have a C# Windows Form with a

datagridview on the left side listing some text files. On the right side,
I

have embedded notepad in a panel that displays the selected file. I am

using notepad instead of a richtextbox because the users need the

functionality.



When a user clicks a different file, the new file appears. No problem.
But

if a user makes changes to a file and does not manually do File->Save
before

clicking a new item in the grid, the changes are lost.



I would like to be able to read the contents of the notepad in the form
both

when the file is loaded and BEFORE loading the next file so I can do a

comparison and prompt the user to save. I don't want to change the file's

modified date unless an actual change is made. Also, there's a lot of
other

things that happen if a file changes so just saving everything is not an

option.



Thank you.



Matthew Wells

How did you implement the Notepad in the System.Windows.Forms? Did you use a
custom control? If you did, you could check if that control's events for a
Content_Changed, File_Saved or similar event.

Without providing more concise info on how you implemented that is hard to
give advice.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top