Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 ADO.NET JavaScript LINQ AjaxPro DataGridView 面向对象 Extjs GridView XML DevExpress HTML教程 Oracle jQuery 分页 GDI+ Visual C++2010 MySQL Office2010 WPF MVC Dojo WCF4.0 VB.NET Sql2005 textbox cookie WCF WinForm Discuz!NT SQL经典语句 T-SQL checkbox ASPxGridView F# asp.net SQL VS2008新特性 DropDownList Access TreeView Ajax VS2008 页面执行时间 Flex 字符串 回调 VB2005 DataSet C#时间 ASP.NET性能优化 用户在线检测 动画
Framework C#技术VB.NET VC.NET WCF WPF
当前位置: 主页 > WinForm教程 > VB.NET >

VB.Net动态创建WinFrom控件并实现缩略图,实现动态控件的Click事件

时间:2010-04-02 21:21来源:未知 作者:admin 点击:

从多方面查找了很多资料都没有办法得到想要的结果,经过自己的构思创建这种缩略图,如有高手能直接获取系统文件的缩略图,请指点,小弟不盛感激。

'''此函数的功能是实现动态创建Panel,PictureBox,TextBox,同时增加控件相应的单击与双击事件

Public Function createPic(ByVal index As Integer, ByVal path As String, ByVal point As Point, ByVal lbtext As String)
        Dim bl As Boolean
        Try

            bl = True
            Dim picbox As String, paName As String, lbName As String
            picbox = "picbox" & index
            paName = "pana" & index
            lbName = "lb" & index
            Dim MySipDImage As Bitmap
            Dim MyBipDImage As Bitmap
            ''生成新的panel
            Dim pana As System.Windows.Forms.Panel
            pana = New System.Windows.Forms.Panel
            pana.Location = New System.Drawing.Point(point)
            pana.Name = paName
            pana.Size = New System.Drawing.Size(131, 126)
            pana.TabIndex = index
            pana.Visible = True

'''将新创建的Panel放到TabPage1容器内
            Me.TabPage1.Controls.Add(pana)


            ''生成picturebox,存放缩略图
            Dim PictureBoxNew As System.Windows.Forms.PictureBox
            PictureBoxNew = New System.Windows.Forms.PictureBox()
            PictureBoxNew.Location = New System.Drawing.Point(0, 0)
            PictureBoxNew.Name = picbox
            PictureBoxNew.Size = New System.Drawing.Size(125, 74)
            MySipDImage = New Bitmap(path)
            MyBipDImage = New Bitmap(MySipDImage, PictureBoxNew.Width, PictureBoxNew.Height)
            PictureBoxNew.Image = MyBipDImage
            PictureBoxNew.TabIndex = index
            PictureBoxNew.TabStop = False
            PictureBoxNew.Visible = True
            AddHandler PictureBoxNew.DoubleClick, AddressOf newPicClick
            AddHandler PictureBoxNew.Click, AddressOf newPClick

'''将新创建的PictureBox放到新创建的Panel容器内

            pana.Controls.Add(PictureBoxNew)

            ''生成TextBox,显示缩略图的描述信息
            Dim tb As System.Windows.Forms.TextBox
            tb = New System.Windows.Forms.TextBox
            tb.Location = New System.Drawing.Point(0, 80)
            tb.Name = lbName
            tb.Size = New System.Drawing.Size(125, 43)
            tb.TabIndex = index
            tb.Text = lbtext
            tb.Multiline = True
            tb.Visible = True
            AddHandler tb.Click, AddressOf newTClick
            AddHandler tb.DoubleClick, AddressOf newTbClick

'''将新创建的TextBox放到新创建的Panel容器内
            pana.Controls.Add(tb)
            Return bl
        Catch ex As Exception
            bl = False
            Return bl
        End Try
    End Function


    ''' <summary>
    ''' 这个是PictureBox和textbox的Click的事件函数,
    ''' 所有动态产生的PictureBox在Click的时候,都会呼叫此事件
    ''' </summary>
    ''' <param name="sender">代表目前click的pictureBox对象</param>
    ''' <param name="e"></param>
    Private Sub newPClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            Dim newtb As New PictureBox
            newtb.TabIndex = CType(sender, PictureBox).TabIndex
            Dim fileP As String = FilesPath(CInt(newtb.TabIndex))

