OnLoad Sub 的问题 (Visual Basic)
Trouble with OnLoad Sub (Visual Basic)
我在启动时声明默认路径文件时遇到了一些问题。
每次我 运行 这个程序,它都说 pathFile 是空的。
有人知道我的代码需要更改什么吗?
Imports System
Imports System.IO
Imports System.Text
Public Class GlobalVariables
Public Shared pathFile As String
End Class
Public Class Form1
Protected Overridable Sub OnLoad(e As EventArgs)
GlobalVariables.pathFile = My.Computer.FileSystem.SpecialDirectories.Desktop
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
' create or overwrite the file
Dim fs As FileStream = File.Create(GlobalVariables.pathFile)
' add text to file
Dim info As Byte() = New UTF8Encoding(True).GetBytes(rtbText.Text)
fs.Write(info, 0, info.Length)
fs.Close()
End Sub
End Class
提前致谢!
- Xaaf 代码
而不是尝试覆盖 OnLoad
(这将是 Overrides
而不是 Overridable
),我会处理加载事件:
Private Sub Form_Load(sender As Object, e As System.EventArgs) Handles Me.Load
GlobalVariables.pathFile = My.Computer.FileSystem.SpecialDirectories.Desktop
End Sub
您可能只设置声明 pathFile
的值:
Public Class GlobalVariables
Public Shared pathFile As String = My.Computer.FileSystem.SpecialDirectories.Desktop
End Class
我在启动时声明默认路径文件时遇到了一些问题。
每次我 运行 这个程序,它都说 pathFile 是空的。
有人知道我的代码需要更改什么吗?
Imports System
Imports System.IO
Imports System.Text
Public Class GlobalVariables
Public Shared pathFile As String
End Class
Public Class Form1
Protected Overridable Sub OnLoad(e As EventArgs)
GlobalVariables.pathFile = My.Computer.FileSystem.SpecialDirectories.Desktop
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
' create or overwrite the file
Dim fs As FileStream = File.Create(GlobalVariables.pathFile)
' add text to file
Dim info As Byte() = New UTF8Encoding(True).GetBytes(rtbText.Text)
fs.Write(info, 0, info.Length)
fs.Close()
End Sub
End Class
提前致谢!
- Xaaf 代码
而不是尝试覆盖 OnLoad
(这将是 Overrides
而不是 Overridable
),我会处理加载事件:
Private Sub Form_Load(sender As Object, e As System.EventArgs) Handles Me.Load
GlobalVariables.pathFile = My.Computer.FileSystem.SpecialDirectories.Desktop
End Sub
您可能只设置声明 pathFile
的值:
Public Class GlobalVariables
Public Shared pathFile As String = My.Computer.FileSystem.SpecialDirectories.Desktop
End Class