如何识别我所在的控件(vb.net cf)

如何识别我所在的控件(vb.net cf),vb.net,compact-framework,Vb.net,Compact Framework,我对动态运行时控件有问题 我为每个记录创建一组控件,以显示在窗体上 我在每个控件上添加记录ID作为标记,以标识它们属于哪个记录 While rCONT.Read Dim txtphome As New TextBox txtphome.Name = "phone" + rCONT.Item("pcontID").ToString txtphome.Text = rCONT.Item("pcontPhHome").ToString txtphome.Tag = rC

我对动态运行时控件有问题

我为每个记录创建一组控件,以显示在窗体上

我在每个控件上添加记录ID作为标记,以标识它们属于哪个记录

While rCONT.Read
    Dim txtphome As New TextBox
    txtphome.Name = "phone" + rCONT.Item("pcontID").ToString
    txtphome.Text = rCONT.Item("pcontPhHome").ToString
    txtphome.Tag = rCONT.Item("pcontID").ToString
    tcPatientDetails.TabPages(2).Controls.Add(txtphome)
       AddHandler txtphome.LostFocus, AddressOf SaveContactChange
       AddHandler txtphome.GotFocus, AddressOf SetContactNumber
End While

在SetContactNumber中,我想保存标记值,如何识别触发它的控件

假设您的SetContactNumber事件定义为:

C#

VB

  Private sub SetContactNumber(sender As object, e As EventArgs)
      //Stuff that happens when the SetContactNumber event is raised
  End Sub
  CType(sender, textbox).tag = "Whatever you wanted to put in here"
  Private Sub SetContactNumber(sender As Object, e As EventArgs)
      Dim thisTextbox As TextBox = CType(sender, Textbox)
      thisTextbox.tag = "Whatever you wanted to put in here"
  End Sub
Sender参数是引发事件的对象。因此,您只需将其强制转换并将值附加到标记:

C#

VB

  Private sub SetContactNumber(sender As object, e As EventArgs)
      //Stuff that happens when the SetContactNumber event is raised
  End Sub
  CType(sender, textbox).tag = "Whatever you wanted to put in here"
  Private Sub SetContactNumber(sender As Object, e As EventArgs)
      Dim thisTextbox As TextBox = CType(sender, Textbox)
      thisTextbox.tag = "Whatever you wanted to put in here"
  End Sub
tag属性采用object类型的值,因此指定的值可以是您喜欢的任何内容:字符串、对象、类实例等。当您将该对象从tag属性中拉出并使用它时,您有责任强制转换该对象

因此,当您将它们放在一起时,这将拉取引发事件的对象,将其转换为文本框,并将您指定的值转储到tag属性中

C#

VB

  Private sub SetContactNumber(sender As object, e As EventArgs)
      //Stuff that happens when the SetContactNumber event is raised
  End Sub
  CType(sender, textbox).tag = "Whatever you wanted to put in here"
  Private Sub SetContactNumber(sender As Object, e As EventArgs)
      Dim thisTextbox As TextBox = CType(sender, Textbox)
      thisTextbox.tag = "Whatever you wanted to put in here"
  End Sub

假设您的SetContactNumber事件定义为:

C#

VB

  Private sub SetContactNumber(sender As object, e As EventArgs)
      //Stuff that happens when the SetContactNumber event is raised
  End Sub
  CType(sender, textbox).tag = "Whatever you wanted to put in here"
  Private Sub SetContactNumber(sender As Object, e As EventArgs)
      Dim thisTextbox As TextBox = CType(sender, Textbox)
      thisTextbox.tag = "Whatever you wanted to put in here"
  End Sub
Sender参数是引发事件的对象。因此,您只需将其强制转换并将值附加到标记:

C#

VB

  Private sub SetContactNumber(sender As object, e As EventArgs)
      //Stuff that happens when the SetContactNumber event is raised
  End Sub
  CType(sender, textbox).tag = "Whatever you wanted to put in here"
  Private Sub SetContactNumber(sender As Object, e As EventArgs)
      Dim thisTextbox As TextBox = CType(sender, Textbox)
      thisTextbox.tag = "Whatever you wanted to put in here"
  End Sub
tag属性采用object类型的值,因此指定的值可以是您喜欢的任何内容:字符串、对象、类实例等。当您将该对象从tag属性中拉出并使用它时,您有责任强制转换该对象

因此,当您将它们放在一起时,这将拉取引发事件的对象,将其转换为文本框,并将您指定的值转储到tag属性中

C#

VB

  Private sub SetContactNumber(sender As object, e As EventArgs)
      //Stuff that happens when the SetContactNumber event is raised
  End Sub
  CType(sender, textbox).tag = "Whatever you wanted to put in here"
  Private Sub SetContactNumber(sender As Object, e As EventArgs)
      Dim thisTextbox As TextBox = CType(sender, Textbox)
      thisTextbox.tag = "Whatever you wanted to put in here"
  End Sub

+1; 很好的解释。但我得到的印象是,他在循环中分配了标记,并希望在事件处理程序中使用它(但有点不清楚)。。。然后就在那之后,我意识到我所有的例子都是用c写的,他想要VB…+1;很好的解释。但我得到的印象是,他在循环中分配了标记,并希望在事件处理程序中使用它(但有点不清楚)。。。然后就在那之后,我意识到我所有的例子都是用c写的,他想要VB。。。