Home > Mobile >  is there any way to create Onclick event while program is on?
is there any way to create Onclick event while program is on?

Time:11-23

I want when I click on link label it give me the name of the textbox which in the same line in a variable named AA `

    Dim serial As Integer = 1
    Public Function addnewline()
        Dim lbl As New System.Windows.Forms.LinkLabel
        Dim txt As New System.Windows.Forms.TextBox

        ' add label
        Me.Controls.Add(lbl)
        lbl.Top = serial * 27
        lbl.Left = 100
        lbl.Text = Me.serial
        lbl.Name = "lbl" & Me.serial

        ' add textbox
        Me.Controls.Add(txt)
        txt.Top = serial * 27
        txt.Left = 200
        txt.Height = 500
        txt.Width = 100
        txt.TextAlign = HorizontalAlignment.Center
        txt.Text = "text" & Me.serial
        txt.Name = "txt" & Me.serial

        serial  = 1
        Return lbl
        Return txt

    End Function

` here is a gif for my code enter image description here

CodePudding user response:

We want two changes: set the textbox to the label's .Tag property, and use AddHandler for the click event. While I'm here I'm also going to fix a few other things that didn't make sense (only one return is allowed, and even that was not needed) and reduce flickering:

Dim serial As Integer = 1

Public Sub addnewline()
    'Textbox
    Dim txt As New System.Windows.Forms.TextBox
    txt.Top = serial * 27
    txt.Left = 200
    txt.Height = 500
    txt.Width = 100
    txt.TextAlign = HorizontalAlignment.Center
    txt.Text = "text" & Me.serial
    txt.Name = "txt" & Me.serial

    'Label 
    Dim lbl As New System.Windows.Forms.LinkLabel
    lbl.Top = serial * 27
    lbl.Left = 100
    lbl.Text = Me.serial
    lbl.Name = "lbl" & Me.serial.ToString()
    ' Next two lines are new
    lbl.Tag = txt 
    AddHandler lbl.Click, AddressOf labelClick

    Me.SuspendLayout()
    Me.Controls.Add(lbl)
    Me.Controls.Add(txt)
    Me.ResumeLayout()

    serial  = 1
End Sub

Public Sub labelClick(sender As Control, e As EventHandler)
    Dim txt As TextBox = TryCast(sender.Tag, TextBox)
    If txt IsNot Nothing Then
        AA.Text = txt.Name
    End If
End Sub

While I'm here, you should really turn on Option Strict!

CodePudding user response:

Try using a AddHandler, like this:

 Dim serial As Integer = 1
 Dim AA As String = ""

Public Function addnewline()
    Dim lbl As New System.Windows.Forms.LinkLabel
    Dim txt As New System.Windows.Forms.TextBox

    ' add label
    Me.Controls.Add(lbl)
    lbl.Top = serial * 27
    lbl.Left = 100
    lbl.Text = Me.serial
    lbl.Name = "lbl" & Me.serial
    
    ' add textbox
    Me.Controls.Add(txt)
    txt.Top = serial * 27
    txt.Left = 200
    txt.Height = 500
    txt.Width = 100
    txt.TextAlign = HorizontalAlignment.Center
    txt.Text = "text" & Me.serial
    txt.Name = "txt" & Me.serial

    AddHandler lbl.Click, Sub ()
        AA = txt.Name
        MessageBox.Show(AA)
    End Sub
    serial  = 1

    Return lbl
    Return txt
End Function
  • Related