有没有办法从 Load 事件处理程序(通过 inputBox 输入)获取输入以在程序中全局使用?
Is there a way to get the input from a Load event handler (entered via inputBox) to be used Globally in the program?
我正在创建一个表单,它会提示用户在加载文件时输入文件名。我遇到的问题是我用来存储输入的变量在我的另一个程序中无法识别。此处的代码显示了我当前的设置。
Imports System.IO
Public Class frmEmployee
Sub frmEmployee_load(ByVal sender As Object, e As System.EventArgs)
Dim strFileName = InputBox("Please name the file you would like to save the data to: ")
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtEmail.Clear()
txtExtension.Clear()
txtFirst.Clear()
txtLast.Clear()
txtMiddle.Clear()
txtNumber.Clear()
txtPhone.Clear()
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim inputFile As New StreamWriter(strFileName)
If File.Exists(strFileName) = True Then
inputFile = File.CreateText(strFileName)
inputFile.Write(txtEmail.Text)
inputFile.Write(txtExtension.Text)
inputFile.Write(txtFirst.Text)
inputFile.Write(txtLast.Text)
inputFile.Write(txtMiddle.Text)
inputFile.Write(txtNumber.Text)
inputFile.Write(txtPhone.Text)
inputFile.Write(cmbDepart.Text)
Else
MessageBox.Show("" & strFileName & "Cannot be created or found.")
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
结束Class
加载事件处理程序是我希望用户输入文件名的地方。
将变量的范围更改为加载方法之外...
Public Class frmEmployee
Private strFileName As String
Sub frmEmployee_load(ByVal sender As Object, e As System.EventArgs)
strFileName = InputBox("Please name the file you would like to save the data to: ")
End Sub
你的方法的问题是你的变量 strFileName 的范围是 class frmEmployee。
您需要设置一个真正的全局变量(在应用程序启动时)然后使用它。因此,您将需要一个 Sub Main 作为应用程序的入口点,请参见 here,并在此之前创建一个 public 变量来插入文件名。
所以您的应用程序启动时可能有这样的东西:
Public fileNametoUse as string
Public Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New Form1)
End Sub
然后在为 frmEmployee 加载子程序时,您将拥有:
fileNametoUse = InputBox("Please name the file you would like to save the data to: ")
您可以使用 My.Settings
并在那里加载文件字符串并保存。所有表格现在都可以访问它。当您关闭应用程序时,如果您愿意,请将其设置为 String.Empty
。
您还可以利用 class 对象作为您正在捕获的字段,然后使用 BinaryFormatter
或 XmlSerializer
来存储数据。使用对象更好地进行数据绑定、创建和重建。
My.Settings.Filename = Inputbox("Filename?")
My.Settings.Save()
我正在创建一个表单,它会提示用户在加载文件时输入文件名。我遇到的问题是我用来存储输入的变量在我的另一个程序中无法识别。此处的代码显示了我当前的设置。
Imports System.IO
Public Class frmEmployee
Sub frmEmployee_load(ByVal sender As Object, e As System.EventArgs)
Dim strFileName = InputBox("Please name the file you would like to save the data to: ")
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtEmail.Clear()
txtExtension.Clear()
txtFirst.Clear()
txtLast.Clear()
txtMiddle.Clear()
txtNumber.Clear()
txtPhone.Clear()
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim inputFile As New StreamWriter(strFileName)
If File.Exists(strFileName) = True Then
inputFile = File.CreateText(strFileName)
inputFile.Write(txtEmail.Text)
inputFile.Write(txtExtension.Text)
inputFile.Write(txtFirst.Text)
inputFile.Write(txtLast.Text)
inputFile.Write(txtMiddle.Text)
inputFile.Write(txtNumber.Text)
inputFile.Write(txtPhone.Text)
inputFile.Write(cmbDepart.Text)
Else
MessageBox.Show("" & strFileName & "Cannot be created or found.")
End If
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
结束Class
加载事件处理程序是我希望用户输入文件名的地方。
将变量的范围更改为加载方法之外...
Public Class frmEmployee
Private strFileName As String
Sub frmEmployee_load(ByVal sender As Object, e As System.EventArgs)
strFileName = InputBox("Please name the file you would like to save the data to: ")
End Sub
你的方法的问题是你的变量 strFileName 的范围是 class frmEmployee。
您需要设置一个真正的全局变量(在应用程序启动时)然后使用它。因此,您将需要一个 Sub Main 作为应用程序的入口点,请参见 here,并在此之前创建一个 public 变量来插入文件名。
所以您的应用程序启动时可能有这样的东西:
Public fileNametoUse as string
Public Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New Form1)
End Sub
然后在为 frmEmployee 加载子程序时,您将拥有:
fileNametoUse = InputBox("Please name the file you would like to save the data to: ")
您可以使用 My.Settings
并在那里加载文件字符串并保存。所有表格现在都可以访问它。当您关闭应用程序时,如果您愿意,请将其设置为 String.Empty
。
您还可以利用 class 对象作为您正在捕获的字段,然后使用 BinaryFormatter
或 XmlSerializer
来存储数据。使用对象更好地进行数据绑定、创建和重建。
My.Settings.Filename = Inputbox("Filename?")
My.Settings.Save()