Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Returning a Value from a Form

Posted on 2006-04-13
25
Medium Priority
?
199 Views
Last Modified: 2010-04-23
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!!
0
Comment
Question by:aborg85
  • 9
  • 8
  • 7
  • +1
25 Comments
 
LVL 4

Expert Comment

by:broadbent
ID: 16444694
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
 
LVL 4

Expert Comment

by:sr101880
ID: 16444767
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
 

Expert Comment

by:Vis781
ID: 16445141
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:broadbent
ID: 16445279
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
 

Author Comment

by:aborg85
ID: 16446001
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
 
LVL 4

Expert Comment

by:broadbent
ID: 16446122
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
 

Author Comment

by:aborg85
ID: 16446328
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
 
LVL 4

Expert Comment

by:broadbent
ID: 16446363
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
 
LVL 4

Expert Comment

by:broadbent
ID: 16446424
It would be really useful to have all the code so I can simply cut and paste and create all the forms.
0
 

Author Comment

by:aborg85
ID: 16446446
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
 

Author Comment

by:aborg85
ID: 16446456
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
 
LVL 4

Expert Comment

by:broadbent
ID: 16446488
And to really help me
What controls are on what form
0
 

Author Comment

by:aborg85
ID: 16446590
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
 
LVL 4

Expert Comment

by:sr101880
ID: 16446672
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
 

Author Comment

by:aborg85
ID: 16446886
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
 
LVL 4

Expert Comment

by:sr101880
ID: 16447013
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
 
LVL 4

Expert Comment

by:sr101880
ID: 16447039
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
 
LVL 4

Expert Comment

by:broadbent
ID: 16447048
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
 
LVL 4

Expert Comment

by:sr101880
ID: 16447050
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
 

Author Comment

by:aborg85
ID: 16447244
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
 
LVL 4

Expert Comment

by:broadbent
ID: 16447515
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
 
LVL 4

Assisted Solution

by:broadbent
broadbent earned 750 total points
ID: 16447520
oops
should be
RaiseEvent MyEvent(my arguments,,,,)
0
 

Author Comment

by:aborg85
ID: 16447770
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
 
LVL 4

Expert Comment

by:sr101880
ID: 16447905
Sorry to make things confusing.  Each one will work.  You should use the one that works best for you.  
0
 
LVL 4

Accepted Solution

by:
sr101880 earned 750 total points
ID: 16447933
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question