How to Pass Value from One Form to another Form

I think it’s time again to teach you on the features that can be found on my program like Hotel Reservation System (VB.NET).

A while ago I received a comment on how to pass value from one form to another form. Although I have already done this in almost all of my program but I know that some of you miss the opportunity to analyze it due to the complexity of the program.

Without further ado, let us take the example from Hotel Reservation System (VB.NET).

In frmRoomWindow form you can see the following code:

  1.         With frmCheckIn
  2.             .State = modGlobal.FormState.adStateEditMode 'Line two
  3.             .RoomNumber = intRoomNumber ' Line three
  4.  
  5.             .ShowDialog()
  6.  
  7.             Call FillList()
  8.         End With

The line two and three will pass a value to State and RoomNumber variable. In order to pass this value the receiving form which is frmCheckIn needs to define these variables first, as shown below:

  1. Friend Class frmCheckIn
  2.     Inherits System.Windows.Forms.Form
  3.  
  4.     Dim daTransactions As New OleDbDataAdapter()
  5.     Dim dsTransactions As New DataSet()
  6.     Dim cnHotel As OleDbConnection
  7.  
  8.     Public RoomNumber As Integer 'Line eight
  9.     Public State As modGlobal.FormState 'Line nine
  10.     Public FolioNumber As String
  11.     Public AmountPaid As Decimal
  12.     Public OtherCharges As Decimal
  13.     Public blnChangeRoom As Boolean
  14.  
  15.     Private IsInitializing As Boolean

As you can see in the line eight and nine RoomNumber and State is define here as public variable. Declaring this variable as public is very important to receive a value from other form, or else it cannot receive any value.

As it is already been defined at the module level of the form then you can call it anywhere in your code like in the Form_Load event. Example:

  1. Private Sub frmCheckIn_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         If State = modGlobal.FormState.adStateAddMode Then
  3.                 txtRoomNumber.Text = CStr(RoomNumber)
  4.                 dtpDateIn.Value = Today
  5.                 dtpDateOut.Value = DateTime.Now.AddDays(1)
  6. End Sub

I hope this helps.

Comments

thx u

sir im a fan of your work and i really appreciate all of the works here. i would like to know how to backup and restore mysql database using vb.net. im using visual studio 2008. tnx for any help!

How are u doing bro? Am good. You are doing a good thing helping us out with programming and databases.Me and my friends really appreciate your help. I'm currently developing a student attendance management system. Please if you have an idea how to go about it, i would appreciate it bro. Thanx a lot Bless u!

For easy coding, it is ok to use public variables. But if we are concerned about proper program design, we must remember that in .NET, a form is treated as a class. Therefore, having public variables breaks the encapsulation principle of OOP. To solve this issue, we may implement a property to get or set value of a variable. for Example:
  1.  public partial class CheckinForm: Form
  2.  {
  3.         int roomNumber;
  4.         string folioNumber;
  5.  
  6.         public frmSplash()
  7.         {
  8.             InitializeComponent();
  9.         }
  10.  
  11.         public int RoomNumber
  12.         {
  13.             get { return roomNumber;}
  14.             set { roomNumbr = value;}
  15.         }
  16.  
  17.         public string FolioNumber
  18.         {
  19.             get { return folioNumber; }
  20.             set { folioNumber = value; }
  21.         }
  22. }
So if you are to pass a value from a form to another form, you will use something like this:
  1. CheckInForm checkInForm = new CheckInForm();
  2. checkInForm.RoomNumber = <whatever value>;
  3. checkInForm.FolioNumber = <whatever value>;

passing a value form one form to another form1 using simple codes. .

how to embed a access database file on a visual basic 6.0 program?

Add new comment