DirectCast(sender, Control).ID 在 Vb 2008 中不起作用



我需要知道我刚刚点击vb 2010的对象的名称,我可以使用DirectCast(sender,Control).ID来做到这一点,但是我需要这样做是2008年,而.ID在2008年不存在

我需要相当于

lblPatient.Text = DirectCast(sender, Control).ID

'写在 VB 2010 Visual Web 开发人员

对于 vb 2008

法典

    Imports System.Data.SqlClient
Imports System.Data
Public Class FormRm3A
    Dim Labels(40) As Label
    Dim X As Integer
    Dim Y As Integer
    Private Sub FormArrayTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer
        i = 1
        For Me.Y = 1 To 5
            For Me.X = 1 To 8
                'creates new textbox
                Labels(i) = New Label()
                'set its properties
                Labels(i).Width = 50
                Labels(i).Height = 35
                Labels(i).Left = X * 49
                Labels(i).Top = 30 + (Y * 34)
                Labels(i).BorderStyle = BorderStyle.FixedSingle
                'add control to current form
                Me.Controls.Add(Labels(i))
                If Clicky = True Then
                    AddHandler Labels(i).Click, AddressOf Label1_Click
                End If
                i = i + 1
            Next X
        Next Y
        Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Dim Subject As String
        Dim StaffInitials As String
        For Session = 1 To 40
            Try
                con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("RoomBookingSystem.My.MySettings.Database1ConnectionString1").ConnectionString
                Dim SessionParm As New SqlParameter("Session", Session)
                SessionParm.Direction = ParameterDirection.Input
                con.Open()
                cmd.Connection = con
                cmd.Parameters.Add(SessionParm)
                cmd.CommandText = "SELECT Subject, StaffInitials FROM PermanantBooking WHERE (Week = 'A') AND(Room = 'Rm3') AND (Session = @Session)"
                Dim lrd As SqlDataReader = cmd.ExecuteReader()
                While lrd.Read()
                    Subject = Convert.ToString((lrd("Subject").trim))
                    StaffInitials = Convert.ToString((lrd("StaffInitials").trim))
                    Labels(Session).Text = Subject & "" & vbNewLine & StaffInitials
                End While
                'Catch ex As Exception
                ' MsgBox("" & ex.Message)
                'here if there is an error it will go here (can use Msgbox or lable)
            Finally
                cmd.Parameters.Clear()
                con.Close()
            End Try
        Next
    End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim objectID As String
    objectID = DirectCast(sender, Control).ToString
    Day = 
    Period = X
    FormMakeBookingDetails.Show()
    Me.Hide()
End Sub
End Class

ASP.NET 3.5 已经具有此属性(实际上Control.Id自 1.1 以来就存在)),因此您的代码应该可以正常工作。

你能显示完整的代码和异常吗?

编辑

既然你提到你正在使用可视化网络开发人员,我错误地认为你正在使用 ASP.NET。实际上你正在使用Winforms。与 ASP.NET 不同,Winforms没有Id财产。也许您想改用 Name 属性(从 1.1 开始可用)。

最新更新