vb.net 使用 SaveFileDialog 将数据保存到文本图块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15420593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:44:24  来源:igfitidea点击:

Save Data to text tile using SaveFileDialog?

vb.netfiletextdialogsave

提问by Matt

I have already viewed the MSDN Examplebut I am still having problems.

我已经查看了MSDN 示例,但我仍然遇到问题。

I created a super-simple program to multiply two numbers, and display the output in the textbox. Now I need to be able to read that text box value and put the value in a text file, bringing up the save to file dialog when the "Save To File" button is clicked.

我创建了一个超级简单的程序来将两个数字相乘,并在文本框中显示输出。现在我需要能够读取该文本框值并将该值放入文本文件中,当单击“保存到文件”按钮时打开保存到文件对话框。

Private Sub MutiplyBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MutiplyBtn.Click
    Dim FirstNum As Double = Num1.Text
    Dim SecondNum As Double = Num2.Text
    Dim Answer2 As Double = FirstNum * SecondNum
    Answerbox.Text = Answer2
End Sub

Private Sub SaveResultToFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveResultToFile.Click
    Dim myStream As Stream
    Dim saveFileDialog1 As New SaveFileDialog()

    saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
    saveFileDialog1.FilterIndex = 2
    saveFileDialog1.RestoreDirectory = True

    If saveFileDialog1.ShowDialog() = DialogResult.OK Then
        myStream = saveFileDialog1.OpenFile()
        If (myStream IsNot Nothing) Then
            System.IO.File.WriteAllText(Answerbox.Text)
            myStream.Close()
        End If
    End If
End Sub

Currently, Visual Studio is giving me an error: Overload resolution failed because no accessible 'WriteAllText' accepts this number of arguments.

目前,Visual Studio 给我一个错误: Overload resolution failed because no accessible 'WriteAllText' accepts this number of arguments.

回答by Steve

WriteAllTextstatic method requires the name of the file where the data should be written to.
You could use directly the name selected in the saveFileDialog1

WriteAllText静态方法需要数据应写入的文件的名称。
您可以直接使用在 saveFileDialog1 中选择的名称

If saveFileDialog1.ShowDialog() = DialogResult.OK Then
    System.IO.File.WriteAllText(saveFiledialog1.FileName, Answerbox.Text)
End If

instead if you really want to use the stream opened by OpenFile() method your code should be

相反,如果你真的想使用 OpenFile() 方法打开的流,你的代码应该是

   If saveFileDialog1.ShowDialog() = DialogResult.OK Then
        Dim sw As StreamWriter = new StreamWriter(saveFileDialog1.OpenFile())
        If (sw IsNot Nothing) Then 
            sw.WriteLine(Answerbox.Text)
            sw.Close()
        End If 
    End If 

The code is an example, you need to add a bit of error handling

代码是一个例子,需要加一点错误处理

回答by DEV RAJ

Hi I tried above method but I succeed in this way....

嗨,我尝试了上述方法,但我以这种方式成功了....

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
   SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt"
      If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK _
      Then
         My.Computer.FileSystem.WriteAllText _
         (SaveFileDialog1.FileName, RichTextBox1.Text, True)
      End If
End Sub