VB.NET 如何将文本框读入字符串并将该字符串用于其他用途?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8627186/
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
VB.NET How does one read a textbox into a string and use the string for something else?
提问by Chris Wheelous
I have two text boxes on my form. Textbox1 and Textbox2. Textbox1 will contain information. I want to code my button to read textbox 1 and put it into a string or some way for the app to read the textbox and store the information into it's memory.
我的表单上有两个文本框。文本框 1 和文本框 2。Textbox1 将包含信息。我想对我的按钮进行编码以读取文本框 1 并将其放入一个字符串或某种方式,以便应用程序读取文本框并将信息存储到它的内存中。
Then I want to be able to read that string into the second part of my application.
然后我希望能够将该字符串读入我的应用程序的第二部分。
Not sure where to go with this. Should I:
不知道该去哪里。我是不是该:
read the textbox into a string using a streamreader and then use a streamwriter to write the data into a string?
save the textbox to a .txt file then use the openfiledialog to read the text file into a string to use?
使用流读取器将文本框读入字符串,然后使用流写入器将数据写入字符串?
将文本框保存到 .txt 文件,然后使用 openfiledialog 将文本文件读入字符串以使用?
Any help guys would be much appreciated.
任何帮助家伙将不胜感激。
回答by Olivier Jacot-Descombes
In older VB dialects like Access VBA there was the concept of default properties that were automatically accessed when you wrote something like this:
在像 Access VBA 这样的旧 VB 方言中,有一个默认属性的概念,当您编写如下内容时会自动访问这些属性:
s = Me!txtMyTextBox
Behind the scenes VBA did automatically something like this:
在幕后,VBA 会自动执行以下操作:
s = Me!txtMyTextBox.Value
In VB.NET if you write Dim t = Textbox1
then t
will by typed as TextBox
and contain a reference to the textbox. To retrieve the text from the textbox, access its Text
property explicitly:
在VB.NET如果你写的Dim t = Textbox1
,然后t
会被书写在TextBox
和包含的文本框的参考。要从文本框中检索文本,请Text
显式访问其属性:
Dim s as String
s = Textbox1.Text;
It depends where you want to use the string later. If you want to use it outside of the form you have two possibilities: Either let the form export the string or get the string from your form from outside.
这取决于您以后要在何处使用该字符串。如果您想在表单之外使用它,您有两种可能性:让表单导出字符串或从表单外部获取字符串。
1 Export the text
1 导出文本
In a Module (let us call it MyModule) define a public string:
在模块中(我们称之为 MyModule)定义一个公共字符串:
Public TheText as String
In the form:
在形式:
MyModule.TheText = Textbox1.Text;
The text is now available globally in your whole application.
该文本现在在您的整个应用程序中全局可用。
2 Get it from outside
2 从外面获取
In the form, wrap the Textbox text in a property:
在表单中,将 Textbox 文本包裹在一个属性中:
Public ReadOnly Property TheText() As String
Get
Return Textbox1.Text
End Get
End Property
Somewhere else, assuming that the form is your main form and is called frmMain
:
在其他地方,假设表单是您的主要表单并被称为frmMain
:
Console.WriteLine(DirectCast(Application.OpenForms(0), frmMain).TheText)
Or if you have a form variable strongly typed as your specific form, e.g. Dim frm As frmMain
(and not just as Form
):
或者,如果您有一个强类型的表单变量作为您的特定表单,例如Dim frm As frmMain
(而不仅仅是作为Form
):
Console.WriteLine(frm.TheText)
回答by Cameron S
To retrieve text from a textbox, you can use the TextBox.Text
property and store that value into a String. To save that String to a file, you can use System.IO.File.WriteAllText(string filename, string content)
.
要从文本框中检索文本,您可以使用该TextBox.Text
属性并将该值存储到一个字符串中。将该字符串保存到文件中,您可以使用System.IO.File.WriteAllText(string filename, string content)
.
There are many ways to store a String for use. This is simply a simple (not always best) way of storing a string to a file.
有很多方法可以存储 String 以供使用。这只是一种将字符串存储到文件的简单(并不总是最好的)方法。