分类: 电脑/网络 程序设计 其他编程语言
成都创新互联公司专注于玄武企业网站建设,响应式网站,商城网站制作。玄武网站建设公司,为玄武等地区提供建站服务。全流程按需定制制作,专业设计,全程项目跟踪,成都创新互联公司专业和态度为您提供的服务
问题描述:
我用的是Timer计时器:
如何将程序控制住,让它第一次发现没保存后,只击活一次提示保存,然后使用修改后的保存路径:(代码如下)
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'如何将程序控制住,让它第一次发现没保存后,只击活一次提示保存,然后使用修改后的保存路径
If SaveFileDialog1.FileName = "" Then
If SaveFileDialog1.ShowDialog Then
rtbox.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.PlainText)
End If
Else
'如果已经选择了要保存的文件名,则保存文本到文件中
rtbox.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.PlainText)
End If
End Sub
————————————————————
此代码执行后变成死循环。
解析:
经过我的潜心修炼问题终于是解决了.请看代码
Imports System.IO
Private strFileName As String = "myRTFdoc.txt"
Private flgFirst As Boolean = True
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
'如何将程序控制住,让它第一次发现没保存后,只击活一次提示保存,然后使用修改后的保存路径
Call zc()
End Sub
Private Sub zc()
'Timer2.Stop()
With SaveFileDialog1
.DefaultExt = "txt"
.FileName = strFileName
.Filter = "Text files(*.txt)|*.txt|All files(*.*)|*.*"
.FilterIndex = 1
.InitialDirectory = "c:\"
.OverwritePrompt = True
.Title = "Save Reminding"
End With
'Timer2.Enabled = False
If flgFirst = True Then
If SaveFileDialog1.ShowDialog = DialogResult.OK Then
strFileName = SaveFileDialog1.FileName
Dim objWriter As StreamWriter = New StreamWriter(strFileName, False)
objWriter.Write(rtbox.Text)
objWriter.Close()
objWriter = Nothing
End If
flgFirst = False
'Timer2.Enabled = True
' Timer2.Start()
Else
'flg= second
Dim objWriter As StreamWriter = New StreamWriter(strFileName, False)
objWriter.Write(rtbox.Text)
objWriter.Close()
objWriter = Nothing
End If
'Timer2.Enabled = True
'Timer2.Start()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer2.Enabled = True
' zc()
End Sub
知道问题在哪里嘛?我调试了确实是"死循环"、其实不是真正的死循环、是time tick事件你设定的时间太短了.估计只设置了1-5s左右.那么程序运行还需要时间.所以他就不停地调用timetick时间.你的savedialog对话框根本来不急弹出来.
所以建议你把timer的interval的值设置高点最好12-15秒 触发一次.
当然我想还有其他的方法、还没有彻底取研究下.
比如stop 什么 、那还需要时间.
至少目前我的方法是可以了.
我设置的12s P4. 3.0 1G 内存 跑下我的程序如果 F10(F8)慢点的话就来不及了.
另为把你的程序小改了下、应该可以满足你的要求了.
VB6.0
写入:
Open "D:\123.txt" For Output As #1 '打开XXX路径的XXX文件(双引号里表示文件位置和文件名)
Print #1, Text1.Text '写入Text1的Text内容
Close #1 '关闭
读取:
Open "D:\123.txt" For Input As #1 '打开打开XXX路径的XXX文件(双引号里表示文件位置和文件名)
Do While Not EOF(1)
Line Input #1, s
Text1.Text = s
Loop 'Do...Loop表示循环读取文件的内容,并让Text1.Text=内容
Close #1 '关闭
VB点虐 中读写文件主要使用System.IO命名空间。
① 使用 File.ReadAllText 读取
Dim s As String = System.IO.File.ReadAllText("C:\a.txt")
② 使用 StreamReader 读取,注意编码格式和写入的编码保持一致。
Dim sr As StreamReader = New StreamReader("C:\a.txt", System.Text.Encoding.UTF8)
Dim s As String = sr.ReadToEnd()
sr.Close()
#Region "二进制文件的存储函数" Public Function BinaryToFile(ByRef TableRowColItem As Object, ByVal FileName As String) As Boolean Dim data As Byte() = TableRowColItem
Dim myfilestream As New System.IO.FileStream(FileName, IO.FileMode.Create)
myfilestream.Write(data, 0, data.Length)
myfilestream.Close()
Return True
End Function Public Function BinaryToImage(ByRef TableRowColItem As Object, ByRef image As Image) As Boolean Dim data As Byte() = TableRowColItem Dim imgStream As New System.IO.MemoryStream '(data)
imgStream.Write(data, 0, data.Length)
image = System.Drawing.Image.FromStream(imgStream)
imgStream.Close()
imgStream.Dispose() Return True
End Function
Public Function BinaryFromFile(ByVal FileName As String, ByRef TableRowColItem As Object) As Boolean Using myfilestream As New FileStream(FileName, FileMode.Open, FileAccess.Read)
Dim data() As Byte
ReDim data(myfilestream.Length - 1)
myfilestream.Read(data, 0, myfilestream.Length)
myfilestream.Close()
TableRowColItem = data End Using
Return True
End Function Public Function BinaryFromImage(ByRef Image As Image, ByRef TableRowColItem As Object) As Boolean
Dim imgStream As New MemoryStream
Dim b As New Bitmap(Image) b.Save(imgStream, System.Drawing.Imaging.ImageFormat.Jpeg) Dim data As Byte() = imgStream.GetBuffer
TableRowColItem = data
data = TableRowColItem
imgStream.Close()
imgStream.Dispose() Return True
End Function Public Function BinaryFromImage(ByVal Image As Image, ByRef TableRowColItem As Object, ByVal imgFormat As System.Drawing.Imaging.ImageFormat) As Boolean
Dim imgStream As New MemoryStream
'Image.Save(imgStream, imgFormat)
'Dim b As New Bitmap(Image)
'b.Save("c:\a.bmp", imgFormat)
'b.Save("c:\a.bmp", imgFormat) Dim data As Byte() = imgStream.GetBuffer TableRowColItem = data
data = TableRowColItem
imgStream.Close()
Return True
End Function
#End Region
分类: 电脑/网络 程序设计 其他编程语言
问题描述:
一个 vb 程序中,想在程序关闭的时候保存一些变量,在下次打开的时候可以继续使用,请问应该怎么做?vb 中是否有现成的和函数可用?
解析:
有,使用app.config或自定义xml文件或读写注册表或用数据库,看MSDN