Question:

How do you code a button that when clicked saves all text in a text box?

by  |  earlier

0 LIKES UnLike

I'm trying to make a copy and save program for convenence. I want to make a program that when i paste text into a text box, i can hit a save button next to it that will save the text to .txt. I know that you can usually just type .txt and it will save as a text file. but so far i can make the save window pop up but when i type a name for the .txt(ex:testing.txt) nothing saves. If anyone could help me out that would be great. I'm very new to visual basic but i cant wait to learn the language.

 Tags:

   Report

1 ANSWERS


  1. VB2008or VB2008Express

    Add a textbox, button and a SaveFileDialog...

    Cut and paste this into the code pane:

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim filename As String

    Try

    With SaveFileDialog1

    .AddExtension = True

    .CheckPathExists = True

    .CreatePrompt = False

    .OverwritePrompt = True

    .ValidateNames = True

    .ShowHelp = True

    .DefaultExt = "txt"

    .FileName = filename

    .Filter = _

    "Text files (*.txt)|*.txt|" & "All files|*.*"

    .FilterIndex = 1

    If .ShowDialog() = Windows.Forms.DialogResult.OK Then

    My.Computer.FileSystem.WriteAllText(.F... TextBox1.Text, False)

    End If

    End With

    Catch ex As Exception

    MsgBox(ex.Message, MsgBoxStyle.Exclamation, Me.Text)

    End Try

    End Sub

    End Class

    : )

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions