|
-
May 25th, 2006, 04:34 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Open file in textbox
Hi again!
I need help with opening a textfile into a textbox...I thought I maybe could do something like this:
VB Code:
Open "c:/test.txt" For Input As #1
Do Until 8 = 5
On Error GoTo ready
Input #1, newline
Text1.Text = Text1 & vbCrLf & newline
Loop
ready:
Close
But as you understand everything that's in Text1 goes on one line and the newline thing goes on another...but I want every new input to go on a new line...do you understand what I'm looking for?
Please help!
//Alex
Last edited by cyber_alex; May 25th, 2006 at 04:38 AM.
-
May 25th, 2006, 04:38 AM
#2
Re: Open file in textbox
VB Code:
Private Sub itmOpen_Click()
Dim strFileName As String 'String of file to open
Dim strText As String 'Contents of file
Dim strFilter As String 'Common Dialog filter string
Dim strBuffer As String 'String buffer variable
Dim FileHandle% 'Variable to hold file handle
'Set the Common Dialog filter
strFilter = "Text (*.txt)|*.txt|All Files (*.*)|*.*"
cdMain.Filter = strFilter
'Open the common dialog
cdMain.ShowOpen
'Make sure the retrieved filename is not a blank string
If cdMain.filename <> "" Then
'If it is not blank open the file
strFileName = cdMain.filename
'Get a free file handle and assign it to the file handle variable
FileHandle% = FreeFile
'Open the file
Open strFileName For Input As #FileHandle%
'Make the mouse cursor an hourglass
MousePointer = vbHourglass
'Traverse the lines of the file
Do While Not EOF(FileHandle%) ' Check for end of file.
'Read a line of the file
Line Input #FileHandle%, strBuffer ' Read line of data.
'Add the line from the output buffer to the text string
strText = strText & strBuffer & vbCrLf
Loop
'Change the mousepointer back to the arrow
MousePointer = vbDefault
'Close the file once you have had your way with it
Close #FileHandle%
'Assign the retrieved text to the text box
txtMain.Text = strText
'Put the file name in the form caption
frmMain.Caption = "Text Editor- [" & strFileName & "]"
End If
End Sub
You need a commondialog control in there as well
VB.NET MVP 2008 - Present
-
May 25th, 2006, 04:38 AM
#3
Re: Open file in textbox
VB Code:
Dim sText As String
Open "C:\test.txt" For Input As #1
sText = Input(LOF(1), #1)
Close #1
Text1.Text = sText
-
May 25th, 2006, 04:46 AM
#4
Thread Starter
Addicted Member
Re: Open file in textbox
I did what you said and adjusted my code a bit...and it works perfect!!
Tnx a lot HanneSThEGreaT!!
I'll reputate you at once!
-
May 25th, 2006, 04:49 AM
#5
Thread Starter
Addicted Member
Re: Open file in textbox
wow!! Your stuff was even better bushmobile! (no offense HanneSThEGreaT )
How do I save the stuff that is in the textbox now?
-
May 25th, 2006, 04:51 AM
#6
Re: Open file in textbox
 Originally Posted by cyber_alex
wow!! Your stuff was even better bushmobile! (no offense HanneSThEGreaT  )
None taken
Save:
VB Code:
Private Sub itmSave_Click()
Dim strFileName As String 'String of file to open
Dim strText As String 'Contents of file
Dim strFilter As String 'Common Dialog filter string
Dim strBuffer As String 'String buffer variable
Dim FileHandle% 'Variable to hold file handle
'Set the Common Dialog filter
strFilter = "Text (*.txt)|*.txt|All Files (*.*)|*.*"
cdMain.Filter = strFilter
'Open the common dialog in save mode
cdMain.ShowSave
'Make sure the retrieved filename is not a blank string
If cdMain.filename <> "" Then
'If it is not blank open the file
strFileName = cdMain.filename
'Assign a value to the text variable
strText = txtMain.Text
'Get a free file handle and assign it to the file handle variable
FileHandle% = FreeFile
'Open a file for writing
Open strFileName For Output As #FileHandle%
'Set an hour glass cursor just in case it takes a while
MousePointer = vbHourglass
'Do the write
Print #FileHandle%, strText
'Reset the cursor to the Windows default.
MousePointer = vbDefault
'Close the file once you have had your way with it
Close #FileHandle%
End If
End Sub
VB.NET MVP 2008 - Present
-
May 25th, 2006, 04:52 AM
#7
Re: Open file in textbox
VB Code:
Open "C:\output.txt" For Output As #1
Print #1, Text1.Text
Close #1
-
May 25th, 2006, 04:53 AM
#8
Thread Starter
Addicted Member
Re: Open file in textbox
hmm..better wait a minute or two to see if bushmobile comes up with a tiny version of your code...
-
May 25th, 2006, 04:54 AM
#9
Thread Starter
Addicted Member
Re: Open file in textbox
HAHA!! I knew it!! Your so funny  lol:
-
May 25th, 2006, 04:54 AM
#10
Re: Open file in textbox
 Originally Posted by cyber_alex
hmm..better wait a minute or two to see if bushmobile comes up with a tiny version of your code... 
this time the code's are pretty much identical, i just have bothered putting the commondialog stuff in.
-
May 25th, 2006, 04:57 AM
#11
Thread Starter
Addicted Member
Re: Open file in textbox
Tnx both of you...my program runs perfect now!
-
May 25th, 2006, 05:06 AM
#12
Re: Open file in textbox
This is a funny thread
But there's actually a lesson that can be learnt here..
Cyber_Alex, so you see that there are always multiple ways to solve one problem. If you were to work within a group, and the group was given a certain task to do. Everyone within that group, will most probably come up with different ideas / implementations to get the same result
VB.NET MVP 2008 - Present
-
May 25th, 2006, 05:10 AM
#13
Thread Starter
Addicted Member
Re: Open file in textbox
yeah, your right...that's what makes programming an art! 
..this really has been a touching thread...
-
May 25th, 2006, 06:31 AM
#14
Re: [RESOLVED] Open file in textbox
Now, cyber_alex, put them both together in one Sub the can perform saving and loading
VB Code:
Private Sub SaveLoadTextBox(pstrOperation As String, pstrFileName As String)
Select Case pstrOperation
Case "save"
'run HanneSThEGreaT code
Case "load"
'run bushmobiles code
End Select
End Sub
Private Sub cmdSave_Click()
SaveLoad "save", "c:\cyber_alex.txt"
End Sub
Private Sub Form_Load()
SaveLoad "load", c:\cyber_alex.txt"
End Sub
-
Jun 6th, 2006, 05:31 PM
#15
Lively Member
Re: Open file in textbox
 Originally Posted by bushmobile
VB Code:
Dim sText As String
Open "C:\test.txt" For Input As #1
sText = Input(LOF(1), #1)
Close #1
Text1.Text = sText
is this the same as inputting to the textbox? i created a form with combo boxes and textbox. when the program runs, the textbox should be a field for input. is this the same?
-
Jun 6th, 2006, 05:38 PM
#16
PowerPoster
Re: [RESOLVED] Open file in textbox
That snippet would load the file into a string then put that string into a textbox...is that what you mean? If not, explain further plz :-)
-
Jun 6th, 2006, 05:51 PM
#17
Lively Member
Re: [RESOLVED] Open file in textbox
let's say you have a gui , and on this gui, u have a textbox , combo boxes...when you compile...you get your form loaded... you want to input filename in the textbox...and then store this string to pass in another parameter or write to a text file , i am not sure how this information is stored actually!
-
Jun 6th, 2006, 05:59 PM
#18
PowerPoster
Re: [RESOLVED] Open file in textbox
Ah, you want the FILENAME to tell the program what file to open and that file to be stored in a string?
VB Code:
Dim sText As String
Open txtFile.text For Input As #1
sText = Input(LOF(1), #1)
Close #1
That would open the location stored in textbox txtFile and write it to sText (string). If you're doing a lot of loading and saving, I suggest you replace all #1 with #fre and at the start (after the Dim) put fre=freefile (which basically works out the next free file number to use)
-
Jun 6th, 2006, 06:11 PM
#19
Lively Member
Re: [RESOLVED] Open file in textbox
hi smUX, actually this question here is pretty related to the other question i have about stored procedures...that you answered...let me think this through and explain it more clearly as to what i'm trying to do. thanks so far..
-
Jun 7th, 2006, 07:38 AM
#20
Thread Starter
Addicted Member
Re: [RESOLVED] Open file in textbox
seems like this thread just woke up again!
-
Jun 7th, 2006, 06:25 PM
#21
Lively Member
Re: [RESOLVED] Open file in textbox
 Originally Posted by smUX
Ah, you want the FILENAME to tell the program what file to open and that file to be stored in a string?
VB Code:
Dim sText As String
Open txtFile.text For Input As #1
sText = Input(LOF(1), #1)
Close #1
That would open the location stored in textbox txtFile and write it to sText (string). If you're doing a lot of loading and saving, I suggest you replace all #1 with #fre and at the start (after the Dim) put fre=freefile (which basically works out the next free file number to use)
yes, i want that. But i've changed my form so there are two options. I have added from menu Editor, the "file" so that i can go to open and select the file i want to store. So with this GUI interface, what i'm getting is, i don't need to make these events like the SQL statements i see in that code from the link i showed you? I would do it just as vb scripts that i've been doing? I got confused when my coworker said he'll do stored procedures...
i was thinking if i would have to wait on his stored procedures to put in my program. But if i'm not really dealing with the DB (not my part) then i dont need to worry about the sql statements, right?? because there are no "add", "delete", "search"...commands in my GUI interface...
-
Jun 10th, 2006, 10:56 AM
#22
Addicted Member
Re: Open file in textbox
 Originally Posted by bushmobile
VB Code:
Dim sText As String
Open "C:\test.txt" For Input As #1
sText = Input(LOF(1), #1)
Close #1
Text1.Text = sText
Hi there,
I used this code to try and read a text file with multiple lines into a text box for display on multiple lines. However, the text that was loaded appears with the first line followed by two black squares and then the second line, all displayed on the first line of the text box.
How do I fix this so that both lines in the text file do actually appear on separate lines in the text box?
-
Jun 10th, 2006, 10:59 AM
#23
PowerPoster
Re: [RESOLVED] Open file in textbox
Try setting the textbox as multiline, the two boxes are the VbCrLf at the end of each line :-)
Note: If it *IS* multiline, copy one of the black boxes and go to immediate window then type "print asc("X")" (replace the X with the black box) and tell us the value :-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Jun 10th, 2006, 11:03 AM
#24
Addicted Member
Re: [RESOLVED] Open file in textbox
 Originally Posted by smUX
Try setting the textbox as multiline, the two boxes are the VbCrLf at the end of each line :-)
Note: If it *IS* multiline, copy one of the black boxes and go to immediate window then type "print asc("X")" (replace the X with the black box) and tell us the value :-)
That fixed it, thank you.
Also, in my code there is this line:
VB Code:
TerminateProcess ("notepad.exe")
How do I make it so that the TerminateProcess runs and will close every single process listed within the text file, rather than just the single one entered?
-
Jun 10th, 2006, 11:07 AM
#25
PowerPoster
Re: [RESOLVED] Open file in textbox
Ah, this is a good question...not much of a challenge, but a good question :-)
First thing you need to do is split the string up into an array...simple matter of doing
VB Code:
process = split (textinput, vbcrlf)
which assumes the text file input was saved to textinput (rename as appropriate. Now in process() you have a list of the programs (I assume that's what you mean) so you now need to know how many there are...a simple ubound(process()) will tell you this...then call using a for/next loop
VB Code:
for proc = 0 to ubound(process())
TerminateProcess (process(proc))
next proc
I can't guarantee that works 100% as I'm writing from memory, without "terminateprocess()" function and without VB, but should only need minor tweaks :-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Jun 10th, 2006, 11:18 AM
#26
Addicted Member
Re: [RESOLVED] Open file in textbox
 Originally Posted by smUX
Ah, this is a good question...not much of a challenge, but a good question :-)
First thing you need to do is split the string up into an array...simple matter of doing
VB Code:
process = split (textinput, vbcrlf)
which assumes the text file input was saved to textinput (rename as appropriate. Now in process() you have a list of the programs (I assume that's what you mean) so you now need to know how many there are...a simple ubound(process()) will tell you this...then call using a for/next loop
VB Code:
for proc = 0 to ubound(process())
TerminateProcess (process(proc))
next proc
I can't guarantee that works 100% as I'm writing from memory, without "terminateprocess()" function and without VB, but should only need minor tweaks :-)
I haven't opened the text file yet on this form, so how do I open it while saving the contents into an array? I've never used arrays before with the exception of a small animation.
-
Jun 10th, 2006, 11:21 AM
#27
PowerPoster
Re: [RESOLVED] Open file in textbox
You originally quoted a piece of code:
VB Code:
Dim sText As String
Open "C:\test.txt" For Input As #1
sText = Input(LOF(1), #1)
Close #1
Text1.Text = sText
Simply remove the last line, and in my code where I put "textinput" you put "sText"...put that file load code just before my split() line and it should load, split then action each terminateprocess :-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Jun 10th, 2006, 11:25 AM
#28
Addicted Member
Re: [RESOLVED] Open file in textbox
 Originally Posted by smUX
You originally quoted a piece of code:
VB Code:
Dim sText As String
Open "C:\test.txt" For Input As #1
sText = Input(LOF(1), #1)
Close #1
Text1.Text = sText
Simply remove the last line, and in my code where I put "textinput" you put "sText"...put that file load code just before my split() line and it should load, split then action each terminateprocess :-)
That was for a form where the process list could be edited, on this form the text is not loaded into a text box. Is there a way to do this without a text box, or would I need to just make an invisible one?
Also, don't I need to define the process array somewhere/how?
Last edited by gt123; Jun 10th, 2006 at 11:31 AM.
-
Jun 10th, 2006, 11:41 AM
#29
PowerPoster
Re: [RESOLVED] Open file in textbox
 Originally Posted by gt123
That was for a form where the process list could be edited, on this form the text is not loaded into a text box. Is there a way to do this without a text box, or would I need to just make an invisible one?
I don't fully understand. If you want the process list *edited* then the processes that are listed in the text box to be terminated you leave the load file bit as is but instead change MY code to take its data from text1.text (or whatever you call the text box) rather than inputtext.
If you don't need the textbox, the previous code I told you should work (where you work directly from the string to the array). The text data is just as usable whether it's in a string or a textbox :-)
 Originally Posted by gt123
Also, don't I need to define the process array somewhere/how?
Nope, split() does that for you :-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Jun 10th, 2006, 11:43 AM
#30
Addicted Member
Re: [RESOLVED] Open file in textbox
I managed to adapt it slightly and it now works, the code is as follows:
VB Code:
Private Sub EndAll()
Dim sText As String
Dim Process() As String
Dim proc As Integer
Open App.Path & "\processlist.txt" For Input As #1
sText = Input(LOF(1), #1)
Close #1
Process = Split(sText, vbCrLf)
For proc = 0 To UBound(Process())
TerminateProcess (Process(proc))
Next proc
End Sub
Thanks alot.
-
Jun 10th, 2006, 11:54 AM
#31
PowerPoster
Re: [RESOLVED] Open file in textbox
Looks about right...dunno why you're getting processes through a text file though, I'm sure other people here can tell you how to get a list of running processes directly within VB :-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Jun 10th, 2006, 11:56 AM
#32
Addicted Member
Re: [RESOLVED] Open file in textbox
 Originally Posted by smUX
Looks about right...dunno why you're getting processes through a text file though, I'm sure other people here can tell you how to get a list of running processes directly within VB :-)
The program is to terminate a list of specified processes via a button click or hotkey. The text file just contains that list of processes to be closed.
-
Jun 10th, 2006, 12:02 PM
#33
PowerPoster
Re: [RESOLVED] Open file in textbox
Ah, then that should work perfectly :-)
Although I have one suggestion:
VB Code:
Open App.Path & "\processlist.txt" For Input As #1
sText = Input(LOF(1), #1)
Close #1
Change it to
VB Code:
fre = freefile
Open App.Path & "\processlist.txt" For Input As #fre
sText = Input(LOF(1), #fre)
Close #fre
And of course declare fre as integer. This ensures that if you have any other files opened it doesn't crash as it opens using a free handle:-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Jun 10th, 2006, 12:36 PM
#34
Addicted Member
Re: [RESOLVED] Open file in textbox
 Originally Posted by smUX
Ah, then that should work perfectly :-)
Although I have one suggestion:
VB Code:
Open App.Path & "\processlist.txt" For Input As #1
sText = Input(LOF(1), #1)
Close #1
Change it to
VB Code:
fre = freefile
Open App.Path & "\processlist.txt" For Input As #fre
sText = Input(LOF(1), #fre)
Close #fre
And of course declare fre as integer. This ensures that if you have any other files opened it doesn't crash as it opens using a free handle:-)
Done, thanks
-
Jun 10th, 2006, 12:47 PM
#35
PowerPoster
Re: [RESOLVED] Open file in textbox
Glad I could help...I may be a bad programmer at times but I've a wealth of knowledge about certain aspects of programming...you know the saying, Jack of all trades and master of none :-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Jun 10th, 2006, 01:00 PM
#36
Addicted Member
Re: [RESOLVED] Open file in textbox
 Originally Posted by smUX
Glad I could help...I may be a bad programmer at times but I've a wealth of knowledge about certain aspects of programming...you know the saying, Jack of all trades and master of none :-)
Well, you managed to help me just fine. The only thing i'm wondering about is having a customisable hotkey. It's currently set to F10 using the following code:
VB Code:
Private Sub tmrHotkey_Timer()
'Responds to the hotkey F10 and runs the sub EndAll
Dim HotOnceOnly As Boolean
If Not GetAsyncKeyState(121) = 0 Then
If (HotOnceOnly = False) Then
EndAll
End If
HotOnceOnly = True
ElseIf GetAsyncKeyState(121) = 0 Then HotOnceOnly = False 'Can you else if only one hotkey per timer
End If
End Sub
Does anyone have any idea how I can change this to a key combination within the code, or have the option to customize the hotkey completely?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|