在 Visual Basic 中按下按钮
我是 Visual Basic.NET 的新手,我只是在玩弄它。我有一本书告诉我如何从文件中读取数据,但不告诉我如何通过单击按钮写入文件。我所拥有的只是一个按钮和一个名为 fullNameBox 的文本框。当我单击该按钮时,它给我一个未处理的异常错误。这是我的代码:
Public Class Form1
Sub outputFile()
Dim oWrite As System.IO.StreamWriter
oWrite = System.IO.File.CreateText("C:\sample.txt")
oWrite.WriteLine(fullNameBox.Text)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
outputFile()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
I am new to Visual Basic.NET and I am just playing around with it. I have a book that tells me how to read from a file but not how to write to the file with a button click. All I have is a button and a textbox named fullNameBox. When I click the button it gives me an unhandled exception error. Here is my code:
Public Class Form1
Sub outputFile()
Dim oWrite As System.IO.StreamWriter
oWrite = System.IO.File.CreateText("C:\sample.txt")
oWrite.WriteLine(fullNameBox.Text)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
outputFile()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过单步执行您的应用程序以查看错误所在?快速浏览一下,您可能需要在第四行 (oWrite = IO.File...) 使用 System.IO.File 而不仅仅是 IO,但我还没有尝试运行它。
Have you tried stepping through your application to see where the error is? With a quick glance, it looks like you might need to use System.IO.File on the fourth line (oWrite = IO.File...) instead of just IO, but I haven't tried to run it.
这是一个完整的功能程序,如果您愿意,您只需拖放一个文本框、openfiledialog 和 savefiledialog 即可。
请随意使用代码。
享受
顺便说一句,代码中的问题是您“必须”在使用完文件流后关闭它,这样做将释放任何资源,例如套接字和文件句柄。
this is a complete functional program if you want, you just need to drag drop a textbox, openfiledialog, and a savefiledialog.
feel free to play around with the code.
enjoy
by the way, the problem in your code is that you "must" close filestream when your done using it, doing so will release any resource such as sockets and file handles.
.net框架是一个非常强大的框架。但同样地,它对于简单的任务也有简单方便的方法。大多数人倾向于将事情复杂化以展示知识。但更少的代码 = 更少的处理 = 更快、更高效的应用程序(有时),所以上面的大方法可能不适合。除此之外,上面提到的方法最好写成一个子函数,或者如果返回一些东西,那么最好写成一个函数。
一个一般示例是
也可以更改为字段的当前文本。
因此,一个更具体的示例是
如果您想了解代码的append部分
通过设置append = true,您可以允许您的应用程序将文本写入文件末尾,使文件中已有的其余文本保持不变。
通过设置append = false,您将删除现有文件中的所有文本并用新文本替换
如果您不想编写这部分代码(尽管它很小),您可以创建一个子程序来处理它,但是该方法会略有不同,只是为了礼仪。功能将保持相似。 (使用StreamWriter)
特定示例将
位于button_click事件调用下:
您也可以进一步采取这一步骤。如果您想创建一个更高级的 Sub 来处理任何文本框和文件。
假设您计划拥有多个单独的文件和每个文件的多个字段(尽管有一个MUCH更干净、更优雅的方法),您可以创建一个函数。 {我将在此示例中尽可能彻底地解释函数背后的概念}
下面是针对您上述请求的更高级的子演示
它的作用是允许我们(以相同的形式 - 如果您需要全局的,我们可以下次讨论,一点也不复杂)调用函数并根据需要输入信息。
Sub Use -> ;一般样本
子用途->具体示例
但此方法最好的部分是您可以根据需要将其更改为任何内容。
假设您有两个按钮*和**两个框,您可以让第一个按钮的button_event 触发上述代码,而第二个按钮触发不同的代码。
示例
关于创建自己的函数和子函数的最佳部分是控制 我不会深入讨论它,因为它会偏离主题,但您可以检查以确保在写入文件之前,文本框首先包含文本。这将保护文件的完整性。
希望这有帮助!
理查德·网站。
The .net framework is a very powerful framework. In the same way (however) it has easy and convenient methods for simple tasks. Most individuals tend to complicate things in order to display knowledge. But less code = less processing = faster and more efficient application (sometimes) so the large above method may not be suitable. Along with that, the above mentioned method would be better off written as a sub or if returning something then a function.
A general Example would be
can be changed to the current text of a field as well.
so a more specific example would be
If you would like to understand the append part of the code
By setting append = true you are allowing your application to write the text at the end of file, leaving the rest of the text already in the file intact.
By setting append = false you will be removing and replacing all the text in the existing file with the new text
If you don't feel like writing that part of the code (though it is small) you could create a sub to handle it, however that method would be slightly different, just for etiquette. functionality would remain similar. (Using StreamWriter)
The Specific Example would be
then under the button_click event call:
You can take this a step further as well. If you would like to create a more advabced Sub to handle any textbox and file.
Lets say you plan on having multiple separate files and multiple fields for each file (though there is a MUCH cleaner more elegant method) you could create a function. {i'll explain the concept behind the function as thoroughly as possible for this example}
below is a more advanced sub demonstration for your above request
What this does is allows us to (on the same form - if you need it global we can discuss that another time, it's not much more complex at all) call the function and input the information as needed.
Sub Use -> General Sample
Sub Use -> Specific Sample
But the best part about this method is you can change that to be anything as you need it.
Let's say you have Two Buttons* and **Two Boxes you can have the button_event for the first button trigger the above code and the second button trigger a different code.
Example
The best part about creating your own functions and subs are Control I won't get into it, because it will be off topic, but you could check to be sure the textbox has text first before writing the file. This will protect the files integrity.
Hope this helps!
Richard Sites.