Hi aginell4life,
Refer below code.
C#
private static string SelectedTextBox { get; set; }
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ItemsName");
dt.Rows.Add("Hat");
dt.Rows.Add("Cap");
dt.Rows.Add("Ball");
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox tb = new TextBox();
tb.Name = "text" + dt.Rows[i][0];
tb.Click += new EventHandler(this.OnTextBoxClick);
flowLayoutPanel1.Controls.Add(tb);
Button btn = new Button();
btn.Name = "btn" + dt.Rows[i][0];
btn.Tag = dt.Rows[i][0];
btn.Text = dt.Rows[i][0].ToString();
btn.BackColor = Color.DarkGray;
btn.Height = 21;
btn.Width = 35;
btn.Click += new EventHandler(this.btnSelect_Click);
flowLayoutPanel1.Controls.Add(btn);
flowLayoutPanel1.Controls.Add(new Label());
}
}
private void btnSelect_Click(object sender, EventArgs e)
{
foreach (TextBox tb in flowLayoutPanel1.Controls.OfType<TextBox>())
{
if (tb.Name == SelectedTextBox)
{
tb.Text = (sender as Button).Text;
break;
}
}
}
private void OnTextBoxClick(object sender, EventArgs e)
{
SelectedTextBox = (sender as TextBox).Name;
}
VB.Net
Private Shared Property SelectedTextBox As String
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim dt As DataTable = New DataTable()
dt.Columns.Add("ItemsName")
dt.Rows.Add("Hat")
dt.Rows.Add("Cap")
dt.Rows.Add("Ball")
For i As Integer = 0 To dt.Rows.Count - 1
Dim tb As TextBox = New TextBox()
tb.Name = "text" & dt.Rows(i)(0)
AddHandler tb.Click, AddressOf Me.OnTextBoxClick
flowLayoutPanel1.Controls.Add(tb)
Dim btn As Button = New Button()
btn.Name = "btn" & dt.Rows(i)(0)
btn.Tag = dt.Rows(i)(0)
btn.Text = dt.Rows(i)(0).ToString()
btn.BackColor = Color.DarkGray
btn.Height = 21
btn.Width = 35
AddHandler btn.Click, AddressOf Me.btnSelect_Click
flowLayoutPanel1.Controls.Add(btn)
flowLayoutPanel1.Controls.Add(New Label())
Next
End Sub
Private Sub btnSelect_Click(ByVal sender As Object, ByVal e As EventArgs)
For Each tb As TextBox In flowLayoutPanel1.Controls.OfType(Of TextBox)()
If tb.Name = SelectedTextBox Then
tb.Text = (TryCast(sender, Button)).Text
Exit For
End If
Next
End Sub
Private Sub OnTextBoxClick(ByVal sender As Object, ByVal e As EventArgs)
SelectedTextBox = (TryCast(sender, TextBox)).Name
End Sub
Screenshot