''通过文件路径去查找相应的图片(在数据库的图片路径)
            Proc_select_CriterionLibTable_FilesName(fileP)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub newPicClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            Dim newtb As New PictureBox
            newtb.TabIndex = CType(sender, PictureBox).TabIndex
            Dim fileP As String = FilesPath(CInt(newtb.TabIndex))
            openFile(fileP)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub newTbClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            Dim newtb As New TextBox
            newtb.Text = CType(sender, TextBox).TabIndex.Tostring()

            Dim fileP As String = FilesPath(CInt(newtb.Text))

''打开文件
            openFile(fileP)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Private Sub newTClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            Dim newtb As New TextBox
            newtb.Text = CType(sender, TextBox).TabIndex.ToString()
            Dim fileP As String = FilesPath(CInt(newtb.Text))

''查找数据
            Proc_Select_CriterionLibTable_Quit()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

''获取数据库信息并显示在窗体

'''在这里使用Treeview的tag记录Nodes的节点级别查找数据信息

   Private Sub Proc_Select_CriterionLibTable_Quit()
        Dim i As Integer, row_FourCatalog As Integer
        ProcStr = "Proc_Select_CriterionLibTable_Quit"
        tableName = "CriterionLibTable_Quit"
        Fname = TwFiles.SelectedNode.Text
        Dim tag As Integer = TwFiles.SelectedNode.Tag
      
        Try
            objCommand = New SqlCommand(ProcStr, conSql)
            objCommand.CommandType = CommandType.StoredProcedure

            If tag < 200 Then
                FirstCatalog = TwFiles.SelectedNode.Text
              ElseIf tag >= 200 And tag < 300 Then
                SecondCatalog = TwFiles.SelectedNode.Text
                ElseIf tag >= 300 And tag < 400 Then
                ThirdCatalog = TwFiles.SelectedNode.Text
                          ElseIf tag >= 400 And tag < 500 Then
                FourCatalog = TwFiles.SelectedNode.Text
                     ElseIf tag >= 500 Then
                Fname = TwFiles.SelectedNode.Text
                     End If
            objCommand.Parameters.AddWithValue("@SecondCatalog", SecondCatalog)
            objCommand.Parameters.AddWithValue("@FirstCatalog", FirstCatalog)
            objCommand.Parameters.AddWithValue("@ThirdCatalog", ThirdCatalog)
            objCommand.Parameters.AddWithValue("@FourCatalog", FourCatalog)
            objCommand.Parameters.AddWithValue("@Name", Fname)

            objDataAdapter = New SqlDataAdapter(objCommand)

            objDataAdapter.Fill(objDataSet, tableName)
            row_FourCatalog = objDataSet.Tables(tableName).Rows.Count

''从这里开始显示缩略图效果
            If row_FourCatalog > 0 Then
                Dim ix As Integer, iy As Integer
                ix = 6
                iy = 6

                For i = 0 To row_FourCatalog - 1
                    Fname = objDataSet.Tables("CriterionLibTable_Quit").Rows(i)("名称").ToString()
                    Dim FileName As String
                    FileName = objDataSet.Tables(tableName).Rows(i)("图片").ToString()
                    FileName = Replace(FileName, "\\", "\", 1)

                    Dim pi As Point
                    pi = New Point(ix, iy)
                    Dim lbt As String, datatime As String

                    FilesPath.Add(FileName)
                    lbt = objDataSet.Tables("CriterionLibTable_Quit").Rows(i)("描述").ToString()
                    If lbt.Length > 11 Then
                        lbt = lbt & vbCrLf
                        If lbt.Length > 20 Then
                            lbt = VB.Left(lbt, 15) & "..."
                        End If
                    End If
                    datatime = vbCrLf + objDataSet.Tables("CriterionLibTable_Quit").Rows(i)("上传日期").ToString()

''调用动态创建控件的函数
                    createPic(i, FileName, pi, lbt + datatime)
                    ix = ix + 143
                    Dim imod As Integer
                    If i > 0 Then
                        imod = i Mod 6
                        If imod = 0 Then
                            iy = iy + 138
                        End If
                    End If
                Next
            End If
            Fname = ""
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            objDataSet.Tables("CriterionLibTable_Quit").Clear()
        End Try
    End Sub

(责任编辑:admin)
Tags:动态创建W控件
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价