【问题标题】:Use SUB variable in other sub在其他 sub 中使用 SUB 变量
【发布时间】:2013-11-30 14:12:59
【问题描述】:

我有一个打开 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 之外,因为“打开文件”按钮不再起作用。

谢谢!

【问题讨论】:

  • 这很简单,只需将 OpenFileDialog 组件从工具箱拖放到表单上即可。

标签: vb.net ini


【解决方案1】:

解决方案非常简单。只需在子外部声明OpenFile

Private OpenFile as String

Private Sub OpenINIButton_Click( ...

删除 Dim 声明 Dim OpenFile = 仅替换为 OpenFile =

可选地测试变量是否设置在Button2_Click的开头

If OpenFile is Nothing then Exit Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多