【问题标题】:DirectCast(sender, Control).ID Not working in Vb 2008DirectCast(sender, Control).ID 在 Vb 2008 中不起作用
【发布时间】:2012-12-10 11:41:48
【问题描述】:

我需要知道我刚刚在 vb 2010 上单击的对象的名称,我可以使用 DirectCast(sender, Control).ID 来完成,但是我需要这样做的是 2008 并且 .ID 不存在2008年

我需要

的等价物
lblPatient.Text = DirectCast(sender, Control).ID

'写在 vb 2010 视觉网页开发者中

适用于 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

【问题讨论】:

    标签: vb.net winforms visual-studio-2008


    【解决方案1】:

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

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

    编辑

    由于您提到您使用的是可视化 Web 开发人员,因此我错误地认为您使用的是 ASP.NET。实际上,您正在使用 Winforms。与 ASP.NET 不同,Winforms 中没有 Id 属性。也许您想改用Name property(自 1.1 起可用)。

    【讨论】:

    • myControl.ID 和 Control.ID 都不例外
    • 我认为问题在于我使用的标签是 label(i) 的控制数组,因此有多个可能的标签可以被点击,因此我需要找到它使用 sender 添加的代码
    • @WillPeckham:你的两个 cmets 都没有解释清楚。您能否显示标签的完整事件处理程序。顺便说一句,Label 在服务器端没有点击事件。
    • 我使用AddHandler Labels(i).Click, AddressOf Label1_Click ...如果单击标签它会运行子程序
    • @WillPeckham:既然您提到您使用的是可视化 Web 开发人员,我就错误地认为您使用的是 ASP.NET。实际上,您正在使用 Winforms。您应该相应地标记它。编辑了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多