有没有办法从Load事件处理程序(通过inputBox输入)获取输入以在程序中全局使用?

时间:2015-06-14 15:07:23

标签: vb.net events handler

我正在创建一个表单,提示用户在加载文件时输入文件名。我遇到的问题是我用来存储输入的变量在我的另一个程序中无法识别。这里的代码显示了我当前的设置。

interpolated.position

结束班

加载事件处理程序是我希望用户输入文件名的地方。

3 个答案:

答案 0 :(得分:1)

将变量的范围更改为加载方法之外...

 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     

答案 1 :(得分:0)

您可以使用My.Settings并在其中加载文件字符串并保存。所有表单现在都可以访问它。当您关闭应用时,如果您愿意,请将其设置为String.Empty

您还可以利用您正在捕获的字段的类对象,然后使用BinaryFormatterXmlSerializer来存储数据。使用对象更好地进行数据绑定,创建和重建。

My.Settings.Filename = Inputbox("Filename?")
My.Settings.Save()

答案 2 :(得分:-1)

你所采用的方法的问题是你的变量strFileName的范围是frmEmployee类。

您需要设置一个真正的全局变量(在应用程序启动时)然后使用它。因此,您需要Sub Main作为应用程序的入口点。请参阅here,然后在此之前创建一个公共变量来填充文件名。

所以你的应用程序启动可能会有这样的东西:

Public fileNametoUse as string

Public Sub Main()

    Application.EnableVisualStyles()
    Application.SetCompatibleTextRenderingDefault(False)
    Application.Run(New Form1)

End Sub

然后在你为frmEmployee加载sub时你会得到:

fileNametoUse =  InputBox("Please name the file you would like to save the data to: ")