Results 1 to 30 of 30

Thread: [RESOLVED] Select all/Find/Open with?

  1. #1

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Resolved [RESOLVED] Select all/Find/Open with?

    I'm working on a program like notepad.

    However, I will need to know the codes for the following actions:

    1) Select all text in a textbox called txtTestFile

    2) Find a certain word or phrase in the document and highlight it and make the textox scroll down to that part of the text

    3) When I select a .txt/.vbs/.html document and make it open with my program, it just shows a blank text box. How do I make the text show?

    Your help will be greatly appreciated
    Last edited by ajames; Nov 6th, 2005 at 03:42 PM.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Select all/Find/Open with?

    Quote Originally Posted by ajames
    1) Select all text in a textbox called txtTestFile
    VB Code:
    1. Private Sub Command4_Click()
    2. txtTestFile.SelStart = 0
    3. txtTestFile.SelLength = Len(txtTestFile.Text)
    4. End Sub
    Quote Originally Posted by ajames
    2) Find a certain word or phrase in the document and highlight it and make the textox scroll down to that part of the text
    VB Code:
    1. Private Sub ColorWord(prtbName As RichTextBox, _
    2.             pstrWord As String, _
    3.             plngHighlightColor As Long, _
    4.             Optional pblnBold As Boolean)
    5.            
    6. Dim lngPos As Long
    7. Dim lngSelStart As Long
    8. Dim lngSelLength As Long
    9. Dim blnRetry As Boolean
    10.  
    11. lngSelStart = prtbName.SelStart
    12. lngSelLength = prtbName.SelLength
    13.  
    14. lngPos = prtbName.Find(pstrWord, 0)
    15.  
    16. Do
    17.  
    18.     blnRetry = False
    19.      
    20.     prtbName.SelColor = plngHighlightColor
    21.    
    22.     If pblnBold = True Then
    23.         prtbName.SelBold = True
    24.     End If
    25.    
    26.     lngPos = prtbName.Find(pstrWord, lngPos + prtbName.SelLength)
    27.  
    28. Loop While lngPos > 0
    29.  
    30. prtbName.SelStart = lngSelStart
    31. prtbName.SelLength = lngSelLength
    32.  
    33. End Sub
    34.  
    35. Private Sub Command1_Click()
    36. If chkBold = vbChecked Then
    37.     ColorWord RichTextBox1, Text1.Text, vbRed, True
    38. Else
    39.     ColorWord RichTextBox1, Text1.Text, vbRed
    40. End If
    41. End Sub
    Quote Originally Posted by ajames
    3) When I select a .txt/.vbs/.html document and make it open with my program, it just shows a blank text box. How do I make the text show?
    How are you selecting and showing it?

  3. #3

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: Select all/Find/Open with?

    Quote Originally Posted by Hack
    How are you selecting and showing it?
    In what way do you mean?

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Select all/Find/Open with?

    You stated
    Quote Originally Posted by ajames
    3) When I select a .txt/.vbs/.html document and make it open with my program, it just shows a blank text box.
    This says to me that you are already at the point where you are selecting a file and attempting to read it into your richtextbox.

    This is a pretty simple task, so if it is just showing a blank, I'd like to know how you are selecting it, and, after selection, how you are reading it into your textbox.

  5. #5
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Select all/Find/Open with?

    Hack, I think he is asking how to associate file extensions with his program. Also, I think he means highlight as in select not highlight as in color it.

    For find/replace: http://www.vbaccelerator.com/home/VB...ce/article.asp

    For associating extensions with your program:
    http://www.vbaccelerator.com/home/VB...ns/article.asp
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  6. #6

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: Select all/Find/Open with?

    I am opening it through windows using "open with". otherwise, I open them using my "open" button (in my program) using this code
    VB Code:
    1. Private Sub openmenu_Click()
    2.  
    3.     Dim strBuffer       As String
    4.     Dim intDemoFileNbr  As Integer
    5.     Dim strFileToOpen   As String
    6.    
    7.     On Error GoTo openmenu_Click_Exit
    8.  
    9.     With dlgDemo
    10.         .CancelError = True
    11.         .InitDir = mstrLastDir
    12.         .Flags = cdlOFNHideReadOnly
    13.         .FileName = ""
    14.         .Filter = "Text Files(*.txt)|*.txt|All Files(*.*)|*.*"
    15.         .ShowOpen
    16.         strFileToOpen = .FileName
    17.     End With
    18.  
    19.     On Error GoTo openmenu_Click_Error
    20.    
    21.     intDemoFileNbr = FreeFile
    22.     Open strFileToOpen For Binary Access Read As #intDemoFileNbr
    23.     strBuffer = Input(LOF(intDemoFileNbr), intDemoFileNbr)
    24.     txtTestFile.Text = strBuffer
    25.     Close #intDemoFileNbr
    26.  
    27.     mstrLastDir = Left$(strFileToOpen, InStrRev(strFileToOpen, "\") - 1)
    28.  
    29.     Exit Sub
    30.    
    31. openmenu_Click_Error:
    32.     MsgBox "The following error has occurred:" & vbNewLine _
    33.          & "Err # " & Err.Number & " - " & Err.Description, _
    34.            vbCritical, _
    35.            "Open Error"
    36.            
    37. openmenu_Click_Exit:
    38. End Sub

    however, It refuses to open if I open it through windows

  7. #7
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Select all/Find/Open with?

    ajames, use the second link I posted above to get it to open through windows. If you have trouble implementing that code, post here and we will help you.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  8. #8

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: Select all/Find/Open with?

    I cannot access the registry to do this.

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Select all/Find/Open with?

    If you are creating a Notepad like program why are you opening a text file for Binary Access Read?

    If you are going to read a text file into a textbox you should be opening the file for Input.

  10. #10

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: Select all/Find/Open with?

    I'm not sure what you are talking about, but this is what i mean:

    If I open a file IN my program (using my code above) it works fine. HOWEVER if I want to open them from OUTSIDE the program, it does not work.

    Please put some kind of code, I don't understand lots of this technical jargon

  11. #11
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Select all/Find/Open with?

    Quote Originally Posted by Hack
    If you are creating a Notepad like program why are you opening a text file for Binary Access Read?

    If you are going to read a text file into a textbox you should be opening the file for Input.
    Opening a text file for binary is just another way of opening things. It tends to be faster. You get the same result.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  12. #12
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Select all/Find/Open with?

    To get openwith to work you have to load the file that is in Command$ when the program starts. For example:
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim strBuffer       As String
    3.     Dim intDemoFileNbr  As Integer
    4.     Dim strFileToOpen   As String
    5.     If Len(Command$) > 0 Then
    6.         intDemoFileNbr = FreeFile
    7.         Open Command$ For Binary Access Read As #intDemoFileNbr
    8.         strBuffer = Input(LOF(intDemoFileNbr), intDemoFileNbr)
    9.         txtTestFile.Text = strBuffer
    10.         Close #intDemoFileNbr
    11.         mstrLastDir = Left$(strFileToOpen, InStrRev(strFileToOpen, "\") - 1)
    12.     End If
    13. End Sub

  13. #13
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Select all/Find/Open with?

    Quote Originally Posted by ajames
    I'm not sure what you are talking about, but this is what i mean:

    If I open a file IN my program (using my code above) it works fine. HOWEVER if I want to open them from OUTSIDE the program, it does not work.

    Please put some kind of code, I don't understand lots of this technical jargon
    These may seem like similar tasks but they are actually quite different.

    You MUST access the registery to have .txt or .html files automatically open with your program. The link I posted earlier in this thread teaches you how. Have you read it? If you don't understand it then ask questions, but at least try to read it.

    The way it works is like so:
    * You double-click a .txt file in Windows
    * Windows looks in the registery to see what program it should open this type of file with
    * It then executes that program using the command line. This means that things are passed to your program via the command line (like the DOS).
    * When your program is run this way, the command line will hold the path of the file that called it. You have to get that path and then use it to open the file you want.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  14. #14

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: Select all/Find/Open with?

    I do not wish for it to AUTOMATICALLY open those type of files. Just for it to open a certain file type once using the "open with" button on windows.

    Also, neither "find" commands worked.

  15. #15
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Select all/Find/Open with?

    And did you try my suggestion? This is how it is done when you select Open With and Browse for a program.

  16. #16
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Select all/Find/Open with?

    Quote Originally Posted by ajames
    I do not wish for it to AUTOMATICALLY open those type of files. Just for it to open a certain file type once using the "open with" button on windows.

    Also, neither "find" commands worked.
    In that case then you don't need to access the registry. but you do need to what moeur said (and what I mentioned). You need to parse the string held in the "Command". That is the only way your program will know what is going on. How else would it know which file to open?

    Here is how the process works
    * Go to "Open With > Browse..." and select your EXE.
    * Windows executes your program using a DOS command that would look something like this: "C:\dev\VB\Projects\test project 2\main.exe C:\My Documents\myfile.txt" (that isn't exactly how itlooks like, but its something like that).
    * You can then use the string held in Command in VB to get the path of that file and open it.

    Do you understand now?
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

  17. #17
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Select all/Find/Open with?

    Quote Originally Posted by ajames
    2) Find a certain word or phrase in the document and highlight it and make the textox scroll down to that part of the text
    You didn't say you were using a RichTextbox so heres the code for a normal Textbox.
    VB Code:
    1. Dim nPos As Long, nPos2 As Long
    2.  
    3. Const PHRASE As String = "Waddup"
    4.  
    5. With txtTestFile
    6.     nPos = InStr(1, .Text, PHRASE)
    7.     If (nPos) Then
    8.         nPos2 = InStr(nPos, .Text, " ")
    9.         .SelStart = nPos
    10.         If (nPos2) Then
    11.             .SelLength = nPos2 - nPos
    12.           Else
    13.             .SelLength = Len(.Text) - nPos
    14.         End If
    15.     End If
    16. End With

  18. #18

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: Select all/Find/Open with?

    Right, we're getting there. Penagate's find command works, except for two things, the line
    ".SelStart = nPos" needed to be ".SelStart = nPos-1"
    and the line
    ".SelLength = Len(.Text) - nPos" needed to be ".SelLength = Len(.Text) - nPos + 1"

    But moeur's open with command came up with an error like "Error 53 : Bad command or file name".
    Last edited by ajames; Nov 6th, 2005 at 09:31 AM.

  19. #19
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Select all/Find/Open with?

    okeydokey,

    do you start in Sub Main(), or in a Form_Load()? If it is the latter, I suggest you make your startup object Sub Main (Project menu -> Properties).

    Then, you can parse the return value of Command$() in there.
    VB Code:
    1. ' In a .bas module
    2.  
    3. Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsW" ( _
    4.     ByVal pszPath As Long _
    5. ) As Long
    6.  
    7. Private Sub Main()
    8. Dim sFilename As String
    9. Dim sBuffer As String, hFile As Long
    10.     sFilename = Trim$(Command$())
    11.  
    12.     Load <MainFormName>
    13.  
    14.     If (PathFileExists(StrPtr(sFileName)) = 1) Then
    15.         hFile = FreeFile()
    16.         Open sFilename For Binary Lock Write As #hFile
    17.         sBuffer = Space$(LOF(hFile))
    18.         Get #hFile, , sBuffer
    19.         Close #hFile
    20.  
    21.         <MainFormName>.txtTestFile.Text = sBuffer
    22.     End If
    23.  
    24.     <MainFormName.Show
    25. End Sub
    That should work.

  20. #20
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Select all/Find/Open with?

    But moeur's open with command came up with an error like "Error 53 : Bad command or file name".
    On what line of code did you get this error?

  21. #21

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: Select all/Find/Open with?

    Penagate's one doesn't come up with errors but the text box is still blank when I open a file through windows

  22. #22
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Select all/Find/Open with?

    Before the Load <MainForm> line, put this line in
    VB Code:
    1. MsgBox sFileName
    and post what it says when you use Open With.

  23. #23

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: Select all/Find/Open with?

    It says nothing

  24. #24
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Select all/Find/Open with?

    That's impossible, as long as you definitely selected the file and chose to "Open With" and navigated to the latest build of your application. Windows will pass the path of the file as a parameter, which will turn up in the result of the Command() function.

  25. #25

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: Select all/Find/Open with?

    aha, it now shows "C:/Documents and Settings/All User/My Documents/test.txt", but when i press "OK" there is nothing in the textbox

  26. #26
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Select all/Find/Open with?

    Is there text in the file?

    (I'm not being funny, it's happened before... )

  27. #27

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: Select all/Find/Open with?

    Yep. About three sentences

  28. #28

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: Select all/Find/Open with?

    Can anyone help?

  29. #29
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Select all/Find/Open with?

    This does work
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim strBuffer       As String
    3.     Dim intDemoFileNbr  As Integer
    4.     Dim strFileToOpen   As String
    5.     strFileToOpen = Trim(Replace(Command$, Chr(34), " "))
    6.     If Len(strFileToOpen) > 0 Then
    7.         intDemoFileNbr = FreeFile
    8.         Open strFileToOpen For Binary Access Read As #intDemoFileNbr
    9.         strBuffer = Input(LOF(intDemoFileNbr), intDemoFileNbr)
    10.         txtTestFile.Text = strBuffer
    11.         Close #intDemoFileNbr
    12.     End If
    13. End Sub

    I'll also attached the project
    Attached Files Attached Files

  30. #30

    Thread Starter
    Addicted Member ajames's Avatar
    Join Date
    Mar 2005
    Location
    Wales, UK
    Posts
    178

    Re: Select all/Find/Open with?

    Thanks! It works!
    I cannot see why the other ones didn't work. Anyway, that's it, thanks you everyone for all your additions, especially the one's that work

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