VB.net中沒有控件數(shù)組的說法。
專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)工農(nóng)免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
替代方法:
·創(chuàng)建一個控件的類型數(shù)組: Button[],將所有的button控件創(chuàng)建好后存進(jìn)去,可以遍歷它;
·或?qū)⑺幸闅v的控件放在一個容器如Panel中,以后遍歷這個容器的子控件即可。
----------
你的情況,推薦放在容器中。比如下面就是一個遍歷容器的控件,然后找出所有的文本框并修改內(nèi)容的程序:
//pn 是個 Panel 控件
foreach (Control item in pn.Controls)
{
if (typeof(TextBox) == item.GetType())
{
((TextBox)item).Text = "我是動態(tài)修改的!";
}
}
處理數(shù)據(jù)行(DataRow)
Windows窗體中的數(shù)據(jù)綁定列表框和組合框很節(jié)省時間 典型的代碼如下(假定已經(jīng)建立了SqlDataAdapter或者其它部件獲取數(shù)據(jù))
Dim ds As New DataSet() SqlDataAdapter Fill(ds Customers ) ListBox DataSource = ds Tables( Customers ) ListBox DisplayMember = CompanyName ListBox ValueMember = CustomerID
在這種情況下 代碼使用Northwind數(shù)據(jù)庫的顧客記錄工作 DisplayMember屬性設(shè)置為你希望用戶在列表框中看到的記錄字段 它是customers表的CompanyName 通常ValueMember屬性設(shè)置為數(shù)據(jù)表中的一個鍵字段 對于customer來說是CustomerID 一旦用戶選擇了列表框中的一行 很容易使用列表框的SelectedValue屬性獲得鍵字段
MsgBox(ListBox SelectedValue)
但是有可能需要一個與被選擇項(xiàng)相關(guān)的整個數(shù)據(jù)行對象的引用 例如 如果被選擇的行需要被刪除 就不知道鍵了 你需要一個數(shù)據(jù)行的引用以使用Delete方法
典型的Visual Basic開發(fā)者通常這樣想 我已經(jīng)得到了該行的鍵了 我將編寫一些邏輯來查找使用該鍵的行 這樣可以實(shí)現(xiàn) 但是有更好的實(shí)現(xiàn)方法 可以使用一行代碼獲取與列表框中選項(xiàng)關(guān)聯(lián)的數(shù)據(jù)行
Dim dr As DataRow = CType(ListBox SelectedItem DataRowView) Row
通常該邏輯不會憑直覺出現(xiàn) 即使對經(jīng)驗(yàn)豐富的開發(fā)者 為了解釋這是怎樣實(shí)現(xiàn)的 我把上面的一行拆成幾行 下面的代碼與上面代碼的功能相同
Dim drv As DataRowView drv = CType(ListBox SelectedItem DataRowView) Dim dr As DataRow dr = drv Row
DataRowView類是數(shù)據(jù)行的包裝 它被多個Windows窗體控件使用 它使得顯示與控件中的數(shù)據(jù)行相關(guān)的數(shù)據(jù)更加容易 當(dāng)列表框被數(shù)據(jù)綁定到數(shù)據(jù)表時(假定列表框中的有些行當(dāng)前被選定了) 列表框的SelectedItem屬性保存了一個DataRowView對象
這意味著我們能把列表框的SelectedItem屬性轉(zhuǎn)換到DataRowView對象 這就是上面代碼中的第二行實(shí)現(xiàn)的 接著DataRowView暴露一個Row屬性 它指向被包裝的數(shù)據(jù)行 上面的代碼聲明了一個數(shù)據(jù)行并設(shè)置了Row屬性
轉(zhuǎn)換對象的類型以訪問它的接口的技術(shù)在Visual Basic 中不是經(jīng)常使用 但是在Visual Basic NET中這是經(jīng)常的 有了上面的例子后 大多數(shù)有經(jīng)驗(yàn)的開發(fā)者迅速跟上了這種技術(shù)
數(shù)據(jù)行的引用(dr)可用于用任何方式維護(hù)行 訪問數(shù)據(jù)行中的任何特定字段是可行的 行中的數(shù)據(jù)可以被改變 能使數(shù)據(jù)行的Delete方法把該行標(biāo)識為刪除 或者從數(shù)據(jù)表的行集合中刪除該行 下面的代碼標(biāo)識刪除了一行
dr Delete()
lishixinzhi/Article/program/net/201311/12974
一委托:此示例演示如何將方法與委托關(guān)聯(lián)然后通過委托調(diào)用該方法。
創(chuàng)建委托和匹配過程
創(chuàng)建一個名為 MySubDelegate 的委托。
Delegate Sub MySubDelegate(ByVal x As Integer)
聲明一個類,該類包含與該委托具有相同簽名的方法。
Class class1
Sub Sub1(ByVal x As Integer)
MsgBox("The value of x is: " CStr(x))
End Sub
End Class
定義一個方法,該方法創(chuàng)建該委托的實(shí)例并通過調(diào)用內(nèi)置的 Invoke 方法調(diào)用與該委托關(guān)聯(lián)的方法。
Protected Sub DelegateTest()
Dim c1 As New class1
' Create an instance of the delegate.
Dim msd As MySubDelegate = AddressOf c1.Sub1
' Call the method.
msd.Invoke(10)
End Sub
二、事件
下面的示例程序闡釋如何在一個類中引發(fā)一個事件,然后在另一個類中處理該事件。AlarmClock 類定義公共事件 Alarm,并提供引發(fā)該事件的方法。AlarmEventArgs 類派生自 EventArgs,并定義 Alarm 事件特定的數(shù)據(jù)。WakeMeUp 類定義處理 Alarm 事件的 AlarmRang 方法。AlarmDriver 類一起使用類,將使用 WakeMeUp 的 AlarmRang 方法設(shè)置為處理 AlarmClock 的 Alarm 事件。
該示例程序使用事件和委托和引發(fā)事件中詳細(xì)說明的概念。
示例
' EventSample.vb.
'
Option Explicit
Option Strict
Imports System
Imports System.ComponentModel
Imports Microsoft.VisualBasic
Namespace EventSample
' Class that contains the data for
' the alarm event. Derives from System.EventArgs.
'
Public Class AlarmEventArgs
Inherits EventArgs
Private _snoozePressed As Boolean
Private nrings As Integer
'Constructor.
'
Public Sub New(snoozePressed As Boolean, nrings As Integer)
Me._snoozePressed = snoozePressed
Me.nrings = nrings
End Sub
' The NumRings property returns the number of rings
' that the alarm clock has sounded when the alarm event
' is generated.
'
Public ReadOnly Property NumRings() As Integer
Get
Return nrings
End Get
End Property
' The SnoozePressed property indicates whether the snooze
' button is pressed on the alarm when the alarm event is generated.
'
Public ReadOnly Property SnoozePressed() As Boolean
Get
Return _snoozePressed
End Get
End Property
' The AlarmText property that contains the wake-up message.
'
Public ReadOnly Property AlarmText() As String
Get
If _snoozePressed Then
Return "Wake Up!!! Snooze time is over."
Else
Return "Wake Up!"
End If
End Get
End Property
End Class
' Delegate declaration.
'
Public Delegate Sub AlarmEventHandler(sender As Object, _
e As AlarmEventArgs)
' The Alarm class that raises the alarm event.
'
Public Class AlarmClock
Private _snoozePressed As Boolean = False
Private nrings As Integer = 0
Private stopFlag As Boolean = False
' The Stop property indicates whether the
' alarm should be turned off.
'
Public Property [Stop]() As Boolean
Get
Return stopFlag
End Get
Set
stopFlag = value
End Set
End Property
' The SnoozePressed property indicates whether the snooze
' button is pressed on the alarm when the alarm event is generated.
'
Public Property SnoozePressed() As Boolean
Get
Return _snoozePressed
End Get
Set
_snoozePressed = value
End Set
End Property
' The event member that is of type AlarmEventHandler.
'
Public Event Alarm As AlarmEventHandler
' The protected OnAlarm method raises the event by invoking
' the delegates. The sender is always this, the current instance
' of the class.
'
Protected Overridable Sub OnAlarm(e As AlarmEventArgs)
RaiseEvent Alarm(Me, e)
End Sub
' This alarm clock does not have
' a user interface.
' To simulate the alarm mechanism it has a loop
' that raises the alarm event at every iteration
' with a time delay of 300 milliseconds,
' if snooze is not pressed. If snooze is pressed,
' the time delay is 1000 milliseconds.
'
Public Sub Start()
Do
nrings += 1
If stopFlag Then
Exit Do
Else
If _snoozePressed Then
System.Threading.Thread.Sleep(1000)
If (True) Then
Dim e As New AlarmEventArgs(_snoozePressed, nrings)
OnAlarm(e)
End If
Else
System.Threading.Thread.Sleep(300)
Dim e As New AlarmEventArgs(_snoozePressed, nrings)
OnAlarm(e)
End If
End If
Loop
End Sub
End Class
' The WakeMeUp class has a method AlarmRang that handles the
' alarm event.
'
Public Class WakeMeUp
Public Sub AlarmRang(sender As Object, e As AlarmEventArgs)
Console.WriteLine((e.AlarmText + ControlChars.Cr))
If Not e.SnoozePressed Then
If e.NumRings Mod 10 = 0 Then
Console.WriteLine(" Let alarm ring? Enter Y")
Console.WriteLine(" Press Snooze? Enter N")
Console.WriteLine(" Stop Alarm? Enter Q")
Dim input As String = Console.ReadLine()
If input.Equals("Y") Or input.Equals("y") Then
Return
Else
If input.Equals("N") Or input.Equals("n") Then
CType(sender, AlarmClock).SnoozePressed = True
Return
Else
CType(sender, AlarmClock).Stop = True
Return
End If
End If
End If
Else
Console.WriteLine(" Let alarm ring? Enter Y")
Console.WriteLine(" Stop Alarm? Enter Q")
Dim input As String = Console.ReadLine()
If input.Equals("Y") Or input.Equals("y") Then
Return
Else
CType(sender, AlarmClock).Stop = True
Return
End If
End If
End Sub
End Class
' The driver class that hooks up the event handling method of
' WakeMeUp to the alarm event of an Alarm object using a delegate.
' In a forms-based application, the driver class is the
' form.
'
Public Class AlarmDriver
Public Shared Sub Main()
' Instantiates the event receiver.
Dim w As New WakeMeUp()
' Instantiates the event source.
Dim clock As New AlarmClock()
' Wires the AlarmRang method to the Alarm event.
AddHandler clock.Alarm, AddressOf w.AlarmRang
clock.Start()
End Sub
End Class
End Namespace
Public?Class?Form1
Public?Class?SquareClass?'把多線程調(diào)用的函數(shù)封裝到類中,通過類事件返回
Public?Value?As?Double
Public?Square?As?Double
Public?Event?ThreadComplete(ByVal?Square?As?Double)
Public?Sub?CalcSquare()
Square?=?Value?*?Value
RaiseEvent?ThreadComplete(Square)
End?Sub
End?Class
Dim?WithEvents?oSquare?As?SquareClass
Private?Sub?Button1_Click(sender?As?Object,?e?As?EventArgs)?Handles?Button1.Click?'多線程返回值測試,當(dāng)線程運(yùn)行完成激發(fā)事件
oSquare?=?New?SquareClass()
Dim?t?As?New?Threading.Thread(AddressOf?oSquare.CalcSquare)
oSquare.Value?=?30
t.Start()
End?Sub
Sub?SquareEventHandler(ByVal?Square?As?Double)?Handles?oSquare.ThreadComplete?'響應(yīng)事件函數(shù)
MsgBox("The?square?is?"??Square)
End?Sub
End?Class
新聞名稱:vb.netmvc例子 vb net
標(biāo)題來源:http://m.newbst.com/article8/dogocip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、軟件開發(fā)、企業(yè)網(wǎng)站制作、做網(wǎng)站、響應(yīng)式網(wǎng)站、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)