Member Avatar for hueikar

Hi guys,
I have the following coding and i would like to save it using save file dialog. Any idea?
Thnx.

Dim numCols As Integer = dgvExp.ColumnCount
        Dim numRows As Integer = dgvExp.RowCount - 1
        Dim strDestinationFile As String = "c:\username.txt"
        Dim tw As TextWriter = New StreamWriter(strDestinationFile)

        'writing the header
        For count As Integer = 0 To numCols - 1
            tw.Write(dgvExp.Columns(count).HeaderText)
            If (count <> numCols - 1) Then
                tw.Write(", ")
            End If
        Next
        tw.WriteLine()
        For count As Integer = 0 To numRows - 1
            For count2 As Integer = 0 To numCols - 1
                tw.Write(dgvExp.Rows(count).Cells(count2).Value)
                If (count2 <> numCols) Then
                    tw.Write(", ")
                End If
            Next
            tw.WriteLine()
        Next
        tw.Close()
    End Sub

Can you copy/paste it into notepad then do a save or saveas?

Try this one

1. Add SaveFileDialog control
2. name it savefile (or any other name)

Dim getfile As String = String.Empty

        With savefile
            .AutoUpgradeEnabled = True 'gets or sets a value indication for dialog upgrade in case of windows vista
            .CheckPathExists = True 'check whether the selected path exists or not
            .CreatePrompt = True 'ask permission from user to create file
            .InitialDirectory = Environment.SpecialFolder.Desktop 'gets or sets initial display directory
            .OverwritePrompt = True 'gives warning if file already exists
            .RestoreDirectory = True 'restores the last directory
            .ShowDialog() 'shows the dialog can also use .show() do it urself to check the difference
            getfile = .FileName 'retrieves the filename provided by user
        End With

        My.Computer.FileSystem.WriteAllText(getfile, "Your text here", True) 'last option for appending text if false overwrites preexisting content

        'u can also use streamwriter

rate me if u like this !! :)

Hi Hueikar

Use the following code for using savefiledialog

Private Sub SaveAsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveToolStripMenuItem.Click, SaveToolStripButton.Click

Dim sfd = New SaveFileDialog
sfd.DefaultExt = ".cbf" '--You can change the format type
sfd.Filter = "(Your) Object Files (*.cbf)|*.cbf" '----Same as the DefaultExt
sfd.ShowDialog()

If sfd.FileName <> "" Then

Dim psf As Stream = File.Create(sfd.FileName)


'<YOUR CODE USING STREAM WRITER, SERIALIZER, etc.>

''Dim serializer As New BinaryFormatter
''serializer.Serialize(psf, cbobject)
psf.Close()
End If
End Sub


Rgrds

Sam

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.