2012-12-10 24 views
0

我需要知道我剛纔單擊過的對象的名稱vb 2010我可以使用DirectCast(sender,Control).ID來完成此操作,但是我需要要做到這一點是2008年和.ID不會在2008年DirectCast(sender,Control).ID不能在Vb 2008中工作

存在我需要的

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

等效「writen在VB 2010的可視化Web deveoper

對於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 

回答

0

ASP.NET 3.5已經有this property(實際上Control.Id存在,因爲1.1)),所以你的代碼應該沒有問題的工作。

你能顯示完整的代碼和異常嗎?

編輯

既然你提到你正在使用的Visual Web deveoper我一直誤以爲你正在使用ASP.NET。其實你正在使用Winforms。與ASP.NET不同,Winforms中沒有Id屬性。也許你想用Name property來代替(從1.1版開始可用)。

+0

myControl.ID和Control.ID都不是例外 –

+0

我認爲問題在於我使用的標籤是標籤(i)的控件數組,因此有多個可能的標籤可能被點擊,因此爲什麼我需要使用_sender_添加的代碼 –

+0

@WillPeckham找到它:您的兩條評論都不明確。你能否顯示標籤的完整事件處理程序?順便說一句,一個'Label'在服務器端沒有點擊事件。 –