Feb 4, 2014

Changing Form Data From Another Form

This Example Shows How To Access Data Of Form From Another Form.
By Using This Method You Can Call A Method Or You Can Set Text To TextBox Also.
Normally After Creating A Sub Class You May Have To Access The Parent Class Text Field In Order To Set Or Get The Text To It.Following Example Shows How To Do This. 

In My Example I'm Assuming I've Two Forms Called MyForm1 and MyForm2.
MyForm1 Opens The MyForm2.
MyForm1 Have Two Text Fields Called txtInvoiceNo, txtFullName

First Of All Create The Two Forms As Above.
Then We Have To Make The Form1 As Parent Class In Form2.
In Order To Do That We Have To Pass The Form2 Object That Created In Form1(Simply The Form2 Object To Show The Form2)


In Your Form1 You Can Open The Form2 By Clicking A Button Or Any Other Event.Use Following Codes To Do That.Paste The Following Codes In The Event You Are Going To Open The Form2 in Form1.


MyForm2 formMyForm2 = new MyForm2();
formMyForm2.Show();
formMyForm2.RegisterParent(this);//This Code Unit Help To Access This Form From A Object


Add The Following Code Lines Inside The Form2.This Has A MyForm1 Class Object and A Method To Get The Parent Class Object.


private MyForm1 _parent;

internal void RegisterParent(MyForm1 form)
{
    this._parent = form;
}


Now You Can Set Your Values From MyForm2 to MyForm1
I'm Using A Button Click Example To Show It.

private void btnAdd_Click(object sender, EventArgs e)
{
    //Adding Values To MyForm1
_parent.txtInvoiceNo.Text = "Your Set Code Goes Here";
_parent.txtFullName.Text = "Your Set Code Goes Here";
}

No comments:

JWT Token Decode Using Jquery

When it come to authentication we use many mechanism. Ones the user authenticated we must keep these details somewhere safe. So we can share...