简体   繁体   中英

How to display open file name in "Save As" dialogue box with Visual Basic?

I'm learning Visual Basic and I need to know how when I click "Save" on my GUI and the Save Dialogue box pops up, the current open file name is already being displayed in "File Name: xxxxxx"

This is what I have so far: Imports System.IO Public Class Form1

Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Dim dr As DialogResult
    dr = dlgSave.ShowDialog()
    If dr = Windows.Forms.DialogResult.OK Then
        Dim writerVar As StreamWriter
        writerVar = New StreamWriter(dlgSave.FileName, False)
        writerVar.Write(txtEdit.Text)
        writerVar.Close()
    End If
End Sub

Cheers, Alex

This should help you out:

        ' Shows the use of a SaveFileDialog to save a MemoryStream to a file.
Private Sub Button2_Click(ByVal sender As Object, _
    ByVal e As EventArgs) Handles Button2.Click

    ' Set the properties on SaveFileDialog1 so the user is 
    ' prompted to create the file if it doesn't exist 
    ' or overwrite the file if it does exist.
    SaveFileDialog1.CreatePrompt = True
    SaveFileDialog1.OverwritePrompt = True

    ' Set the file name to myText.txt, set the type filter
    ' to text files, and set the initial directory to the 
    ' MyDocuments folder.
    SaveFileDialog1.FileName = "myText"
    ' DefaultExt is only used when "All files" is selected from 
    ' the filter box and no extension is specified by the user.
    SaveFileDialog1.DefaultExt = "txt"
    SaveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
    SaveFileDialog1.InitialDirectory = _
        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

Source: https://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.filename(v=vs.110).aspx

I recommend you read more into the FileName property there in order to find what you're looking for :-) hopefully that solves your problem!

Edit: Changed code to VB. Sorry, should have read the question more clearly. Been doing too much C# today..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM