Click here to Skip to main content
16,012,028 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hey
i'm working on a notepad project
and i want to open .txt files with my app
it worked
BUT
when i open a file ,modify it and save... save only NOT save as
it opens save as file
i want to modify the opened file

works great when a file is opened from opendialog

What I have tried:

here is the code i'm using
VB
Shared Sub DoubleClickFile(Textbox As TextBox)
    If System.Environment.GetCommandLineArgs().Length > 1 Then
        Dim i As Integer
        For i = 1 To System.Environment.GetCommandLineArgs().Length - 1
            Dim zReader As StreamReader
            zReader = New StreamReader(System.Environment.GetCommandLineArgs(i), True)
            Textbox.Text = zReader.ReadToEnd
        Next
    Else
    End If
End Sub
Posted
Updated 29-Mar-18 22:30pm
v2

Not sure if I understand the question correctly but you seem to loop through the command line arguments. However, each time you read a file based on an argument you overwrite the old content of the textbox.

So perhaps you should have
Textbox.Text = Textbox.Text & zReader.ReadToEnd

Another thing is that you seem start looping the array from index 1. Should the starting position be 0?

EDIT:
-----

Based on the discussion the code should probably be something like
VB
Shared Sub DoubleClickFile(Textbox As TextBox)
    If System.Environment.GetCommandLineArgs().Length > 1 Then
        Dim i As Integer
        For i = 1 To System.Environment.GetCommandLineArgs().Length - 1
            Dim zReader As StreamReader
            zReader = New StreamReader(System.Environment.GetCommandLineArgs(i), True)
            Textbox.Text = zReader.ReadToEnd
            EditorForm.DocumentName = System.Environment.GetCommandLineArgs(i)
        Next
    End If
End Sub
 
Share this answer
 
v3
Comments
Sh3R3iF 30-Mar-18 4:42am    
thanks for reply
when i open a file with double click from file explorer and modify, it uses save as not save, i want to overwrite the current loaded file
but
when i open a file from open dialog and modify, and click save... works
Wendelius 30-Mar-18 4:46am    
I'm a bit confused. The code you've posted has nothing to do with saving a file.
Sh3R3iF 30-Mar-18 4:48am    
Shared Sub Save(txtDisplay As TextBox)
If EditorForm.DocumentName = "Untitled" Then
SaveAs(txtDisplay)
Else
Dim SaveFile As New StreamWriter(EditorForm.DocumentPath)
SaveFile.Write(txtDisplay.Text)
SaveFile.Close()
txtDisplay.Modified = False
End If
End Sub

Shared Sub SaveAs(txtDisplay As TextBox)
Dim SaveFileDialog1 As New SaveFileDialog
With SaveFileDialog1
SaveFileDialog1.Filter = "Text Files (*.txt) |*.txt"
SaveFileDialog1.Title = "Save As"
If .ShowDialog = DialogResult.OK Then
EditorForm.DocumentPath = .FileName
Dim SaveFile As New StreamWriter(EditorForm.DocumentPath)
SaveFile.Write(txtDisplay.Text)
SaveFile.Close()
txtDisplay.Modified = False
Dim FileInfo As New FileInfo(EditorForm.DocumentPath)
EditorForm.DocumentName = FileInfo.Name
End If
End With
End Sub
Sh3R3iF 30-Mar-18 4:49am    
that's for save and save as
my problem with double clicked file
Wendelius 30-Mar-18 7:13am    
If you want to save the document instead of "save as", place a breakpoint on the following line

If EditorForm.DocumentName = "Untitled" Then

Chances are that the DocumentName property is "Untitled" even if you have opened an existing file.

If this is the case, modify the method that opens the file to set a proper value to DocumentName property.

Have a look at the modified answer
You already posted this question in the Visual Basic forum. Please do not repost.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900