i making a list of names to be entered in 10 boxes. no i need to save this as document or text file. how do i use savefiledialog() in vb .

Dani AI

Generated

asked how to save the ten name boxes; and pointed toward SaveFileDialog and a simple File.WriteAllText approach. For a reliable WinForms pattern that preserves order, handles encoding and errors, and is easy to wire to a Save menu or button, collect the TextBox values explicitly, prompt for a filename, then write lines with a StreamWriter. The example below writes one name per line (UTF-8) and shows basic exception handling.

' collect textboxes in the exact order required
Dim boxes As TextBox() = {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5,
                          TextBox6, TextBox7, TextBox8, TextBox9, TextBox10}

Using dlg As New SaveFileDialog()
    dlg.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
    dlg.DefaultExt = "txt"
    dlg.AddExtension = True
    dlg.OverwritePrompt = True
    dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

    If dlg.ShowDialog() = DialogResult.OK Then
        Try
            Using fs As IO.Stream = dlg.OpenFile()
                Using sw As New IO.StreamWriter(fs, System.Text.Encoding.UTF8)
                    For Each tb As TextBox In boxes
                        sw.WriteLine(tb.Text.Trim())
                    Next
                End Using
            End Using
        Catch ex As IO.IOException
            MessageBox.Show("Save failed: " & ex.Message)
        End Try
    End If
End Using

Notes and quick tips that expand on earlier replies: set OverwritePrompt = True to avoid accidental overwrite and AddExtension = True so a missing ".txt" is added. Explicitly listing the TextBoxes preserves the intended order; alternatively use Controls.OfType(Of TextBox)().OrderBy(Function(t) t.TabIndex) if TabIndex ordering is relied upon. To skip empty entries, write only when tb.Text.Trim() <> "". For name lists with commas or special characters consider saving CSV (escape quotes) or use File.WriteAllLines if the values are already in a string array.

Common pitfalls: file-permission errors or open handles will throw IO exceptions (the try/catch above handles that). If the goal was a formatted document (.docx) rather than plain text, a different library is required. This complements the quick snippets from and with a production-ready pattern.

Recommended Answers

All 4 Replies

First make the form and add what you want.Then go to file menu and click to save command or cick ctrl+S.

can u give the code ?

First you add a FileSaveDialog object to your form by dragging and dropping from the toolbox. After you have done that, select the control and browse the properties. Each property has a description that explains what it is for. To prompt the user for a filename you do

If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    MsgBox("you selected " & SaveFileDialog1.FileName)
End If

You must search using google before posting any question. Following is the code to save text file

        Dim svFileDialog As New SaveFileDialog
        svFileDialog.Filter = "Text file|*.txt"
        svFileDialog.Title = "Save text file"
        svFileDialog.ShowDialog()

        If Not svFileDialog.FileName = "" Then
            System.IO.File.WriteAllText(svFileDialog.FileName, "your Text")
        End If
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.