[NET.VB]小問題集錦(供初學者參考)
2024-07-21 02:20:57
供稿:網友
聲明:
1、所有代碼不注明出處的皆為本人所寫
2、所有代碼均在本機調試通過
3、本貼適用于初學者(本人也是)
4、歡迎各位仁兄斧正,提供想法或代碼
5、本人力求每日補充內容
6、非本人同意,請勿轉載本人所寫的代碼
7、各位說要不要上面第6條?
8、哈,聲明是這樣寫的嗎,一點都不嚴肅,也不規范
水如煙 2004.7.19
[控件類]
treeview
1、給節點加個tooltip(2004.7.19)
private tmptreenode as treenode
private mtreeviewtooltip as new tooltip
private sub treeview1_mousemove(byval sender as object, byval e as system.windows.forms.mouseeventargs) handles treeview1.mousemove
dim mnode as treenode
mnode = me.treeview1.getnodeat(e.x, e.y)
'當前沒節點
if mnode is nothing then
mtreeviewtooltip.settooltip(me.treeview1, nothing)
exit sub
end if
'超過當前node的邊界
if mnode.bounds.right < e.x orelse mnode.bounds.left > e.x then
mtreeviewtooltip.settooltip(me.treeview1, nothing)
exit sub
end if
'同一個node
if mnode is tmptreenode then
exit sub
end if
mtreeviewtooltip.automaticdelay = 500
mtreeviewtooltip.settooltip(me.treeview1, mnode.text)
tmptreenode = mnode
end sub
datagrid
1、自適應各列長度(2004.7.19)
'定義一個字段信息類
private class columninfo
public [name] as string
public [datatype] as string
public maxwidth as integer = 0
public sub comparestringlength(byval mstring as string)
dim mlength as integer
mlength = system.text.encoding.default.getbytes(mstring).length()
if maxwidth < mlength then maxwidth = mlength
end sub
public function columnwidth(byval mdatagrid as datagrid) as integer
dim mgraphics as graphics = mdatagrid.creategraphics
dim mcolwidth as single
mcolwidth = mgraphics.measurestring(new string(ctype("a", char), maxwidth), mdatagrid.font).width + 2
return ctype(mcolwidth, integer)
end function
end class
private sub makedatagridautoextend(byval mdatagrid as datagrid)
'判斷mdatagrid數據源類型
'如果綁定的是dataset或dataviewmanager或沒有綁定任何數據源,則退出,
if typeof mdatagrid.datasource is system.data.dataset orelse _
typeof mdatagrid.datasource is system.data.dataviewmanager orelse _
mdatagrid.datasource is nothing then exit sub
'以下分別考慮兩種數據源,一是dataview,一是datatable
dim dt as datatable
if typeof mdatagrid.datasource is system.data.dataview then
dt = ctype(mdatagrid.datasource, dataview).table
else
dt = ctype(mdatagrid.datasource, datatable)
end if
'取各字段信息
dim mcolumninfo(dt.columns.count - 1) as columninfo
dim i as integer = 0
dim mcolumn as datacolumn
for each mcolumn in dt.columns
dim minfo as new columninfo
with minfo
.name = mcolumn.columnname
.datatype = mcolumn.datatype.tostring
.comparestringlength( mcolumn.columnname)
end with
mcolumninfo(i) = minfo
i += 1
next
'取各字段的最大長度
dim mrow as datarow
for each mrow in dt.rows
for i = 0 to dt.columns.count - 1
if not isdbnull(mrow(i)) then
mcolumninfo(i).comparestringlength(ctype(mrow(i), string))
end if
next
next
'建datagridtablestyle
dim ts as new datagridtablestyle
ts.mappingname = dt.tablename
for i = 0 to dt.columns.count - 1
if mcolumninfo(i).datatype.equals("system.boolean") then
'這是boolean字段
dim blncol as new datagridboolcolumn
with blncol
.mappingname = mcolumninfo(i).name
.headertext = mcolumninfo(i).name
.width = mcolumninfo(i).columnwidth(mdatagrid)
.nulltext = ""
end with
ts.gridcolumnstyles.add(blncol)
else
'非boolean字段
dim txtcol as new datagridtextboxcolumn
with txtcol
.mappingname = mcolumninfo(i).name
.headertext = mcolumninfo(i).name
.width = mcolumninfo(i).columnwidth(mdatagrid)
.nulltext = ""
.readonly = false '這里可以設置為只讀
.format = "" '這里可以設置顯示格式,要顯示日時分秒的就在這設
end with
ts.gridcolumnstyles.add(txtcol)
end if
next
mdatagrid.tablestyles.clear()
mdatagrid.tablestyles.add(ts)
'至于其它的功能,比如綁定事件之類的,后面再補充
end sub
'調用
private sub button2_click(byval sender as system.object, byval e as system.eventargs) handles button2.click
makedatagridautoextend(me.datagrid1)
end sub