Click here to Skip to main content
15,998,694 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All.
Pl. Help me. I am working in vb.net. I have create textboxes tb(i) and Ln(i) dynamically in form . Tb has some text and Ln has line no.. now when I click on any Ln i need to get data of TB.. code for your ref. is here with.

What I have tried:

VB
Private Sub Nol_TextChanged(sender As Object, e As EventArgs) Handles Nol.TextChanged

        NOLs = Nol.Text
        Dim XS(5) As String
        XS(0) = "PENDING."
        XS(1) = "APPROVED."
        XS(2) = "WAITING."
        XS(3) = "AUTHORIZED."
        XS(4) = "REGISTERED."
        XS(5) = "TIME-OUT."

        Dim XTC(26) As String
        XTC(0) = "A"
        XTC(1) = "B"
        XTC(2) = "C"
        XTC(3) = "D"
        XTC(4) = "E"
        XTC(6) = "F"
        XTC(7) = "G"
        XTC(8) = "H"
        XTC(9) = "I"
        XTC(10) = "J"
        XTC(11) = "K"
        XTC(12) = "L"
        XTC(13) = "M"
        XTC(14) = "N"
        XTC(15) = "O"
        XTC(16) = "P"
        XTC(17) = "Q"
        XTC(18) = "R"
        XTC(19) = "S"
        XTC(20) = "T"
        XTC(21) = "U"
        XTC(22) = "V"
        XTC(23) = "W"
        XTC(24) = "X"
        XTC(25) = "Y"
        XTC(26) = "Z"

        ' If NOLs = "" Then

        NOLs = Val(Nol.Text) - 1
        '  End If
        With BaseTB
            .Name = "BaseTB"
            If NOLs = 1 Then

                .Text = "GJ" & Int(99 * Rnd()) + 11 & "-XYZ-" & Int(9999 * Rnd()) + 1111 & "^G" & "-^" & Int(52 * Rnd()) + 1
            Else
                .Text = XTC(Rnd) & XTC(Rnd) & ("-") & Int(99 * Rnd()) + 11 & ("-") & XTC(Rnd) & XTC(Rnd) & XTC(Rnd) & ("-") & Int(9999 * Rnd()) + 1111 & (" ") & XS(Rnd)
            End If
            .ForeColor = Color.Black
            .Location = New Point(30, 35)
            .Size = New Point(250, 22)

            .BackColor = Color.Yellow
            .Font = New Font("ARIAL", 12)
        End With
        'BaseTB.Text = "GJ" & Int(99 * Rnd()) + 11 & "-XYZ-" & Int(9999 * Rnd()) + 1111 & "^G" & "-^" & Int(52 * Rnd()) + 1

        Me.Controls.Add(BaseTB)
        For i As Integer = 1 To NOLs ' create text box as nol
            Dim tb As New TextBox
            With tb
                .Name = "tb" & i.ToString
                .Location = New Point(30, 35 + i * 22)
                .Size = New Point(250, 22)
                .BorderStyle = BorderStyle.Fixed3D
                .Text = XTC(Rnd) & XTC(Rnd) & ("-") & Int(99 * Rnd()) + 1 & ("-") & XTC(Rnd) & XTC(Rnd) & XTC(Rnd) & ("-") & Int(9999 * Rnd()) + 111 & ("-") & XS(Rnd)
                .Font = New Font("ARIAL", 12)
                '.Text = "GJ" & Int(99 * Rnd()) + 11 & "-XYZ-" & Int(9999 * Rnd()) + 1111 & "^G" & "-^" & Int(52 * Rnd()) + 1

                ' .Text = "GJ" & Int(99 * Rnd()) + 11 & "-XYZ-" & Int(9999 * Rnd()) + 1111 & ("   ") & XS(Rnd)

                .BackColor = Color.Yellow
            End With
            Me.Controls.Add(tb)
        Next



        With BaseEB
            .Name = "BaseEB"
            .Font = New Font("ARIAL", 12)
            .Text = "A"
            .ForeColor = Color.Black
            .Location = New Point(15, 35)
            .Size = New Point(20, 22)
            .BackColor = Color.White
        End With
        Me.Controls.Add(BaseEB)
        For i As Integer = 1 To NOLs ' create text box as nol
            Dim eb As New TextBox
            With eb
                .Name = "eb" & i.ToString
                .Location = New Point(15, 35 + i * 22)
                .Font = New Font("ARIAL", 12)
                .Text = "A"
                '.TextAlign = HorizontalAlignment.Center
                .Size = New Point(20, 22)
                .ForeColor = Color.Black
                .BackColor = Color.White
            End With
            Me.Controls.Add(eb)
        Next

        With BaseLN
            .Name = "BaseLN"
            .Font = New Font("ARIAL", 12)
            .Text = "1"
            .ForeColor = Color.Black
            .Location = New Point(280, 35)
            .Size = New Point(25, 22)
            .BackColor = Color.Yellow
        End With
        Me.Controls.Add(BaseLN)
        For i As Integer = 1 To NOLs ' create text box as nol
            Dim ln As New TextBox
            With ln
                .Name = "LN" & i.ToString
                .Location = New Point(280, 35 + i * 22)
                .Font = New Font("ARIAL", 12)
                .Text = i + 1
                '.TextAlign = HorizontalAlignment.Center
                .Size = New Point(25, 22)
                .ForeColor = Color.Black
                .BackColor = Color.Yellow
            End With
            Me.Controls.Add(ln)
        Next
    End Sub
