Focus to dynamic Button on TextBox change using C# and VB.Net in Windows Application

dnnyobi
 
on Sep 19, 2021 10:49 PM
Sample_156566.zip
1068 Views

I want the text on any dynamically created buttons to cast on any focused dynamically created textbox when clicked.

I was able to cast the text of a dynamically created button on a non dynamically created textboxes when focused using the code below.

Private lastFocusedTextBox As TextBox
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim button As Button = CType(sender, Button)
    lastFocusedTextBox.Text += button.Text
End Sub
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
    Dim btn As Button = New Button()
 
    btn.BackColor = Color.DarkGray
    btn.Height = 21
    btn.Width = 35
    btn.Text = TextBox1.Text
    FlowLayoutPanel1.Controls.Add(btn)
    AddHandler btn.Click, AddressOf Me.Button1_Click
    TextBox1.Text = ""
End Sub
Private Sub TextBoxes_Enter(sender As Object, e As EventArgs) Handles TextBox2.Enter, TextBox3.Enter, TextBox4.Enter, TextBox5.Enter, TextBox5.Enter
    lastFocusedTextBox = DirectCast(sender, TextBox)
End Sub

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 20, 2021 06:50 AM
on Sep 20, 2021 11:42 PM

Hi dnnyobi,

Check this example. Now please take its reference and correct your code.

Code

C#

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        Button btn = new Button();
        btn.BackColor = System.Drawing.Color.DarkGray;
        btn.Height = 25;
        btn.Width = 40;
        btn.Text = textBox1.Text;
        flowLayoutPanel1.Controls.Add(btn);

        TextBox txt = new TextBox();
        txt.Width = 120;
        txt.KeyUp += new KeyEventHandler(OnTextBoxKeyUp);
        flowLayoutPanel2.Controls.Add(txt);

        textBox1.Text = "";
    }

    private void OnButtonPaint(object sender, PaintEventArgs e)
    {
        Button btn = (Button)sender;
        ControlPaint.DrawBorder(e.Graphics, btn.ClientRectangle,
                                System.Drawing.Color.Blue, 2, ButtonBorderStyle.Solid,
                                System.Drawing.Color.Blue, 2, ButtonBorderStyle.Solid,
                                System.Drawing.Color.Blue, 2, ButtonBorderStyle.Solid,
                                System.Drawing.Color.Blue, 2, ButtonBorderStyle.Solid);
    }

    private void OnTextBoxKeyUp(object sender, KeyEventArgs e)
    {
        string text = e.KeyCode.ToString().ToLower();
        if (!string.IsNullOrEmpty(text))
        {
            foreach (Control control in flowLayoutPanel1.Controls)
            {
                if ((control is Button) && (control.Text.ToLower() == text))
                {
                    (control as Button).Paint += new PaintEventHandler(OnButtonPaint);
                    (control as Button).Refresh();
                }
                else if ((control is Button))
                {
                    (control as Button).Paint -= new PaintEventHandler(OnButtonPaint);
                    (control as Button).Refresh();
                }
            }
        }
    }
}

VB.Net

Public Class Form1
    Private Sub OnAdd(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim btn As Button = New Button()
        btn.BackColor = System.Drawing.Color.DarkGray
        btn.Height = 25
        btn.Width = 40
        btn.Text = textBox1.Text
        flowLayoutPanel1.Controls.Add(btn)

        Dim txt As TextBox = New TextBox()
        txt.Width = 120
        AddHandler(txt).KeyUp, AddressOf OnTextBoxKeyUp
        flowLayoutPanel2.Controls.Add(txt)

        textBox1.Text = ""
    End Sub

    Private Sub OnButtonPaint(ByVal sender As Object, ByVal e As PaintEventArgs)
        Dim btn As Button = CType(sender, Button)
        ControlPaint.DrawBorder(e.Graphics, btn.ClientRectangle,
                                System.Drawing.Color.Blue, 2, ButtonBorderStyle.Solid,
                                System.Drawing.Color.Blue, 2, ButtonBorderStyle.Solid,
                                System.Drawing.Color.Blue, 2, ButtonBorderStyle.Solid,
                                System.Drawing.Color.Blue, 2, ButtonBorderStyle.Solid)
    End Sub

    Private Sub OnTextBoxKeyUp(sender As Object, e As KeyEventArgs)
        Dim text As String = e.KeyCode.ToString().ToLower()
        If Not String.IsNullOrEmpty(text) Then
            For Each control As Control In flowLayoutPanel1.Controls
                If (TypeOf control Is Button) AndAlso (control.Text.ToLower() = text) Then
                    AddHandler(TryCast(control, Button)).Paint, AddressOf OnButtonPaint
                    TryCast(control, Button).Refresh()
                ElseIf (TypeOf control Is Button) Then
                    RemoveHandler(TryCast(control, Button)).Paint, AddressOf OnButtonPaint
                    TryCast(control, Button).Refresh()
                End If
            Next
        End If
    End Sub
End Class

Screenshot