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