资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

vb.netmvc例子 vb net

VB中控件数组在VB.NET中用法,请给一个例子

VB.net中没有控件数组的说法。

专注于为中小企业提供成都网站建设、成都网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业工农免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

替代方法:

·创建一个控件的类型数组: Button[],将所有的button控件创建好后存进去,可以遍历它;

·或将所有要遍历的控件放在一个容器如Panel中,以后遍历这个容器的子控件即可。

----------

你的情况,推荐放在容器中。比如下面就是一个遍历容器的控件,然后找出所有的文本框并修改内容的程序:

//pn 是个 Panel 控件

foreach (Control item in pn.Controls)

{

if (typeof(TextBox) == item.GetType())

{

((TextBox)item).Text = "我是动态修改的!";

}

}

使用VB.NET的五个技巧之处理数据行

处理数据行(DataRow)

Windows窗体中的数据绑定列表框和组合框很节省时间 典型的代码如下(假定已经建立了SqlDataAdapter或者其它部件获取数据)

Dim ds As New DataSet() SqlDataAdapter Fill(ds Customers ) ListBox DataSource = ds Tables( Customers ) ListBox DisplayMember = CompanyName ListBox ValueMember = CustomerID

在这种情况下 代码使用Northwind数据库的顾客记录工作 DisplayMember属性设置为你希望用户在列表框中看到的记录字段 它是customers表的CompanyName 通常ValueMember属性设置为数据表中的一个键字段 对于customer来说是CustomerID 一旦用户选择了列表框中的一行 很容易使用列表框的SelectedValue属性获得键字段

MsgBox(ListBox SelectedValue)

但是有可能需要一个与被选择项相关的整个数据行对象的引用 例如 如果被选择的行需要被删除 就不知道键了 你需要一个数据行的引用以使用Delete方法

典型的Visual Basic开发者通常这样想 我已经得到了该行的键了 我将编写一些逻辑来查找使用该键的行 这样可以实现 但是有更好的实现方法 可以使用一行代码获取与列表框中选项关联的数据行

Dim dr As DataRow = CType(ListBox SelectedItem DataRowView) Row

通常该逻辑不会凭直觉出现 即使对经验丰富的开发者 为了解释这是怎样实现的 我把上面的一行拆成几行 下面的代码与上面代码的功能相同

Dim drv As DataRowView drv = CType(ListBox SelectedItem DataRowView) Dim dr As DataRow dr = drv Row

DataRowView类是数据行的包装 它被多个Windows窗体控件使用 它使得显示与控件中的数据行相关的数据更加容易 当列表框被数据绑定到数据表时(假定列表框中的有些行当前被选定了) 列表框的SelectedItem属性保存了一个DataRowView对象

这意味着我们能把列表框的SelectedItem属性转换到DataRowView对象 这就是上面代码中的第二行实现的 接着DataRowView暴露一个Row属性 它指向被包装的数据行 上面的代码声明了一个数据行并设置了Row属性

转换对象的类型以访问它的接口的技术在Visual Basic 中不是经常使用 但是在Visual Basic NET中这是经常的 有了上面的例子后 大多数有经验的开发者迅速跟上了这种技术

数据行的引用(dr)可用于用任何方式维护行 访问数据行中的任何特定字段是可行的 行中的数据可以被改变 能使数据行的Delete方法把该行标识为删除 或者从数据表的行集合中删除该行 下面的代码标识删除了一行

dr Delete()

lishixinzhi/Article/program/net/201311/12974

vb.net中如何用事件和委托,会C#中的事件和委托,但不知VB.net中的语法,望给个简单的例子熟悉语法。

一委托:此示例演示如何将方法与委托关联然后通过委托调用该方法。

创建委托和匹配过程

创建一个名为 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

定义一个方法,该方法创建该委托的实例并通过调用内置的 Invoke 方法调用与该委托关联的方法。

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

二、事件

下面的示例程序阐释如何在一个类中引发一个事件,然后在另一个类中处理该事件。AlarmClock 类定义公共事件 Alarm,并提供引发该事件的方法。AlarmEventArgs 类派生自 EventArgs,并定义 Alarm 事件特定的数据。WakeMeUp 类定义处理 Alarm 事件的 AlarmRang 方法。AlarmDriver 类一起使用类,将使用 WakeMeUp 的 AlarmRang 方法设置为处理 AlarmClock 的 Alarm 事件。

该示例程序使用事件和委托和引发事件中详细说明的概念。

示例

' 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

vb.net多线程如何返回参数,举个例子,谢谢

Public Class Form1

Public Class SquareClass '把多线程调用的函数封装到类中,通过类事件返回

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 '多线程返回值测试,当线程运行完成激发事件

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 '响应事件函数

MsgBox("The square is "  Square)

End Sub

End Class


文章名称:vb.netmvc例子 vb net
文章起源:http://cdkjz.cn/article/dogocip.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220