在其他子中使用SUB变量

时间:2013-11-17 12:29:33

标签: vb.net ini

我有一个打开文件的打开文件按钮。

现在我想使用文件的路径,这样我就可以单击一个保存按钮并保存文件。

这是打开按钮的代码:

    Private Sub OpenINIButton_Click(sender As Object, e As EventArgs) Handles OpenINIButton.Click
    Dim OpenDLG As New OpenFileDialog
    OpenDLG.Filter = "Configuration File (*.ini)|*.ini"
    OpenDLG.Title = "Open INI File"
    OpenDLG.InitialDirectory = "C:\"
    OpenDLG.RestoreDirectory = True

    DialogResult = OpenDLG.ShowDialog

    If DialogResult = Windows.Forms.DialogResult.OK Then
        Dim OpenFile = OpenDLG.FileName.ToString()

        wValue.Text = ReadIni(OpenFile, Isolation, Value, "")

    ElseIf DialogResult = Windows.Forms.DialogResult.Cancel Then

    End If

End Sub

我想要保存按钮中的OpenFile变量,我想要使用的代码是:

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles saveINI.Click

    System.IO.File.WriteAllText(OpenFile, "")
    writeIni(OpenFile, BuildOptions, Isolation, w.Value.Text)
End Sub

但是OpenFile变量不可用。

是否有可能设置OpenFile变量Global? 我无法将其移动到SUB之外,因为“打开文件”按钮不再起作用。

谢谢!

1 个答案:

答案 0 :(得分:0)

解决方案非常简单。只需在子

之外声明OpenFile即可
Private OpenFile as String

Private Sub OpenINIButton_Click( ...

删除Dim语句Dim OpenFile =替换为OpenFile =

可选择测试变量是否设置在Button2_Click

的开头
If OpenFile is Nothing then Exit Sub