网站导航:首页 -> 计算机等级考试 -> 计算机等级考试动态 -> 计算机等级考试VB编程常见问题和技巧解答

计算机等级考试VB编程常见问题和技巧解答

 下面有几个较为典型的vb问题的解答,希望能对广大vb爱好者有所帮助。
问题:如何编程使文本框中文本的某一特定字符或字符串同时高亮显示?
答:由于普通textbox控件不支持不连续字符串的同时高亮显示,所以我们选择richtextbox控件。单击工程(project)选单项,在弹出的下拉选单中单击组件(components)选单项,从弹出的对话框中选择microsoft rich textbox control 5.0复选框,确定加载richtextbox控件。
新建(new)一个工程,在窗体(form)上添加一个richtextbox控件和两个command(按钮)控件,都采用系统默认的name属性值;设置richtextbox的text属性值为空,command1和command2的caption属性值分别设为“输入文本”和“选择字符串”。最后,添加如下vb代码:
private sub command1—click()
dim str as string
dim text as string
str=″输入文本″
text=inputbox(str)
richtextbox1.text=text
end sub
private sub command2—click()
dim str as string
dim text as string
dim position as integer
dim lenth as integer
str=″输入要高亮显示的字符串″
text=inputbox(str)
if text 〈〉 ″″ then
position=instr(richtextbox1.text, text)-1
lenth=len(text)
richtextbox1.selstart=position
richtextbox1.sellength=lenth
richtextbox1.selcolor=rgb(255,0,0)
do while instr(position+lenth+1, richtextbox1.text, text) 〈〉 0
position=instr(position+lenth+1, richtextbox1.text, text)-1
richtextbox1.selstart=position
richtextbox1.sellength=lenth
richtextbox1.selcolor=rgb(255,0,0)
loop
end if
end sub
按f5执行程序,单击“输入文本”按钮,在弹出的对话框中输入一些文本,确定后,刚刚输入的文本将显示在richtextbox中;再单击“选择字符串”按钮,在弹出的对话框中输入你希望高亮显示的字符串,确定后,richtextbox中相应的字符串将以红色高亮显示。
问题:如何利用vb编写程序实现windows 95操作系统的热启动?
答:要利用程序实现系统的重新启动,可以在你的程序中调用api函数来实现。建一个子函数:(以vb为例)
declare function systemparametersinfo lib ″user32″ alias —
″systemparametersinfo″ (byval uaction as long, byval uparam as long,
byval lpvparam as any, byval fuwinini as long) as long
sub disablectrlaltdelete(bdisabled as boolean)
dim x as long
x=systemparametersinfo(97, bdisabled, cstr(1), 0)
end sub
call disablectrlaltdelete(true)  ′禁止热启
call disablectrlaltdelete(false) ′允许热启
问题:如何把数据文件输出到text控件中?如果数据量比较大,窗体满屏也不够大,怎么解决?
答:有一个比较简单的方法,就是把数据放到一个文本框(text)里,并在其中加上水平和垂直滚动条。具体实现步骤为:先在窗体(form)里加入一个文本框,采用默认名text1;然后,设置文本框text1的属性:text属性设置为空,multiline属性设置为true,scrollbars属性设置为3-both;接着添加如下vb代码:
private sub form—load()
dim handle as integer
dim filename as string
on error goto errexit
begin:
′输入要显示的数据文件的名称
filename=inputbox$(″input filename″,
″open file″)
on error goto fileerr
handle=freefile
open filename for input as #handle
′把数据文件中的数据输出到文本框中
text1.text=input$(lof(handle), handle)
close #handle
exit sub
fileerr:
dim errnum as integer
if err.number=53 then
errnum=msgbox(″file not exist″, vbokcancel, ″error information″)
if errnum=1 then
goto begin