Returning a Value from a Form

Hey Guys,

Hope you can help me,

Right, basicly im doing a Check In system, whereby the check in agent enters a PNR number, and all tickets under that PNR will  come up using data grid view. Now, the check in agent select one of tickets and clicks a button to assign a seat. A new form will pop up open with a view of the seats from the top and the user basicly select a a seat( made up of buttons) . The prob i have is how to return the selected seat back to the original form.

As simple as possible pls guys

THANKS!!
aborg85Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

broadbentCommented:
If both forms are part of tjhe same application, then why not create a Global variable, say
Friend SelectedTicket as Integer

and use this variable in both forms.
0
sr101880Commented:
Check out this solution:  http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21787817.html

It has a couple ways to get the job done.  I think that the accepted answer would be the best way to go for you.  If you need any help adapting it for your project let me know.
0
Vis781Commented:
I would just add a property to the popup form, readonly will do.Add the information to the property after the button is clicked, Show the form using showdialog then on closing the form retrieve the value of the property before disposing of the form.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

broadbentCommented:
I had a look at sr101880's comment, and it ain't half complicated.
Keep it simple is what I say.

Thought again though.

Public Class Form1
    Dim WithEvents Frm As Form2
 
    Private Sub Frm_ButtonClicked(ByVal Value As Integer) Handles Frm.ButtonClicked
        DataGridView1.CurrentCell.Value = Value
    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        Frm.ShowDialog()
    End Sub
End Class

Public Class Form2
    Friend Event ButtonClicked(ByVal Value As Integer)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        RaiseEvent ButtonClicked(1)
    End Sub
End Class
0
aborg85Author Commented:
Thanks for your reply all of you.

BroadBent,

I trying your out,

once the user clicks on the button, how can i assign a labellink used in form 1 to button clicked in form 2.

This what is what i ve done:

Form 2:
Public Class AssignSeat

    Friend Event ButtonClicked(ByVal Value As String)

 Friend Sub A1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A1.Click
        RaiseEvent ButtonClicked("A1")
  End Sub

Form 1
Public Class ViewTicketsForCheckin

    Dim WithEvents Frm As AssignSeat
    Dim flightNo As String
    Dim Flightdate As String

Private Sub AssSeatLink_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles AssSeatLink.LinkClicked
        Dim AssignSeatForm As New AssignSeat
        AssignSeatForm.AssignSeat_Load(flightNo, Flightdate) <--- forget to tell you that i need pass these values to form 2 too
        Frm.ShowDialog()
End Sub

How do i code that i want Ass_SeatLink.text = (value from form 2)
0
broadbentCommented:
I'm getting myslef confused with what form is where.

However, you want to pass flightno and flightdate from one form to another.

In the form you wish to receive this data, you will have two variables I presume.
Private flightno as integer, flightdate as date

