vb.net如何捆绑控件 vbs怎么绑定窗口
VB.NET中层次数据绑定TreeView的实现
; 提要:在VB 中 常将TreeView用来表示层次数据 但相关的与数据库进行交互的代码 需要大量的采用手工编码;在中 由于数据绑定功能的加强及语言特性的增强 可以很容易的实现TreeView与层次数据的绑定 本文将首先建立一个继承自TreeView的 dbTreeView 然后用一个单位(部门)的层次数据与dbTreeView进行数据绑定 并提供了与数据库进行交互的代码 从层次数据的表达方式开始在本例中 部门表(department)中有五个字段 如下表: 字段名 字段 类型说明 ID 自动编号 Key Code String 编码 Name String 名称 PID Int 父结点的ID CPtr boolean 是否有子结点 继承自TreeNode的myTreeNode在myTreeNode中 新增了三个属性 如下表: 属性名 类型 说明 Value Object Key PID Object 父结点的ID CPtr Boolean 是否有子结点在Init事件中 根据传入的四个参数 设置这三个属性和Text属性 将dbTreeView绑定到数据源 属性名 类型 说明 Datasource dataview dbTreeVIew的数据源使用dataview 而不是object Value Member string值成员(数据源[dataview]的列名) Display Member string显示(在Text中)成员 Pid Member string父ID成员 CPtr Member string是否有子结点后四个属性对应myTreeNode的value text pid cptr 相关代码如下: 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 注意 这几个属性都是保护成员 必须在Init事件中设置: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最大值 新增时将value+ 保证关健值唯一 Me mDataView Sort = Me ValueMember Me m_MaxID = Me GetValue(Me mDataView Count )End Sub
创新互联公司专注为客户提供全方位的互联网综合服务,包含不限于成都网站制作、成都做网站、外贸营销网站建设、鹿邑网络推广、成都微信小程序、鹿邑网络营销、鹿邑企业策划、鹿邑品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联公司为所有大学生创业者提供鹿邑建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com
设置DisplayMember属性的格式如:字段 ;字段 ;字段 …
在设置属性时 将传来的参数转换为字符串数组mDisplayMember 在检索值时返回数据如:值 值 值 …
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其它检索值的函数请参见源程序 生成树UpdateTreeView调用私有方法FillTree来生成树 需要注意的 FillTree只是生成指定结点的子结点并将其添加到指定结点 而不是一次就将所有结点添加到树中 如果未指定结点(第一次填充时) 只是添加顶层结点 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)) 当有子结点时 为这个结点添加一个空子结点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在展开有子结点的结点前 删除所有子结点 再用FillTree为待展开结点新增子结点 Private Sub dbTreeView_BeforeExpand(ByVal sender As Object ByVal e As System Windows Forms TreeViewCancelEventArgs) Handles MyBase BeforeExpand 当是新增结点引起BeforeExpand事件时 直接退出 If ExpandWhenAddNode Then Exit Sub 在展开结点前更新子结点 Dim currentnode As myTreeNode = CType(e Node myTreeNode) With currentnode Nodes Clear()FillTree(currentnode mPidMember = CInt( Value)) End WithEnd Sub 实现数据与绑定控件的同步要实现两个方面的同步: 其它绑定控件(如textbox等)应与TreeView当前结点所指向的记录位置一致 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 在其它绑定控件改变了数据源后 更新树结点 这个工作在触发CurrencyManager的PositionChanged事件时进行 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 当新增记录时 新增树结点 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 更新老结点oldNode Text = GetDisplay(oldPos) Else End IfEnd If End IfEnd Sub
使用dbTreeView程序运行后界面如下:相关代码请参见源程序 这里不做详述
lishixinzhi/Article/program/net/201311/13916
用.net做网页!用VB语言怎么把label控件绑定到一个数据库的列!
可以使用Datalist,
Repeater
等控件.
绑定数据源后,在ItemTemplate添加label,
设定label的Text为某列的值.
不懂可以加我QQ问.注明"dotnet"
VB.net 如何绑定许多button和数组
vb.net里没有像vb那样的控件数组。也就是说复制控件后粘贴没有提示你是否创建控件数组。
你可以试验下面的代码。
Form1.Designer.vb 设计器中的窗体代码如下,上面有六个button。最右边的用来改变前5个地text:
Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated() Partial Class Form1
#Region "Windows フォーム デザイナによって生成されたコード "
System.Diagnostics.DebuggerNonUserCode() Public Sub New()
MyBase.New()
'この呼び出しは、Windows フォーム デザイナで必要です。
InitializeComponent()
End Sub
'Form は、コンポーネント一覧に后処理を実行するために 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
'按钮标题数组。
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
vb.net动态添加控件,并且绑定了一些事件,这些事件只能对最新生成的控件做修改,之前生成的无法修改
比如说这样添加的计时器:
Dim Timer1 As New Timer
你要在合适的地方开始,比如窗体Load事件中,这样:
Timer1.Interval = 1000 '单位毫秒,1000毫秒=1秒
Timer1.Enabled = True '启用计时器
然后在代码窗口上方下拉列表里选择Timer1的Tick事件,自动添加代码:
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
End Sub
这是计时器触发的事件,每秒触发一次,在这里面输入循环判断是否有日程即可。提示:你可以用Now对象获取当前的日期和时间。
高分求助!vb.net中如何在DataGird中绑定一列checkbox控件
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("内容", GetType(String)) '内容
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
'开始属性を设定
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 = "内容" '内容
.HeaderText = "内容" '内容
.Width = 686
.NullText = ""
.ReadOnly = False
End With
ts.GridColumnStyles.Add(tc)
dgdDokyu.TableStyles.Clear()
dgdDokyu.TableStyles.Add(ts)
End Sub
vb.net的DataGridView数据绑定
在配置文件app.config中加入
appSettings
add key="LotDBConnection" value="DATA SOURCE=数据库连接;PASSWORD=密码;PERSIST SECURITY INFO=True;USER ID=登录名"/
/appSettings
//m_SqlPath 存放SQL语句的XML文件
Dim sqlHelper As New SQLHelper("LotDBConnection")
Dim sqlRead As New ResourceHelper
Dim ht As New Hashtable
Dim strSQL As String
Try
'设置SQL文参数
ht.Clear()
ht.Add(ColumnName.USER_ID.ToString, Trim(Me.txtUserID.Text))
ht.Add(ColumnName.USER_NAME.ToString, Trim(Me.txtUserName.Text))
'数据查询
strSQL = sqlRead.GetSQLSentence("GetUserList", m_SqlPath, ht)
ds = sqlHelper.ExecuteDataSet(strSQL)
If ds.Tables(0).Rows.Count = 0 Then
MessageBox .Show ("数据不存在");
Else
'数据绑定
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怎么绑定窗口
转载来于:http://myzitong.com/article/hgjjoe.html