Hi Vasanth057,
First you need to create a class file to store which button is clicked. Then assign the value to the class that worked like viewstate in asp.net.
ButtonClicked.cs
public static class ButtonClicked
{
public static string buttonClicked;
}
ButtonClicked.vb
Public NotInheritable Class ButtonClicked
Private Sub New()
End Sub
Public Shared buttonClicked As String
End Class
Form1.cs
public Form1()
{
InitializeComponent();
}
private void btnClicked(object sender, EventArgs e)
{
ButtonClicked.buttonClicked = (sender as Button).Text;
Form2 form2 = new Form2();
form2.Show();
}
Form1.vb
Public Sub New()
InitializeComponent()
End Sub
Private Sub btnClicked(sender As Object, e As EventArgs)
ButtonClicked.buttonClicked = TryCast(sender, Button).Text
Dim form2 As New Form2()
form2.Show()
End Sub
Form2.cs
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(ButtonClicked.buttonClicked))
{
lblMessage.Text = ButtonClicked.buttonClicked + " Button Clicked";
}
}
Form2.vb
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs)
If Not String.IsNullOrEmpty(ButtonClicked.buttonClicked) Then
lblMessage.Text = ButtonClicked.buttonClicked + " Button Clicked"
End If
End Sub
When you clicked on button in form1 you will get the button clicked text in the label in form2 like below.
OutPut
First Button Clicked
or
Second Button Clicked