VB 版 (精华区)
发信人: xiangchu (天歌), 信区: VisualBasic
标 题: Visual Basic编程问答集(二)
发信站: 紫 丁 香 (Tue Aug 31 14:54:23 1999), 转信
(接上期)
Write X, int1,str1
Close X
SaveData=True
End Function
’读取文件操作
Function ReadData(fname$) As Boolean
On Error Resume Next ’设置错误处理
Dim int1 As Integer
Dim str1 As String
Dim X as Integer
X = FreeFile ’取得一个空闲文件句柄
Open fname$ For Input As X ’试图打开该文件
If Err <> 0 Then ’如果打开不成功
ReadData=False
Exit Function
End If
Input X, int1,str1
Close X
ReadData=True
’输入执行结果
Debug.Print int1,str1
End Function
□ 我想在窗体中按下鼠标右键就弹出一个菜单,怎么做?
首先按下Ctrl+E设计一个菜单PopMenu(具体的菜单设计操作参考
帮助文件),把菜单的Visible设置成False。在Form_MouseUp中编写
程序如下:
If Button And 2 ’鼠标右键按下
PopupMenu PopMenu
End If
□ Visual Basic里面的整数和字符串如何互相转换?
VB提供了系统函数Val()和Str(),可以用来实现整数和字符串之间的
转换。例子如下:
Sub Str2Int()
Dim int1 As Integer
Dim str1 As String
int1=100
str1=Str(int1)
Debug.Print str1
str1=“400”
int1=Val(str1)
Debug.Print int1
End Sub
□ 我可以在一个窗体里执行别的窗体的程序代码吗?
是可以的,我们可以用“窗体.代码”的模式去执行别的窗体的代
码。先假设我在窗体frm2里要执行窗体frm1的Form_Click代码,程
序如下:
Private Sub Form_Click()
frm1.Form_Click() ’在frm2里执行frm1的From_Click代码
End Sub
□ 如何在不改变数组内容的情况下重定义数组的范围?
ReDim可以用来重新定义数组的范围,重定义后还要保留数组
的原值,可以用Preserve说明。例如:
Sub ReDim_Data()
Dim tmpdim() as Integer ’定义一个未知范围的数组
ReDim tmpdim(10) ’设定数组的范围0..9
tmpdim(0)=1
tmpdim(1)=2
ReDim Preserve tmpdim(20) ’设定数组的范围0..19,并保留数据
Debug.Print tmpdim(0), tmpdim(1),
End Sub
□ 怎样运行在我的程序里执行别的软件?
系统提供的函数Shell可以解决这个问题。Shell函数的参数为可执
行文件名和运行模式。例如:
Shell“C:\PWIN95\WORDPAD.EXE README.TXT”,1
□ 如何用打印机输出一张图片?
先把图片调入窗体中,然后用PaintPicture方法(Method)把图片打印
出来。
Sub PrintPhoto()
Picture1.Picture = LoadPicture(“C:\ABC\1.BMP”)
Printer.PaintPicture Picture1.Picture, Picture1.Left, Picture1.Top, _
Picture1.Width, Picture1.Height, vbMergeCopy
End Sub
□ 怎样取得一个字符串在另外一个字符串中出现的次数?
Public Function sCount(String1 As String, String2 As String) As Integer
Dim I As Integer, iCount As Integer
I = 1
Do
If (I > Len(String1)) Then Exit Do
I = InStr(I, String1, String2, vbTextCompare)
If I Then
iCount = iCount + 1
I = I + 2
DoEvents
End If
Loop While I
sCount = iCount
End Function
□ 怎样在一个字符串中删除里面的另外一个字符串?
Public Sub sRemove(String1 As String, String2 As String)
Dim I As Integer
I = 1
Do
If (I > Len(String1)) Then Exit Do
I = InStr(I, String1, String2)
If I Then
String1 = Left$(String1, I - 1) + Mid$(String1, I + Len(String2)+1)
I = I + 2
DoEvents
End If
Loop While I
End Sub
□ 怎样在一个字符串中替换里面的另外一个字符串?
Public Sub sReplace(String1 As String, String2 As String, RepString As String)
Dim I As Integer
I = 1
Do
If (I > Len(String1)) Then Exit Do
I = InStr(I, String1, String2)
If I Then
String1 = Left$(String1, I - 1) + RepString + Mid$(String1, I + Len(String2)+1 )
I = I + 2
DoEvents
End If
Loop While I
End Sub
□ 如何计算一个字符串中的行数?
Function CountStringLine(src_string As String) As Integer
On Error Resume Next
Dim string_flag As Integer
Dim line_cnt As Integer
Dim test_string As String
line_cnt = 0 '初始--> 行数为1
string_flag = 1 '标志为1
test_string = src_string
DoEvents
Do
line_cnt = line_cnt + 1
string_flag = InStr(test_string, vbCrLf) ’判断回车换行
test_string = Right(test_string, Len(test_string) - string_flag - 1)
Loop Until string_flag <= 0
CountStringLine = line_cnt
End Function
□ 如何从一个字符串中读取一行字符?
Function ReadStringLine(src_str As String, lineno As Integer) As String
On Error Resume Next
Dim string_flag As Integer
Dim line_cnt As Integer
Dim test_string As String
Dim ret_string As String
line_cnt = 0 '初始--> 行数为1
string_flag = 1 '标志为1
test_string = Right(src_str, 2)
If test_string <> vbCrLf Then
test_string = src_str + vbCrLf
Else
test_string = src_str
End If
DoEvents
Do
line_cnt = line_cnt + 1
string_flag = InStr(test_string, vbCrLf)
ret_string = Left(test_string, string_flag)
test_string = Right(test_string, Len(test_string) - string_flag - 1)
Loop Until lineno <= line_cnt
'If line_cnt = 1 Then
' ReadStringLine = ret_string
'Else
ReadStringLine = Left(ret_string, Len(ret_string) - 1)
'End If
End Function
(待续)
--
※ 来源:.紫 丁 香 bbs.hit.edu.cn.[FROM: 202.118.228.152]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.568毫秒