Results 1 to 8 of 8

Thread: [RESOLVED] How can I incorporate a prewritten Notepad file into my program

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2024
    Posts
    95

    Resolved [RESOLVED] How can I incorporate a prewritten Notepad file into my program

    Visual Studio 2022. Windows 10 desktop computer.

    I know how to start Notepad from within my program.

    Code:
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
         Shell("Notepad.exe", AppWinStyle.NormalFocus)
    
     End Sub
    I would like to have the program bring up a pre-written Notepad file, instead of just an empty notepad.

    Any ideas from the experts?

    Thank you!

    PS: If this is possible, can I somehow incorporate it into my Resources?

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Dec 2024
    Posts
    95

    Re: How can I incorporate a prewritten Notepad file into my program

    Quote Originally Posted by FordPrefect View Post
    Hi

    Here is one way to do that. This example uses a path to a file on my DeskTop.

    Code:
    ' The file name is a string for the full path and name
    ' and could be from anywhere such as a DataGridView, or
    ' as shown here, a string variable etc
    
    Dim MyFileName As String = "C:\Users\lesha\Desktop\TESTFILE.txt"
    
    Dim si = New ProcessStartInfo With {.FileName = MyFileName, .UseShellExecute = True}
    Process.Start(si)
    EDIT: not sure what you mean re: resources.
    Thanks!, I'm off to try that now.

    By resources, I mean that if another user has my program, and doesn't have that particular notepad file on his computer, he wouldn't be able to view it. So if I can incorporate it into my program, and he has my program, he would be able to view the pre-written notepad file.

    I apologize if this sound confusing.... I've only been at this since November, but having a ton of fun!

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,940

    Re: How can I incorporate a prewritten Notepad file into my program

    This will open an instance of notepad, displaying a text file from My.Resources. You should note your My.Resources text file will be readonly.

    Code:
    Public Class Form1
    
        Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
        Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr
        
        Private Const WM_SETTEXT As Integer = &HC
    
        Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim pr As New Process With {.StartInfo = New ProcessStartInfo("notepad.exe")}
            pr.Start()
            pr.WaitForInputIdle()
    
            Dim iHwndChild As IntPtr = FindWindowEx(pr.MainWindowHandle, 0, "Edit", vbNullString)
            SendMessage(iHwndChild, WM_SETTEXT, 0, My.Resources.myResourceTextFile)
        End Sub
    
    End Class

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,940

    Re: How can I incorporate a prewritten Notepad file into my program

    It's better to avoid an error than explicitly catch an error, when there is a likely chance an error could be thrown…
    Last edited by .paul.; Feb 8th, 2025 at 11:04 PM.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,940

    Re: How can I incorporate a prewritten Notepad file into my program

    If you want to use My.Settings...

    Code:
    Public Class Form1
    
        Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
        Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr
        
        Private Const WM_SETTEXT As Integer = &HC
    
        Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim pr As New Process With {.StartInfo = New ProcessStartInfo("notepad.exe")}
            pr.Start()
            pr.WaitForInputIdle()
    
            Dim iHwndChild As IntPtr = FindWindowEx(pr.MainWindowHandle, 0, "Edit", vbNullString)
            SendMessage(iHwndChild, WM_SETTEXT, 0, My.Settings.anyStringSetting)
        End Sub
    
    End Class

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,940

    Re: How can I incorporate a prewritten Notepad file into my program

    To be honest, I don't know why you even need to use notepad. As a programmer, you have the tools to build your own custom notepad style editor really easily, which you can customize to fit your requirements.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,940

    Re: How can I incorporate a prewritten Notepad file into my program

    Quote Originally Posted by FordPrefect View Post
    Hi Paul

    That post at #6 was one I was working on and for unknown reason the edit window shut down so I started a new reply (which is #5 ???)

    Anyway, are you able to delete post #6?
    Post #6 edited
    Last edited by .paul.; Feb 8th, 2025 at 11:05 PM.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Dec 2024
    Posts
    95

    Re: How can I incorporate a prewritten Notepad file into my program

    Ford, and Paul...

    It was a success and everything turned out exactly how I wanted it to. Thank you so much for your help!!!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width