Posted
Updated 8-Feb-21 2:38am
v2

The simplest way is to handle the Click event for the ln textbox, and use the sender parameter to find out which one it is.
Then, when you create the textboxes, use the Control.Tag property to hold the reference to the appropriate "matching" TB textbox.
In the CLick handler, you can now fetch the Tag property, cast it to a textbox, and do what you need to with it.

Every Control has a Tag property which is free for your use - it holds an Object so you can put anything that you need to relate to that control in there and retrieve it later when you want it.
 
Share this answer
 
Comments
cws_ap 11-Feb-21 23:39pm    
will you pl. give me small example for click event using sender parameter... will help me lots... Thanks
OriginalGriff 12-Feb-21 4:23am    
You are kidding, right?
You have the sender as a parameter to your event handler method, and all you need is to cast it to the appropriate type ... and you need someone else to code that one line of trivial code for you?
OK ... but you may want to reconsider if development is the right career for you, it gets a whole load harder than this...

Dim tb As TextBox = CType(sender, TextBox)

Here is an implementation of what OriginalGriff stated...

This code has been prepared in LinqPad[^].

VB
Sub Main
	Dim mf As MyForm =  New MyForm()
	mf.Show()
End Sub

' Define other methods and classes here
Public Class MyForm
	Inherits Form
	
	Private lblStatus As Label
	
	Public Sub New()
		Dim BaseTB As New TextBox
        Dim XS As String() = {"PENDING.", "APPROVED.", "WAITING.", "AUTHORIZED.", "REGISTERED.", "TIME-OUT."}
        Dim XTC As String() = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
		Dim NoOfRows As Integer = XS.Length -1
		Dim NoOfCols As Integer = XTC.Length -1

		For rn As Integer = 0 To NoOfRows
			Dim tb As TextBox = New TextBox() With {.Name = $"TextBox{rn}", .Location = New Point(4, (28 * rn) +4), .Size= New Size(90, 24), .Text =XS(rn), .Enabled=False}
			Me.Controls.Add(tb)
			For cn As Integer = 0 To NoOfCols
				Dim lbPoint = New Point(tb.Location.X + tb.Width + (20 *cn) +4 , tb.Location.Y)
				Dim lb As Label = New Label() With {.Name = $"Label{rn}", .Location = lbPoint, .AutoSize = False, .Size = New Size(20,20), _
					.BorderStyle = BorderStyle.Fixed3D, .TextAlign = HorizontalAlignment.Center, .Text =XTC(cn), .Tag = tb}
				AddHandler lb.Click, AddressOf MyLabel_Click
				Me.Controls.Add(lb)
			Next cn
		Next rn
		
		lblStatus = New Label() With {.AutoSize = True, .Location = New Point(4, NoOfRows * 35), .ForeColor = Color.Brown, .Text = "Click on [A-Z] to get status related to it."}
		Me.Controls.Add(lblStatus)
		Me.ShowIcon = False
		Me.MinimizeBox = False
		Me.MaximizeBox = False
		Me.Size = New Size(660,240)
	End Sub
	
	
	Private Sub MyLabel_Click(sender As Object, e As EventArgs)
		Dim lb As Label = DirectCast(sender, Label)
		Dim tb As TextBox = DirectCast(lb.Tag, TextBox)
		lb.Font = New Font("Arial", 9, FontStyle.Bold)
		lb.ForeColor = Color.Red
		lb.BackColor = Color.Yellow
		lblStatus.Text = ($"You've clicked '{lb.Text}' related with {tb.Text}.")
	End Sub
End Class


Above code needs the following references:
* <RuntimeDirectory>\Microsoft.VisualBasic.dll
* <RuntimeDirectory>\System.Windows.Forms.dll
and namespaces:
* Microsoft.VisualBasic
* System.Drawing
* System.Windows.Forms
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900