想用就用,VB基础代码[1]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 csdntoll 的 blog

作者:cooly
出处:http://search.csdn.net/expert/topic/51/5101/2003/3/20/1555609.htm

'=======================================================
'一、如何使用adodc控件绑定数据到datagrid和datalist
'=======================================================

public isdb as boolean

private sub form_load()
dim connstr, accesslocation as string
accesslocation = "c:\db1.mdb"
connstr = "provider=microsoft.jet.oledb.4.0;data source=" & accesslocation & ";persist security info=false"
adodc1.connectionstring = connstr
adodc1.commandtype = adcmdtext
adodc1.recordsource = "select * from tableabc"
adodc1.refresh
for i = 0 to adodc1.recordset.fields.count - 1
    list1.additem adodc1.recordset.fields(i).name
next
set datalist1.datasource = adodc1
datalist1.datafield = "col1"
datalist1.boundcolumn = "col1"
set datalist1.rowsource = adodc1
datalist1.listfield = "col1"

adodc1.recordset.movefirst
end sub

private sub list1_click() '选择datagrid中显示的字段
dim sql, sql1 as string

sql = "select "
for i = 0 to list1.listcount - 1
 if list1.selected(i) then
    if trim(sql1) = "" then
       sql1 = list1.list(i)
    else
       sql1 = sql1 & ", " & list1.list(i)
    end if
 end if
next

if trim(sql1) = "" then
   sql1 = "*"
end if

sql = sql & sql1 & " from tableabc"

adodc1.recordsource = sql
adodc1.refresh
set datagrid1.datasource = adodc1
end sub

 

'========================================================
'二、如何对文件进行二进制读写
'========================================================
dim getvalue() as byte

private sub command1_click()
open "c:\1.cmd" for binary access write as #2
     put #2, , getvalue()
close #2

end sub

private sub form_load()

open "c:\command.com" for binary access read as #1
      redim getvalue(filelen("c:\command.com"))
      get #1, , getvalue
close #1
end sub

'========================================================
'三、字符串处理算法(1)
' 求出已知字符串中出现频率最高的字串内容及出现次数
'========================================================
private sub command1_click()
dim a, b as string
dim i as long
dim c, t as long

c = 0
a = "abcdefcdedgcdeethcdenbicde"
for i = 1 to len(a)
    t = 0
    b = a
    if i = len(a) - 2 then exit for
    do until instr(b, mid(a, i, 3)) = 0
       b = right(b, len(b) - instr(b, mid(a, i, 3)))
       t = t + 1
    loop
    if t > c then
       c = t
    end if
next
msgbox c
end sub

'========================================================
'四、drivelistbox,dirlistbox,filelistbox三个控件的使用
'========================================================

private sub dir1_change()
file1.path = dir1.path
end sub

private sub drive1_change()
dir1.path = drive1.drive
end sub

private sub file1_click()
text1.text = file1.path & "\" & file1.filename
end sub

'========================================================
'五、如何对目录进行操作 (使用fso)
'========================================================

private sub command1_click()
dim fso as object
dim sourcepath, targetpath as string
sourcepath = text1.text
targetpath = text2.text
set fso = createobject("scripting.filesystemobject")
if fso.folderexists(targetpath) then
   fso.copyfolder sourcepath & "*.*", targetpath
   fso.copyfile sourcepath & "*.*", targetpath
else
   fso.createfolder (targetpath)
   fso.copyfolder sourcepath & "*.*", targetpath
   fso.copyfile sourcepath & "*.*", targetpath
end if
set fso = nothing
msgbox "复制完成"
end sub

private sub command2_click()
dim fso as object
dim targetpath as string
targetpath = "d:\test"
set fso = createobject("scripting.filesystemobject")
fso.deletefolder targetpath, true
set fso = nothing
msgbox "删除成功"
end sub

'========================================================
'六、如何取出datagrid控件选定行的内容
'========================================================

private sub datagrid1_mousedown(button as integer, shift as integer, x as single, y as single)
datagrid1.row = datagrid1.rowcontaining(y)

本文关键:VB 代码
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top