;???? 提要:在VB 中 常將TreeView用來(lái)表示層次數(shù)據(jù) 但相關(guān)的與數(shù)據(jù)庫(kù)進(jìn)行交互的代碼 需要大量的采用手工編碼;在中 由于數(shù)據(jù)綁定功能的加強(qiáng)及語(yǔ)言特性的增強(qiáng) 可以很容易的實(shí)現(xiàn)TreeView與層次數(shù)據(jù)的綁定 本文將首先建立一個(gè)繼承自TreeView的 dbTreeView 然后用一個(gè)單位(部門(mén))的層次數(shù)據(jù)與dbTreeView進(jìn)行數(shù)據(jù)綁定 并提供了與數(shù)據(jù)庫(kù)進(jìn)行交互的代碼 從層次數(shù)據(jù)的表達(dá)方式開(kāi)始在本例中 部門(mén)表(department)中有五個(gè)字段 如下表: 字段名 字段 類型說(shuō)明 ID 自動(dòng)編號(hào) Key Code String 編碼 Name String 名稱 PID Int 父結(jié)點(diǎn)的ID CPtr boolean 是否有子結(jié)點(diǎn) 繼承自TreeNode的myTreeNode在myTreeNode中 新增了三個(gè)屬性 如下表: 屬性名 類型 說(shuō)明 Value Object Key PID Object 父結(jié)點(diǎn)的ID CPtr Boolean 是否有子結(jié)點(diǎn)在Init事件中 根據(jù)傳入的四個(gè)參數(shù) 設(shè)置這三個(gè)屬性和Text屬性 將dbTreeView綁定到數(shù)據(jù)源 屬性名 類型 說(shuō)明 Datasource dataview dbTreeVIew的數(shù)據(jù)源使用dataview 而不是object Value Member string值成員(數(shù)據(jù)源[dataview]的列名) Display Member string顯示(在Text中)成員 Pid Member string父ID成員 CPtr Member string是否有子結(jié)點(diǎn)后四個(gè)屬性對(duì)應(yīng)myTreeNode的value text pid cptr 相關(guān)代碼如下: Protected Property DataSource() As Object GetReturn mDataView End Get Set(ByVal Value As Object)If Value Is Nothing ThenElse mDataView = Value cm = CType(Me BindingContext(mDataView) CurrencyManager) UpdateTreeView()End If End SetEnd PropertyProtected Property PidMember() As String GetReturn mPidMember End Get Set(ByVal Value As String)mPidMember = Value End SetEnd PropertyProtected Property DisplayMember() As String GetReturn Join(mDisplayMember SplitChar) End Get Set(ByVal Value As String)mDisplayMember = Split(Value SplitChar) End SetEnd Property 注意 這幾個(gè)屬性都是保護(hù)成員 必須在Init事件中設(shè)置:Public Sub Init(ByVal dispmember As String ByVal valuemember As String ByVal pidmember As String ByVal cptrmember As String ByVal datasource As DataView) Me ValueMember = valuemember Me DisplayMember = dispmember Me PidMember = pidmember Me CPtrMember = cptrmember Me DataSource = datasource 取value最大值 新增時(shí)將value+ 保證關(guān)健值唯一 Me mDataView Sort = Me ValueMember Me m_MaxID = Me GetValue(Me mDataView Count )End Sub
創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站制作、成都做網(wǎng)站、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、鹿邑網(wǎng)絡(luò)推廣、成都微信小程序、鹿邑網(wǎng)絡(luò)營(yíng)銷、鹿邑企業(yè)策劃、鹿邑品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供鹿邑建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:m.kartarina.com
設(shè)置DisplayMember屬性的格式如:字段 ;字段 ;字段 …
在設(shè)置屬性時(shí) 將傳來(lái)的參數(shù)轉(zhuǎn)換為字符串?dāng)?shù)組mDisplayMember 在檢索值時(shí)返回?cái)?shù)據(jù)如:值 值 值 …
Protected Overridable Function GetDisplay(ByVal Index As Integer) As Object Dim i As Integer Dim temp As String = For i = To mDisplayMember Length temp = temp IIf(i LinkChar ) mDataView(Index)(mDisplayMember(i)) Next Return tempEnd Function其它檢索值的函數(shù)請(qǐng)參見(jiàn)源程序 生成樹(shù)UpdateTreeView調(diào)用私有方法FillTree來(lái)生成樹(shù) 需要注意的 FillTree只是生成指定結(jié)點(diǎn)的子結(jié)點(diǎn)并將其添加到指定結(jié)點(diǎn) 而不是一次就將所有結(jié)點(diǎn)添加到樹(shù)中 如果未指定結(jié)點(diǎn)(第一次填充時(shí)) 只是添加頂層結(jié)點(diǎn) Private Sub FillTree(ByRef pnode As myTreeNode Optional ByVal filter As String = ) mDataView RowFilter = filter Dim i As Integer icol As Integer Dim newnode As myTreeNode RemoveHandler cm PositionChanged AddressOf cm_PositionChanged Me BeginUpdate() For i = To mDataView Count() newnode = New myTreeNode(GetDisplay(i) GetValue(i) GetPid(i) GetCPtr(i)) 當(dāng)有子結(jié)點(diǎn)時(shí) 為這個(gè)結(jié)點(diǎn)添加一個(gè)空子結(jié)點(diǎn)If newnode CPtr Then Dim nullnode As New myTreeNode() nullnode Value = NoExpandNodeValue newnode Nodes Add(nullnode)End IfIf pnode Is Nothing Then Me Nodes Clear() Me Nodes Add(newnode)Else pnode Nodes Add(newnode)End If Next Me EndUpdate() mDataView RowFilter = AddHandler cm PositionChanged AddressOf cm_PositionChangedEnd Sub在展開(kāi)有子結(jié)點(diǎn)的結(jié)點(diǎn)前 刪除所有子結(jié)點(diǎn) 再用FillTree為待展開(kāi)結(jié)點(diǎn)新增子結(jié)點(diǎn) Private Sub dbTreeView_BeforeExpand(ByVal sender As Object ByVal e As System Windows Forms TreeViewCancelEventArgs) Handles MyBase BeforeExpand 當(dāng)是新增結(jié)點(diǎn)引起B(yǎng)eforeExpand事件時(shí) 直接退出 If ExpandWhenAddNode Then Exit Sub 在展開(kāi)結(jié)點(diǎn)前更新子結(jié)點(diǎn) Dim currentnode As myTreeNode = CType(e Node myTreeNode) With currentnode Nodes Clear()FillTree(currentnode mPidMember = CInt( Value)) End WithEnd Sub 實(shí)現(xiàn)數(shù)據(jù)與綁定控件的同步要實(shí)現(xiàn)兩個(gè)方面的同步: 其它綁定控件(如textbox等)應(yīng)與TreeView當(dāng)前結(jié)點(diǎn)所指向的記錄位置一致 Private Sub dbTreeView_AfterSelect(ByVal sender As Object ByVal e As System Windows Forms TreeViewEventArgs) Handles MyBase AfterSelect If e Node Is Nothing Then Exit Sub 定位到position cm Position = GetPosition(CType(e Node myTreeNode) Value) If AllowEdit ThenoldNode = e NodeoldPos = cm Position End IfEnd Sub 在其它綁定控件改變了數(shù)據(jù)源后 更新樹(shù)結(jié)點(diǎn) 這個(gè)工作在觸發(fā)CurrencyManager的PositionChanged事件時(shí)進(jìn)行 Public Sub cm_PositionChanged(ByVal sender As Object ByVal e As System EventArgs) If CType(Me SelectedNode myTreeNode) Value GetValue(cm Position) ThenDebug WriteLine( Current node isn t correct point to currencymanager position! )Me SelectedNode = FindNodeByValue(GetValue(cm Position) Me Nodes) End If If AllowEdit ThenIf Me SelectedNode Is Nothing AndAlso cm Position = cm Count Then 當(dāng)新增記錄時(shí) 新增樹(shù)結(jié)點(diǎn) If CType(cm Current DataRowView) IsNew ThenMe SelectedNode = AddNode(cm Position)Exit Sub End IfEnd IfIf Not oldNode Is Nothing Then If CType(oldNode myTreeNode) Value = GetValue(oldPos) Then 更新老結(jié)點(diǎn)oldNode Text = GetDisplay(oldPos) Else End IfEnd If End IfEnd Sub
使用dbTreeView程序運(yùn)行后界面如下:相關(guān)代碼請(qǐng)參見(jiàn)源程序 這里不做詳述
lishixinzhi/Article/program/net/201311/13916
可以使用Datalist,
Repeater
等控件.
綁定數(shù)據(jù)源后,在ItemTemplate添加label,
設(shè)定label的Text為某列的值.
不懂可以加我QQ問(wèn).注明"dotnet"
vb.net里沒(méi)有像vb那樣的控件數(shù)組。也就是說(shuō)復(fù)制控件后粘貼沒(méi)有提示你是否創(chuàng)建控件數(shù)組。
你可以試驗(yàn)下面的代碼。
Form1.Designer.vb 設(shè)計(jì)器中的窗體代碼如下,上面有六個(gè)button。最右邊的用來(lái)改變前5個(gè)地text:
Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated() Partial Class Form1
#Region "Windows フォーム デザイナによって生成されたコード "
System.Diagnostics.DebuggerNonUserCode() Public Sub New()
MyBase.New()
'この呼び出しは、Windows フォーム デザイナで必要です。
InitializeComponent()
End Sub
'Form は、コンポーネント一覧に后処理を?qū)g行するために dispose をオーバーライドします。
System.Diagnostics.DebuggerNonUserCode() Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
If Disposing Then
If Not components Is Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(Disposing)
End Sub
'Windows フォーム デザイナで必要です。
Private components As System.ComponentModel.IContainer
Public ToolTip1 As System.Windows.Forms.ToolTip
Public WithEvents Command2 As System.Windows.Forms.Button
Public WithEvents _Command1_4 As System.Windows.Forms.Button
Public WithEvents _Command1_3 As System.Windows.Forms.Button
Public WithEvents _Command1_2 As System.Windows.Forms.Button
Public WithEvents _Command1_1 As System.Windows.Forms.Button
Public WithEvents _Command1_0 As System.Windows.Forms.Button
Public WithEvents Command1 As Microsoft.VisualBasic.Compatibility.VB6.ButtonArray
'メモ: 以下のプロシージャは Windows フォーム デザイナで必要です。
'Windows フォーム デザイナを使って変更できます。
'コード エディタを使用して、変更しないでください。
System.Diagnostics.DebuggerStepThrough() Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
Me.Command2 = New System.Windows.Forms.Button
Me._Command1_4 = New System.Windows.Forms.Button
Me._Command1_3 = New System.Windows.Forms.Button
Me._Command1_2 = New System.Windows.Forms.Button
Me._Command1_1 = New System.Windows.Forms.Button
Me._Command1_0 = New System.Windows.Forms.Button
Me.Command1 = New Microsoft.VisualBasic.Compatibility.VB6.ButtonArray(Me.components)
CType(Me.Command1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Command2
'
Me.Command2.BackColor = System.Drawing.SystemColors.Control
Me.Command2.Cursor = System.Windows.Forms.Cursors.Default
Me.Command2.ForeColor = System.Drawing.SystemColors.ControlText
Me.Command2.Location = New System.Drawing.Point(235, 176)
Me.Command2.Name = "Command2"
Me.Command2.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Command2.Size = New System.Drawing.Size(65, 25)
Me.Command2.TabIndex = 5
Me.Command2.Text = "Change"
Me.Command2.UseVisualStyleBackColor = False
'
'_Command1_4
'
Me._Command1_4.BackColor = System.Drawing.SystemColors.Control
Me._Command1_4.Cursor = System.Windows.Forms.Cursors.Default
Me._Command1_4.ForeColor = System.Drawing.SystemColors.ControlText
Me.Command1.SetIndex(Me._Command1_4, CType(4, Short))
Me._Command1_4.Location = New System.Drawing.Point(16, 176)
Me._Command1_4.Name = "_Command1_4"
Me._Command1_4.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Command1_4.Size = New System.Drawing.Size(93, 25)
Me._Command1_4.TabIndex = 4
Me._Command1_4.Text = "Command1"
Me._Command1_4.UseVisualStyleBackColor = False
'
'_Command1_3
'
Me._Command1_3.BackColor = System.Drawing.SystemColors.Control
Me._Command1_3.Cursor = System.Windows.Forms.Cursors.Default
Me._Command1_3.ForeColor = System.Drawing.SystemColors.ControlText
Me.Command1.SetIndex(Me._Command1_3, CType(3, Short))
Me._Command1_3.Location = New System.Drawing.Point(16, 137)
Me._Command1_3.Name = "_Command1_3"
Me._Command1_3.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Command1_3.Size = New System.Drawing.Size(93, 25)
Me._Command1_3.TabIndex = 3
Me._Command1_3.Text = "Command1"
Me._Command1_3.UseVisualStyleBackColor = False
'
'_Command1_2
'
Me._Command1_2.BackColor = System.Drawing.SystemColors.Control
Me._Command1_2.Cursor = System.Windows.Forms.Cursors.Default
Me._Command1_2.ForeColor = System.Drawing.SystemColors.ControlText
Me.Command1.SetIndex(Me._Command1_2, CType(2, Short))
Me._Command1_2.Location = New System.Drawing.Point(16, 96)
Me._Command1_2.Name = "_Command1_2"
Me._Command1_2.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Command1_2.Size = New System.Drawing.Size(93, 25)
Me._Command1_2.TabIndex = 2
Me._Command1_2.Text = "Command1"
Me._Command1_2.UseVisualStyleBackColor = False
'
'_Command1_1
'
Me._Command1_1.BackColor = System.Drawing.SystemColors.Control
Me._Command1_1.Cursor = System.Windows.Forms.Cursors.Default
Me._Command1_1.ForeColor = System.Drawing.SystemColors.ControlText
Me.Command1.SetIndex(Me._Command1_1, CType(1, Short))
Me._Command1_1.Location = New System.Drawing.Point(16, 56)
Me._Command1_1.Name = "_Command1_1"
Me._Command1_1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Command1_1.Size = New System.Drawing.Size(93, 25)
Me._Command1_1.TabIndex = 1
Me._Command1_1.Text = "Command1"
Me._Command1_1.UseVisualStyleBackColor = False
'
'_Command1_0
'
Me._Command1_0.BackColor = System.Drawing.SystemColors.Control
Me._Command1_0.Cursor = System.Windows.Forms.Cursors.Default
Me._Command1_0.ForeColor = System.Drawing.SystemColors.ControlText
Me.Command1.SetIndex(Me._Command1_0, CType(0, Short))
Me._Command1_0.Location = New System.Drawing.Point(16, 16)
Me._Command1_0.Name = "_Command1_0"
Me._Command1_0.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Command1_0.Size = New System.Drawing.Size(93, 25)
Me._Command1_0.TabIndex = 0
Me._Command1_0.Text = "Command1"
Me._Command1_0.UseVisualStyleBackColor = False
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.BackColor = System.Drawing.SystemColors.Control
Me.ClientSize = New System.Drawing.Size(312, 213)
Me.Controls.Add(Me.Command2)
Me.Controls.Add(Me._Command1_4)
Me.Controls.Add(Me._Command1_3)
Me.Controls.Add(Me._Command1_2)
Me.Controls.Add(Me._Command1_1)
Me.Controls.Add(Me._Command1_0)
Me.Cursor = System.Windows.Forms.Cursors.Default
Me.Location = New System.Drawing.Point(4, 23)
Me.Name = "Form1"
Me.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Text = "Form1"
CType(Me.Command1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
End Class
’==========================================
Form1中的代碼如下:
Friend Class Form1
Inherits System.Windows.Forms.Form
Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command2.Click
Dim i As Object
'按鈕標(biāo)題數(shù)組。
Dim cArr() As String = New String() {"A", "B", "C", "D", "E", "F", "G"}
For i = 0 To Me.Command1.Count - 1
Me.Command1(i).Text = cArr(i)
Next
End Sub
End Class
比如說(shuō)這樣添加的計(jì)時(shí)器:
Dim Timer1 As New Timer
你要在合適的地方開(kāi)始,比如窗體Load事件中,這樣:
Timer1.Interval = 1000 '單位毫秒,1000毫秒=1秒
Timer1.Enabled = True '啟用計(jì)時(shí)器
然后在代碼窗口上方下拉列表里選擇Timer1的Tick事件,自動(dòng)添加代碼:
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
End Sub
這是計(jì)時(shí)器觸發(fā)的事件,每秒觸發(fā)一次,在這里面輸入循環(huán)判斷是否有日程即可。提示:你可以用Now對(duì)象獲取當(dāng)前的日期和時(shí)間。
Private Sub setdgdDokyuStyle()
Dim lv_DataTable_Program As New DataTable
Dim lv_DataView_Program As DataView
Dim lv_DataColumn_Check As New DataColumn("選択", GetType(Boolean)) '選択
Dim lv_DataColumn_Num As New DataColumn("內(nèi)容", GetType(String)) '內(nèi)容
Dim ts As New DataGridTableStyle
Dim tc As DataGridTextBoxColumn
Dim tb As DataGridBoolColumn
dgdDokyu.CaptionVisible = False
lv_DataTable_Program.Columns.Clear()
lv_DataTable_Program.Columns.Add(lv_DataColumn_Check)
lv_DataTable_Program.Columns.Add(lv_DataColumn_Num)
lv_DataView_Program = New DataView(lv_DataTable_Program)
dgdDokyu.DataSource = lv_DataView_Program.Table
lv_DataView_Program.Table.DefaultView().AllowNew = False
'開(kāi)始屬性を設(shè)定
ts.MappingName = lv_DataTable_Program.TableName
ts.RowHeadersVisible = False
tb = New DataGridBoolColumn
With tb
.MappingName = "選択" '選択
.HeaderText = "選択" '選択
.Width = 50
.ReadOnly = False
.AllowNull = False
End With
ts.GridColumnStyles.Add(tb)
tc = New DataGridTextBoxColumn
With tc
.MappingName = "內(nèi)容" '內(nèi)容
.HeaderText = "內(nèi)容" '內(nèi)容
.Width = 686
.NullText = ""
.ReadOnly = False
End With
ts.GridColumnStyles.Add(tc)
dgdDokyu.TableStyles.Clear()
dgdDokyu.TableStyles.Add(ts)
End Sub
在配置文件app.config中加入
appSettings
add key="LotDBConnection" value="DATA SOURCE=數(shù)據(jù)庫(kù)連接;PASSWORD=密碼;PERSIST SECURITY INFO=True;USER ID=登錄名"/
/appSettings
//m_SqlPath 存放SQL語(yǔ)句的XML文件
Dim sqlHelper As New SQLHelper("LotDBConnection")
Dim sqlRead As New ResourceHelper
Dim ht As New Hashtable
Dim strSQL As String
Try
'設(shè)置SQL文參數(shù)
ht.Clear()
ht.Add(ColumnName.USER_ID.ToString, Trim(Me.txtUserID.Text))
ht.Add(ColumnName.USER_NAME.ToString, Trim(Me.txtUserName.Text))
'數(shù)據(jù)查詢
strSQL = sqlRead.GetSQLSentence("GetUserList", m_SqlPath, ht)
ds = sqlHelper.ExecuteDataSet(strSQL)
If ds.Tables(0).Rows.Count = 0 Then
MessageBox .Show ("數(shù)據(jù)不存在");
Else
'數(shù)據(jù)綁定
dgvUserMeisai.DataSource = ds.Tables(0)
End If
Catch ex As Exception
MessageBox .Show (ex.Message );
Finally
sqlHelper = Nothing
sqlRead = Nothing
ht = Nothing
End Try
文章名稱:vb.net如何捆綁控件 vbs怎么綁定窗口
轉(zhuǎn)載來(lái)于:http://m.kartarina.com/article4/hgjjoe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、ChatGPT、微信小程序、全網(wǎng)營(yíng)銷推廣、品牌網(wǎng)站制作、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)