So have a public method in this form
Friend (because we don't wnat it visible outside app) SetFlight(byval fno as integer, byval fldate as Data)
flightno = fno
flightdate = fldate
End Sub

or create 2 properties.
0
aborg85Author Commented:
The passing of flightDate and FlightNo is already done... as you can see:

Friend Sub AssignSeat_Load(ByVal FlightNo As String, ByVal FlightDate As String)

But how do i call the value in form 2 from form 1?

FORM 1: where tickets come up on data grid, the user selects a ticket, the data is then transfered to textboxs. Of which there is a linklabel which will open FORM 2, this is where the seat will be shown made up of buttons.

FORM 1
Public Class ViewTicketsForCheckin

    Dim WithEvents Frm As AssignSeat
    Dim flightNo As String
    Dim Flightdate As String

Private Sub ProcessCheckIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcessCheckIn.Click

        Dim SelectedPNR As String
        Dim TicketNo As Long
        Dim Name As String
        Dim Surname As String

        SelectedPNR = Me.CheckInTicketList.CurrentRow.Cells(Me.PNRDataGridViewTextBoxColumn.Index).Value()
        TicketNo = Me.CheckInTicketList.CurrentRow.Cells(Me.TicketNoDataGridViewTextBoxColumn.Index).Value()
        Name = Me.CheckInTicketList.CurrentRow.Cells(Me.NameDataGridViewTextBoxColumn.Index).Value()
        Surname = Me.CheckInTicketList.CurrentRow.Cells(Me.SurnameDataGridViewTextBoxColumn.Index).Value()
        flightNo = Me.CheckInTicketList.CurrentRow.Cells(Me.FlightNoDataGridViewTextBoxColumn.Index).Value()
        Flightdate = Me.CheckInTicketList.CurrentRow.Cells(Me.FlightDateDataGridViewTextBoxColumn.Index).Value()
        Me.CheckInBox.Visible = True
        Me.TicketNoTB.Text = TicketNo
        Me.NameTB.Text = Name
        Me.SurnameTB.Text = Surname

    End Sub
Private Sub AssSeatLink_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles AssSeatLink.LinkClicked
        Dim AssignSeatForm As New AssignSeat
        AssignSeatForm.AssignSeat_Load(flightNo, Flightdate)
        Frm.ShowDialog()
        Me.AssSeatLink.Text = ??????

    End Sub

Form 2
Public Class AssignSeat
    Dim tempSeat As String
    Friend Event ButtonClicked(ByVal Value As String)

Friend Sub AssignSeat_Load(ByVal FlightNo As String, ByVal FlightDate As String)
        'TODO: This line of code loads data into the 'SeatDataSet.Seat' table. You can move, or remove it, as needed.
        Me.SeatTableAdapter.Fill(Me.SeatDataSet.Seat)
        Me.FlightDateLink.Text = (CType(FlightDate, Date))
        Me.FLightNoLink.Text = FlightNo

        Try
            Me.SeatTableAdapter.FillBy(Me.SeatDataSet.Seat, FLightNoLink.Text, New System.Nullable(Of Date)(CType(FlightDateLink.Text, Date)))
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
    End Sub

Friend Sub A1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A1.Click
        RaiseEvent ButtonClicked("A1")
    End Sub
0
broadbentCommented:
Please forgive me, but I don't have VS loaded and so I'm not testing the code.

Friend Sub AssignSeat_Load(ByVal FlightNo As String, ByVal FlightDate As String)
I assumes AssignSeat was a control and this was the load event, in which case the arguments can't be used.

I'll load up your code and get back to you
0
broadbentCommented:
It would be really useful to have all the code so I can simply cut and paste and create all the forms.
0
aborg85Author Commented:
Public Class AssignSeat
    Dim tempSeat As String
    Friend Event ButtonClicked(ByVal Value As String)

    Friend Sub AssignSeat_Load(ByVal FlightNo As String, ByVal FlightDate As String)
        'TODO: This line of code loads data into the 'SeatDataSet.Seat' table. You can move, or remove it, as needed.
        Me.SeatTableAdapter.Fill(Me.SeatDataSet.Seat)
        Me.FlightDateLink.Text = (CType(FlightDate, Date))
        Me.FLightNoLink.Text = FlightNo

        Try
            Me.SeatTableAdapter.FillBy(Me.SeatDataSet.Seat, FLightNoLink.Text, New System.Nullable(Of Date)(CType(FlightDateLink.Text, Date)))
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub AssignSeat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim I As Integer
        For I = 0 To (Me.BookedSeatlist.RowCount - 1)
            tempSeat = Me.BookedSeatlist.Rows(I).Cells(Me.SeatNoDataGridViewTextBoxColumn.Index).Value()

            If tempSeat.Contains("A1") = True Then Me.A1.Visible = False
            If tempSeat.Contains("A2") = True Then Me.A2.Visible = False
            If tempSeat.Contains("A3") = True Then Me.A3.Visible = False
            If tempSeat.Contains("A4") = True Then Me.A4.Visible = False
            If tempSeat.Contains("A5") = True Then Me.A5.Visible = False
            If tempSeat.Contains("A6") = True Then Me.A6.Visible = False

            If tempSeat.Contains("B1") = True Then Me.B1.Visible = False
            If tempSeat.Contains("B2") = True Then Me.B2.Visible = False
            If tempSeat.Contains("B3") = True Then Me.B3.Visible = False
            If tempSeat.Contains("B4") = True Then Me.B4.Visible = False
            If tempSeat.Contains("B5") = True Then Me.B5.Visible = False
            If tempSeat.Contains("B6") = True Then Me.B6.Visible = False

            If tempSeat.Contains("C1") = True Then Me.C1.Visible = False
            If tempSeat.Contains("C2") = True Then Me.C2.Visible = False
            If tempSeat.Contains("C3") = True Then Me.C3.Visible = False
            If tempSeat.Contains("C4") = True Then Me.C4.Visible = False
            If tempSeat.Contains("C5") = True Then Me.C5.Visible = False
            If tempSeat.Contains("C6") = True Then Me.C6.Visible = False

            If tempSeat.Contains("D1") = True Then Me.D1.Visible = False
            If tempSeat.Contains("D2") = True Then Me.D2.Visible = False
            If tempSeat.Contains("D3") = True Then Me.D3.Visible = False
            If tempSeat.Contains("D4") = True Then Me.D4.Visible = False
            If tempSeat.Contains("D5") = True Then Me.D5.Visible = False
            If tempSeat.Contains("D6") = True Then Me.D6.Visible = False

            If tempSeat.Contains("E1") = True Then Me.E1.Visible = False
            If tempSeat.Contains("E2") = True Then Me.E2.Visible = False
            If tempSeat.Contains("E3") = True Then Me.E3.Visible = False
            If tempSeat.Contains("E4") = True Then Me.E4.Visible = False
            If tempSeat.Contains("E5") = True Then Me.E5.Visible = False
            If tempSeat.Contains("E6") = True Then Me.E6.Visible = False

            If tempSeat.Contains("F1") = True Then Me.F1.Visible = False
            If tempSeat.Contains("F2") = True Then Me.F2.Visible = False
            If tempSeat.Contains("F3") = True Then Me.F3.Visible = False
            If tempSeat.Contains("F4") = True Then Me.F4.Visible = False
            If tempSeat.Contains("F5") = True Then Me.F5.Visible = False
            If tempSeat.Contains("F6") = True Then Me.F6.Visible = False

            If tempSeat.Contains("G1") = True Then Me.G1.Visible = False
            If tempSeat.Contains("G2") = True Then Me.G1.Visible = False
            If tempSeat.Contains("G3") = True Then Me.G3.Visible = False
            If tempSeat.Contains("G4") = True Then Me.G4.Visible = False
            If tempSeat.Contains("G5") = True Then Me.G5.Visible = False
            If tempSeat.Contains("G6") = True Then Me.G6.Visible = False
        Next I
    End Sub

    Friend Sub A1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A1.Click
        RaiseEvent ButtonClicked("A1")
    End Sub
End Class
0
aborg85Author Commented:
This is form 1, the previous is form 2


Public Class ViewTicketsForCheckin

    Dim WithEvents Frm As AssignSeat
    Dim flightNo As String
    Dim Flightdate As String

    Friend Sub ViewTicketsForCheckin_Load(ByVal PNR As String)
        'TODO: This line of code loads data into the 'CheckInDataSet.CheckIn' table. You can move, or remove it, as needed.
        Me.CheckInTableAdapter.Fill(Me.CheckInDataSet.CheckIn)

        Me.PNRtextbox.Text = PNR
        Me.TodayDate.Value = Date.Today

        Try
            Me.BookingTableAdapter.FillBy(Me.ReservationDataSet.Booking, Me.PNRtextbox.Text)
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
        Try
            Me.TicketingTableAdapter.FillByCI(Me.ReservationDataSet.Ticketing, Me.PNRtextbox.Text, New System.Nullable(Of Date)(CType(TodayDate.Value, Date)))
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

        Try
            Me.CheckInTableAdapter.FillBy(Me.CheckInDataSet.CheckIn, PNRtextbox.Text)
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

        If Me.CheckInListGrid.RowCount = 0 Then Me.CheckInListGrid.Visible = False

    End Sub

    Private Sub Searchbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Searchbtn.Click
        Try
            Me.TicketingTableAdapter.FillByCI(Me.ReservationDataSet.Ticketing, Me.PNRtextbox.Text, New System.Nullable(Of Date)(CType(TodayDate.Value, Date)))
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

        Try
            Me.CheckInTableAdapter.FillBy(Me.CheckInDataSet.CheckIn, PNRtextbox.Text)
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

        If Me.CheckInListGrid.RowCount = 0 Then Me.CheckInListGrid.Visible = False

    End Sub

    Private Sub ProcessCheckIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProcessCheckIn.Click

        Dim SelectedPNR As String
        Dim TicketNo As Long
        Dim Name As String
        Dim Surname As String

        SelectedPNR = Me.CheckInTicketList.CurrentRow.Cells(Me.PNRDataGridViewTextBoxColumn.Index).Value()
        TicketNo = Me.CheckInTicketList.CurrentRow.Cells(Me.TicketNoDataGridViewTextBoxColumn.Index).Value()
        Name = Me.CheckInTicketList.CurrentRow.Cells(Me.NameDataGridViewTextBoxColumn.Index).Value()
        Surname = Me.CheckInTicketList.CurrentRow.Cells(Me.SurnameDataGridViewTextBoxColumn.Index).Value()
        flightNo = Me.CheckInTicketList.CurrentRow.Cells(Me.FlightNoDataGridViewTextBoxColumn.Index).Value()
        Flightdate = Me.CheckInTicketList.CurrentRow.Cells(Me.FlightDateDataGridViewTextBoxColumn.Index).Value()
        Me.CheckInBox.Visible = True
        Me.TicketNoTB.Text = TicketNo
        Me.NameTB.Text = Name
        Me.SurnameTB.Text = Surname

    End Sub

    Private Sub AssSeatLink_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles AssSeatLink.LinkClicked
        Dim AssignSeatForm As New AssignSeat
        AssignSeatForm.AssignSeat_Load(flightNo, Flightdate)
        Frm.ShowDialog()
        Me.AssSeatLink.Text = Me.Frm.Text

    End Sub
End Class
0
broadbentCommented:
And to really help me
What controls are on what form
0
aborg85Author Commented:
IN from 2, ViewTicketsFor CheckIn,

A data grid view which when selected and pressing the process check in, textboxes appear and One LinkLabel. So there all textboxes, one data grid and one linklabel(which pops up form2) this is where the seat no needs to be shown.

in form 2, two linklabel, both just showing the flightdata and the flight no, the rest are small textnboxes used to represent the seats on the plane.
0
sr101880Commented:
A quick way to get your form two info from form one is:

In form two setup a property for each piece of info you wish to pass.
'-----------------------------------------------------------------------------------------
Dim mstrItem

 ReadOnly Property Item() As String

        Get

            Return mstrItem ' Set the value of this var to info you want to share

        End Get

    End Property

'----------------------------------------------------------------------------------------

Then in your button click even simply set the value for the property

Friend Sub A1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A1.Click
        mstrItem = "A1"
End Sub

'-----------------------------------------------------------------------
to call from form one use:

txtOne.text = frmtwo.Item

Let me know if you have any questions.
0
aborg85Author Commented:
Sr101880,


I just tried out the way you suggested. It seems easy however i have a problem, since i need to wait for the user to actually select the box(seat), the answer is obviously is coming to empty or null. How can i code to tell the program to wait before fetching the value until a seat is selected?


Private Sub AssSeatLink_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles AssSeatLink.LinkClicked
        Dim AssignSeatForm As New AssignSeat
        AssignSeatForm.AssignSeat_Load(flightNo, Flightdate)
        AssignSeatForm.Show()
        Me.AssSeatLink.Text = AssignSeatForm.Item


THANKS once again guys
0
sr101880Commented:
In that case you should be able to change your form two sub to :


Friend Sub A1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A1.Click

    ViewTicketsForCheckin.AssSeatLink.text = "A1"

End Sub


Then remove the line after your form show from your link clicked sub in form one.

Let me know how it goes.
0
sr101880Commented:
You might need to create a new object for form one like so first:

Friend Sub A1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A1.Click

    dim myViewTicketsForCheckin as ViewTicketsForCheckin

    myViewTicketsForCheckin.AssSeatLink.text = "A1"

End Sub
0
broadbentCommented:
You'll need to raiseevents when you click on something.

So user selects box(seat) - raise event SeatSelected(byval SelectedSeat as Integer)

in other form your Dim withevents will create an event SeatSelected.
Go to your Class name box at the top left and scroll down to Form1 (whatever)
In Method Name box at top right, scroll down to SeatSelected and click
VS will produce the Private Sub and End Sub statements. Fill in your code between so that your datagrid gets updated.
0
sr101880Commented:
sorry forgot the "new"

You might need to create a new object for form one like so first:

Friend Sub A1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A1.Click

    dim myViewTicketsForCheckin as new ViewTicketsForCheckin  '<------------------------------------------------- Here!

    myViewTicketsForCheckin.AssSeatLink.text = "A1"

End Sub
0
aborg85Author Commented:
hi again i ve understood this:


You might need to create a new object for form one like so first:

Friend Sub A1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A1.Click

    dim myViewTicketsForCheckin as new ViewTicketsForCheckin  '<------------------------------------------------- Here!

    myViewTicketsForCheckin.AssSeatLink.text = "A1"

End Sub

could you explain to me again of the raise events... never used them before
0
broadbentCommented:
You can create Events by using the following code.
Public Event MyEvent(my arguments,,,,)

Public means that any other application can reference this application and listen out for the event to be raised.
If you want the event to be only heard in your application, then use Friend.

To raise an event, which must be defined, use
RaiseEvent(my arguments,,,,,)

For another form say to hear the event, you can either
Dim WithEvents Form1 (which has the Event) or
AddHandler MyObject.MyEvent, Addressof EventHandler (written Sub Eventhandler in this form)

When the object raises an event, then the listening form, object will execute the code in the eventhandler routine.

There is a lot of info on all this in your Help file.
0
broadbentCommented:
oops
should be
RaiseEvent MyEvent(my arguments,,,,)
0
aborg85Author Commented:
but now im getting confused....

should i use this:

Friend Sub A1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A1.Click

    dim myViewTicketsForCheckin as new ViewTicketsForCheckin

    myViewTicketsForCheckin.AssSeatLink.text = "A1"

End Sub

or this

Friend Sub A1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles A1.Click

    RaiseEvent ButtonClicked("A1")
End Sub
???
0
sr101880Commented:
Sorry to make things confusing.  Each one will work.  You should use the one that works best for you.  
0
sr101880Commented:
The solution that I gave you is easy to setup for this situation that is why I recommended it.  Let me know how you make out or if you have any questions.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.