-
[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
-
Re: Select all/Find/Open with?
Quote:
Originally Posted by ajames
1) Select all text in a textbox called txtTestFile
VB Code:
Private Sub Command4_Click()
txtTestFile.SelStart = 0
txtTestFile.SelLength = Len(txtTestFile.Text)
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:
Private Sub ColorWord(prtbName As RichTextBox, _
pstrWord As String, _
plngHighlightColor As Long, _
Optional pblnBold As Boolean)
Dim lngPos As Long
Dim lngSelStart As Long
Dim lngSelLength As Long
Dim blnRetry As Boolean
lngSelStart = prtbName.SelStart
lngSelLength = prtbName.SelLength
lngPos = prtbName.Find(pstrWord, 0)
Do
blnRetry = False
prtbName.SelColor = plngHighlightColor
If pblnBold = True Then
prtbName.SelBold = True
End If
lngPos = prtbName.Find(pstrWord, lngPos + prtbName.SelLength)
Loop While lngPos > 0
prtbName.SelStart = lngSelStart
prtbName.SelLength = lngSelLength
End Sub
Private Sub Command1_Click()
If chkBold = vbChecked Then
ColorWord RichTextBox1, Text1.Text, vbRed, True
Else
ColorWord RichTextBox1, Text1.Text, vbRed
End If
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?
-
Re: Select all/Find/Open with?
Quote:
Originally Posted by Hack
How are you selecting and showing it?
In what way do you mean?
-
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.
-
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
-
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:
Private Sub openmenu_Click()
Dim strBuffer As String
Dim intDemoFileNbr As Integer
Dim strFileToOpen As String
On Error GoTo openmenu_Click_Exit
With dlgDemo
.CancelError = True
.InitDir = mstrLastDir
.Flags = cdlOFNHideReadOnly
.FileName = ""
.Filter = "Text Files(*.txt)|*.txt|All Files(*.*)|*.*"
.ShowOpen
strFileToOpen = .FileName
End With
On Error GoTo openmenu_Click_Error
intDemoFileNbr = FreeFile
Open strFileToOpen For Binary Access Read As #intDemoFileNbr
strBuffer = Input(LOF(intDemoFileNbr), intDemoFileNbr)
txtTestFile.Text = strBuffer
Close #intDemoFileNbr
mstrLastDir = Left$(strFileToOpen, InStrRev(strFileToOpen, "\") - 1)
Exit Sub
openmenu_Click_Error:
MsgBox "The following error has occurred:" & vbNewLine _
& "Err # " & Err.Number & " - " & Err.Description, _
vbCritical, _
"Open Error"
openmenu_Click_Exit:
End Sub
however, It refuses to open if I open it through windows
-
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.
-
Re: Select all/Find/Open with?
I cannot access the registry to do this.
-
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.
-
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
-
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.
-
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:
Private Sub Form_Load()
Dim strBuffer As String
Dim intDemoFileNbr As Integer
Dim strFileToOpen As String
If Len(Command$) > 0 Then
intDemoFileNbr = FreeFile
Open Command$ For Binary Access Read As #intDemoFileNbr
strBuffer = Input(LOF(intDemoFileNbr), intDemoFileNbr)
txtTestFile.Text = strBuffer
Close #intDemoFileNbr
mstrLastDir = Left$(strFileToOpen, InStrRev(strFileToOpen, "\") - 1)
End If
End Sub
-
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.
-
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.
-
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.
-
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?
-
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:
Dim nPos As Long, nPos2 As Long
Const PHRASE As String = "Waddup"
With txtTestFile
nPos = InStr(1, .Text, PHRASE)
If (nPos) Then
nPos2 = InStr(nPos, .Text, " ")
.SelStart = nPos
If (nPos2) Then
.SelLength = nPos2 - nPos
Else
.SelLength = Len(.Text) - nPos
End If
End If
End With
-
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".
-
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:
' In a .bas module
Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsW" ( _
ByVal pszPath As Long _
) As Long
Private Sub Main()
Dim sFilename As String
Dim sBuffer As String, hFile As Long
sFilename = Trim$(Command$())
Load <MainFormName>
If (PathFileExists(StrPtr(sFileName)) = 1) Then
hFile = FreeFile()
Open sFilename For Binary Lock Write As #hFile
sBuffer = Space$(LOF(hFile))
Get #hFile, , sBuffer
Close #hFile
<MainFormName>.txtTestFile.Text = sBuffer
End If
<MainFormName.Show
End Sub
That should work.
-
Re: Select all/Find/Open with?
Quote:
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?
-
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
-
Re: Select all/Find/Open with?
Before the Load <MainForm> line, put this line in
and post what it says when you use Open With.
-
Re: Select all/Find/Open with?
-
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.
-
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
-
Re: Select all/Find/Open with?
Is there text in the file?
(I'm not being funny, it's happened before... :))
-
Re: Select all/Find/Open with?
Yep. About three sentences
-
Re: Select all/Find/Open with?
-
1 Attachment(s)
Re: Select all/Find/Open with?
This does work
VB Code:
Private Sub Form_Load()
Dim strBuffer As String
Dim intDemoFileNbr As Integer
Dim strFileToOpen As String
strFileToOpen = Trim(Replace(Command$, Chr(34), " "))
If Len(strFileToOpen) > 0 Then
intDemoFileNbr = FreeFile
Open strFileToOpen For Binary Access Read As #intDemoFileNbr
strBuffer = Input(LOF(intDemoFileNbr), intDemoFileNbr)
txtTestFile.Text = strBuffer
Close #intDemoFileNbr
End If
End Sub
I'll also attached the project
-
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