[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