重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
'数据库连接函数,其中mysql(数据集连接语句),sqlip(SQL服务器IP),rsmod(数据库打开模式)为动态取值
宁晋网站建设公司创新互联,宁晋网站设计制作,有大型网站制作公司丰富经验。已为宁晋超过千家提供企业网站建设服务。企业网站搭建\成都外贸网站建设要多少钱,请找那个售后服务好的宁晋做网站的公司定做!
Public Function myRs(ByVal mySQL As String, ByVal sqlIP As String, ByVal rsMod As Integer) As ADODB.Recordset
Dim myConn As New ADODB.Connection
If myConn.State = 1 Then
myConn.Close()
End If
If rs.State = 1 Then
rs.Close()
End If
myConn.ConnectionString = "Provider = Sqloledb;SERVER=" sqlIP ";uid=sa;pwd=;database=school"
myConn.Open()
rs.Open(mySQL, myConn, 1, rsMod)
myRs = rs
End Function
'显示列表内容函数,该函数能显示两个子项
Public Function addLst() As Boolean
lstUser.Clear()
mySql = "select * from sqlusers"
rs = myRs(mySql, sqlIP, 1)
lstUser.View = View.Details
Dim i As Integer
If rs.Fields.Count 0 Then
'添加列表框列标题项目
For i = 1 To rs.Fields.Count
lstUser.Columns.Add(rs.Fields.Item(i - 1).Name)
Next
'添加列表框数据内容
If rs.RecordCount 0 Then
rs.MoveFirst()
For i = 1 To rs.RecordCount
lstUser.Items.Add(rs("用户名").Value)
lstUser.Items.Item(i - 1).SubItems.Add(rs("密码").Value)
lstUser.Items.Item(i - 1).SubItems.Add(rs("用户类型").Value)
rs.MoveNext()
Next
End If
End Function
以上是我刚刚写完的,调试通过,希望对你有用.
Public Class Form1
Private Imgfile As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListBox1.DrawMode = DrawMode.OwnerDrawVariable
Imgfile = "X:\Users\......\Pictures\ssm.png"‘显示为listbox的每一行文字前面的图标文件,换一张你自己的图片。
End Sub
Private Sub ListBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ListBox1.DrawItem
e.DrawBackground()
Dim rect As Rectangle = New Rectangle(2, e.Bounds.Y + 2, e.Bounds.Height, e.Bounds.Height - 4)
e.Graphics.DrawImage(New Bitmap(Imgfile), rect)
e.Graphics.DrawString(sender.Items(e.Index), sender.Font, New SolidBrush(sender.ForeColor), _
New RectangleF(e.Bounds.X + rect.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
e.DrawFocusRectangle()
End Sub
End Class
预先准备三个图标文件,用于树型控件中显示磁盘符号和文件夹的图像之用。
1、窗体上添加控件如下:
组合框控件 ComboBox1,树型控件 TreeView1,列表框控件 ListBox1,图像列表控件 ImageList1。
选中TreeView1,设置其ImageList属性为ImageList1。
2、设置属性
选中图像列表控件 ImageList1,在属性窗口里,选中属性Images,单击三个小点按钮,出现图像集合编辑器窗口,单击[添加按钮],一一把准备好的图标文件进行添加,注意先后次序,如果不符合要求可以通过上下移动按钮重新改变次序。完成后单击[确定]。
运行图如下:
完整代码如下:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'添加系统所有磁盘目录符号
For Each MyDrive As String In Environment.GetLogicalDrives()
ComboBox1.Items.Add(MyDrive)
Next
'显示第一个磁盘符号
ComboBox1.Text = ComboBox1.Items(0)
End Sub
'递归过程添加目录树
Public Sub AddDirectory(ByVal strFatherPath As String, ByVal strPath As String, ByVal nodeFather As TreeNode)
Dim i As Integer
Dim Mynode As New TreeNode
'先添加本目录
Mynode.Text = Strings.Replace(strPath, strFatherPath "\", "", , 1)
'为节点指定未被选中时显示的图标
Mynode.ImageIndex = 1
'为节点指定被选中时显示的图标
Mynode.SelectedImageIndex = 2
nodeFather.Nodes.Add(Mynode)
Application.DoEvents()
Try
Dim str() As String = Directory.GetDirectories(strPath)
'递归遍历该目录的子文件夹
For i = 0 To str.GetUpperBound(0)
AddDirectory(strPath, str(i), Mynode)
Next
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
Mynode = Nothing
End Sub
'根据给出的盘符添加目录树
Private Sub AddRootDirectory(ByVal DiscSymbol As String)
Dim Nynode As New TreeNode
'先把磁盘盘符添加到树中
TreeView1.Nodes.Clear()
Nynode.ImageIndex = 0
Nynode.Text = DiscSymbol
Nynode.SelectedImageIndex = -1
TreeView1.Nodes.Add(Nynode)
Dim i As Integer
'获取磁盘根目录下的文件夹
Dim str() As String = Directory.GetDirectories(DiscSymbol "\")
For i = 0 To str.GetUpperBound(0)
'调用递归过程遍历该文件夹里的所有子文件夹,并添加到树型控件
AddDirectory(DiscSymbol, str(i), Nynode)
Next
Nynode = Nothing
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'根据磁盘符号的变更,显示根目录里的文件
ListBox1.Items.Clear()
For Each MyFile As String In System.IO.Directory.GetFiles(ComboBox1.Text)
ListBox1.Items.Add(MyFile)
Next
'根据磁盘符号的变更,重新显示目录树
Dim DiscSymbol As String
DiscSymbol = Microsoft.VisualBasic.Left(ComboBox1.Text, Len(ComboBox1.Text) - 1)
Call AddRootDirectory(DiscSymbol)
End Sub
'递归过程根据子目录寻找上级目录名--从而构成完整的目录路径
Private Sub AllPath(ByVal ThisNode As TreeNode, ByRef MyPathName As String)
If ThisNode.Level 1 Then
'该节点层数大于1,其父节点不是磁盘根目录
MyPathName = ThisNode.Parent.Text "\" MyPathName
Dim MyNode As TreeNode = ThisNode.Parent
Call AllPath(MyNode, MyPathName)
Else
'该节点层数等于1,其父节点就是磁盘根目录
MyPathName = ComboBox1.Text MyPathName
End If
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
'为了搜索选中的节点对应目录的文件,需要组成全路径
Dim MyAllPathName As String = TreeView1.SelectedNode.Text
Dim MyNode As TreeNode = TreeView1.SelectedNode
If TreeView1.SelectedNode.Level = 0 Then
'如果选中的是根节点
MyAllPathName = ComboBox1.Text
Else
'如果选中的是非根节点,调用递归过程组成全路径
Call AllPath(MyNode, MyAllPathName)
MyAllPathName = MyAllPathName "\"
End If
'根据路径,搜索文件名并显示
ListBox1.Items.Clear()
For Each MyFile As String In System.IO.Directory.GetFiles(MyAllPathName)
ListBox1.Items.Add(MyFile)
Next
End Sub
End Class
有这个条件是为了避免 当列表没有选项的时候 或者 有选项但没有选中的时候 点击删除按钮会引发错误 selectindex =-1 是没有选中的时候默认值 而列表序号从0开始 删除一个不存在的序号引发错误
da.Fill(ds1, "收费站信息表")
For i = 0 To ds1.Tables.Count - 1
ComboBox1.Items.Add((ds1.Tables(0).Rows(i).Item("收费站名称").ToString))
Next i