Passing an address from one form to another

claire_bicknell

Well-known member
Joined
Dec 10, 2008
Messages
49
Programming Experience
Beginner
I have an interactive map as illustrated in screenshot 1. The user is expected to select a point on the map and then depending on the region clicked (i.e Manchester), on a second form (see screenshot 2) the centreName and Postcode should be automatically entered into textboxes on the second form ready for the user to click "search for directions".

This is my full code for form1 (interactive map):

HTML:
Imports System
Imports System.Data.Sql
Imports System.Data.SqlClient

Public Class Form1
    Private places As New Dictionary(Of String, Rectangle)
    Const strConnection As String = "Data Source=(local);Initial Catalog=ShoppingCentre;Integrated Security=True"
    Dim con As New SqlConnection(strConnection)
    Dim com As New SqlCommand("Select * from dbo.Centre", con)

    Dim MyDataSet As New DataSet()
    Dim CentreTableAdapter As New SqlDataAdapter()


    Private Sub LoadPlaces()
        Me.CentreTableAdapter.Fill(MyDataSet, "Centre")
        For Each row As DataRow In MyDataSet.Tables("Centre").Rows

            places.Add(row.Item("CentreName") & vbCrLf & ("Address: ") & row.Item("Address") & (", ") & row.Item("Postcode") & vbCrLf & ("Phone: ") & row.Item("TelephoneNumber") & vbCrLf & ("Opening Times: ") & row.Item("OpeningClosingTimes"), New Rectangle(row.Item("X1"), row.Item("Y1"), row.Item("X2"), row.Item("Y2")))


        Next

    End Sub

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

        CentreTableAdapter.SelectCommand = com
        LoadPlaces()

    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        For Each hotspot As Rectangle In places.Values
            e.Graphics.DrawEllipse(Pens.Red, hotspot)
        Next

    End Sub
    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        For Each place As String In places.Keys
            If places(place).Contains(e.Location) Then


                For Each row As DataRow In MyDataSet.Tables("Centre").Rows

                    Directions.TextBox1.Text = row.Item("CentreName")
                    Directions.TextBox2.Text = row.Item("Postcode")


                    Stores.Show()
                    Me.Hide()
                Next
                Exit For
            End If
        Next
    End Sub
    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        Dim current As String = Me.ToolTip1.GetToolTip(Me.PictureBox1)
        For Each place As String In places.Keys
            If places(place).Contains(e.Location) Then
                If current <> place Then

                    Me.ToolTip1.Show(place, Me.PictureBox1)
                    Exit For
                End If
            End If
        Next
    End Sub
    Private Sub StoresToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StoresToolStripMenuItem.Click

        Stores.Show()
        Me.Hide()

    End Sub

    Private Sub FloorPlanToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FloorplanToolStripMenuItem.Click

        Floor_Plan.Show()
        Me.Hide()

    End Sub

    Private Sub DirectionsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DirectionsToolStripMenuItem.Click

        Directions.Show()
        Me.Hide()

    End Sub

    Private Sub ContactUsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContactUsToolStripMenuItem.Click

        Contact_Us.Show()
        Me.Hide()

    End Sub

    Private Sub LeaveYourCommentsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LeaveYourCommentsToolStripMenuItem.Click

        Leave_your_Comments.Show()
        Me.Hide()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Me.Close()

    End Sub

    Private Sub AdminToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AdminToolStripMenuItem.Click

        Admin.Show()
        Me.Hide()

    End Sub
End Class

This is the fragment of code I am working on to try and get this working:

HTML:
 Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        For Each place As String In places.Keys
            If places(place).Contains(e.Location) Then


                For Each row As DataRow In MyDataSet.Tables("Centre").Rows

                    Directions.TextBox1.Text = row.Item("CentreName")
                    Directions.TextBox2.Text = row.Item("Postcode")


                    Stores.Show()
                    Me.Hide()
                Next
                Exit For
            End If
        Next
    End Sub

Currently when i select a region on the map... it returns the same result (centreName and Postcode for Belfast) no matter what region is clicked. Obviously the centreName and Postcode should differ depending on the region clicked.

I hope someone can help.

Kind Regards
 

Attachments

  • interactive map.jpg
    interactive map.jpg
    122.4 KB · Views: 27
  • Directions.jpg
    Directions.jpg
    115.2 KB · Views: 28
You're looping through each row in the datatable without searching for the Centre you clicked on.

VB.NET:
	Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
		For Each place As String In places.Keys
			If places(place).Contains(e.Location) Then

				'Split the place on the first vbCrLf and return the Centre Name (0th element)
				Dim centre As String = place.Split(Environment.NewLine)(0)

				'Select a 1 dimensional array of System.Data.DataRows from your Centre DataTable
				Dim row() As DataRow = MyDataSet.Tables("Centre").Select("CentreName = '" & centre & "'")

				'Since you won't have duplicate Centre Names get the items from row 0.
				Directions.TextBox1.Text = row(0).Item("CentreName")
				Directions.TextBox2.Text = row(0).Item("Postcode")

				Directions.Show()
				Me.Hide()

			End If
		Next
	End Sub
 
Hi Matt,

I have tested the code that you produced in your last post and unfortunately I get an error message with this line:

HTML:
Dim row() As DataRow = MyDataSet.Tables("Centre").Select("CentreName = '" & centre & "'")


and the error reads:

"Object reference not set to an instance of an object."

Any ideas?
 
Here's the full code from the stub I created to test your issue.

I believe that I just changed the connection information and omitted part of the ToolTip that I was too lazy add to my dummy table.

VB.NET:
Imports System
Imports System.Data.Sql
Imports System.Data.SqlClient

Public Class Form1

	Private places As New Dictionary(Of String, Rectangle)
	Const strConnection As String = "Data Source=.\SQLEXPRESS;Initial Catalog=CentreExample;Integrated Security=True"
	Dim con As New SqlConnection(strConnection)
	Dim com As New SqlCommand("Select * from dbo.Centre", con)

	Dim MyDataSet As New DataSet()
	Dim CentreTableAdapter As New SqlDataAdapter()


	Private Sub LoadPlaces()
		Me.CentreTableAdapter.Fill(MyDataSet, "Centre")
		For Each row As DataRow In MyDataSet.Tables("Centre").Rows

			places.Add(row.Item("CentreName") & vbCrLf & ("Address: ") & row.Item("Address") & (", ") & row.Item("Postcode"), New Rectangle(row.Item("X1"), row.Item("Y1"), row.Item("X2"), row.Item("Y2")))

		Next

	End Sub

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

		CentreTableAdapter.SelectCommand = com
		LoadPlaces()

	End Sub

	Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

		For Each hotspot As Rectangle In places.Values
			e.Graphics.DrawEllipse(Pens.Red, hotspot)
		Next

	End Sub

	Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove

		Dim current As String = Me.ToolTip1.GetToolTip(Me.PictureBox1)
		For Each place As String In places.Keys
			If places(place).Contains(e.Location) Then
				If current <> place Then

					Me.ToolTip1.Show(place, Me.PictureBox1)
					Exit For
				End If
			End If
		Next
	End Sub

	Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
		For Each place As String In places.Keys
			If places(place).Contains(e.Location) Then

				'Split the place on the first vbCrLf and return the Centre Name (0th element)
				Dim centre As String = place.Split(Environment.NewLine)(0)

				'Select a 1 dimensional array of System.Data.DataRows from your Centre DataTable
				Dim row() As DataRow = MyDataSet.Tables("Centre").Select("CentreName = '" & centre & "'")

				'Since you won't have duplicate Centre Names get the items from row 0.
				Directions.TextBox1.Text = row(0).Item("CentreName")
				Directions.TextBox2.Text = row(0).Item("Postcode")

				Directions.Show()
				Me.Hide()

			End If
		Next
	End Sub

End Class
 
Hi MattP. Thanks for your last post... i had made a silly little mistake.

I have noticed one irritating thing though.

The first time i click a place on the map, on the second page it only fills out the Postcode and not the CentreName.

If i then go back to the map instantly and click a different place, all is filled out correctly.

It is only the very first time i run the program and click the map.

I have attached a screenshot showing exactly what I mean.
 

Attachments

  • getdirections.jpg
    getdirections.jpg
    3.9 KB · Views: 47
I'm not sure why your CentreName would be blank. It works fine on the sample program I created.

Put a break point here and check what's in the ItemArray for row(0) once you step through it.

Dim row() As DataRow = MyDataSet.Tables("Centre").Select("CentreName = '" & centre & "'")
 
I don't know why it wasn't working but i created a new form and got it working perfectly :)

I now want to apply the same idea to a picture.

I select a point on my map for instance "Manchester" and i want it to load a picture of Manchester on a second form.

The code I have currently is:

HTML:
 Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

        For Each place As String In places.Keys
            If places(place).Contains(e.Location) Then


                Dim centre As String = place.Split(Environment.NewLine)(0)
                'Dim strLoad_File As String
                ' Dim myDataTable As Integer

                Dim row() As DataRow = MyDataSet.Tables("dbo.Centre").Select("CentreName = '" & centre & "'")


                DirectionsNew.Label1.Text = row(0).Item("CentreName")
                DirectionsNew.Label2.Text = row(0).Item("Postcode")
                Contact_Us.Label12.Text = row(0)("TelephoneNumber")
                Contact_Us.Label13.Text = row(0)("Email")
                Contact_Us.Label14.Text = row(0)("Fax")
                Contact_Us.Label15.Text = row(0)("Address")
                Contact_Us.Label16.Text = row(0)("CentreName")
                Leave_your_Comments.Label6.Text = row(0)("CentreName")
                strLoad_File = myDataTable.Rows(0).Item("Picture")
                Contact_Us.PictureBox1.Load(IO.Path.Combine(Application.StartupPath, strLoad_File))


                Me.Hide()
                Stores.Show()

            End If
        Next
    End Sub
In particular this fragment of code:

HTML:
   strLoad_File = myDataTable.Rows(0).Item("Picture")
                Contact_Us.PictureBox1.Load(IO.Path.Combine(Application.StartupPath, strLoad_File))

Im not sure why this isn't working.

Any ideas would be fab. Thankies
 
I'm assuming that your 'Picture' column is just a varchar field with the name of the image and not a blob image in the database. The code you've got should be correct assuming the file exists.

Try this to see what's actually being passed to the Load method.

VB.NET:
MessageBox.Show(IO.Path.Combine(Application.StartupPath, strLoad_File))
 
Hi Matt,

Sorry for such a late response.

Right I am still having no luck with getting this picture showing.

I have attached some screenshots so you can see my database design.

I have only entered a picture file in the first and last row to test it. So when i click the corresponding point on the map it should work. But still nothing. I get the message:

"There is no row at position 0"

This is the fragment of code with the error:

HTML:
strLoad_File = myDataTable.Rows(intRow).Item("Picture")
                Contact_Us.PictureBox1.Load(IO.Path.Combine(Application.StartupPath, strLoad_File))

Thanks again
 

Attachments

  • centre28th.jpg
    centre28th.jpg
    101.9 KB · Views: 24
  • centre2 28th.jpg
    centre2 28th.jpg
    55.8 KB · Views: 23
One other thing:

I am also trying to load a background image that depending on what location is clicked on, will change accordingly.

This is the code I have:

HTML:
 Stores.BackgroundImage = row(0).Item("Background")

and I have a column "Background" in my centre table full of jpg files.

I get an error message saying:

"Unable to cast object of type 'System.String' to type 'System.Drawing.Image'."

Any clue?
 
and I have a column "Background" in my centre table full of jpg files.

When you say this do you mean a path to an image or a blob?

Assuming a string path to the image:

VB.NET:
Stores.BackgroundImage = System.Drawing.Image.FromFile(row(0).Item("Background")

Haven't tested this out but it should be pretty close.
 
Hi Matt,

Sorry for such a late response.

Right I am still having no luck with getting this picture showing.

I have attached some screenshots so you can see my database design.

I have only entered a picture file in the first and last row to test it. So when i click the corresponding point on the map it should work. But still nothing. I get the message:

"There is no row at position 0"

This is the fragment of code with the error:

HTML Code:

strLoad_File = myDataTable.Rows(intRow).Item("Picture")
Contact_Us.PictureBox1.Load(IO.Path.Combine(Application.StartupPath, strLoad_File))

Thanks again
 

Attachments

  • centre28th.jpg
    centre28th.jpg
    101.9 KB · Views: 25
  • centre2 28th.jpg
    centre2 28th.jpg
    55.8 KB · Views: 27
VB.NET:
Dim row() As DataRow = MyDataSet.Tables("dbo.Centre").Select("CentreName = '" & centre & "'")

Contact_Us.Label12.Text = row(0)("TelephoneNumber")
Contact_Us.Label13.Text = row(0)("Email")
Contact_Us.Label14.Text = row(0)("Fax")
Contact_Us.Label15.Text = row(0)("Address")
Contact_Us.Label16.Text = row(0)("CentreName")

strLoad_File = myDataTable.Rows(0).Item("Picture")
Contact_Us.PictureBox1.Load(IO.Path.Combine(Application.StartupPath, strLoad_File))

You've got an array of DataRow 'row' that you've been using for loading all of your textboxes and then you switch to using myDataTable for this one.

I went ahead and put a test picture in my debug directory and the code below worked just fine for me.

VB.NET:
Contact.uxCentreName.Text = row(0).Item("CentreName")
Contact.uxAddress.Text = row(0).Item("Address")
Contact.PictureBox1.Load(IO.Path.Combine(Application.StartupPath, row(0).Item("Picture")))

Contact.Show()

While you're still in the design phase I would STRONGLY suggest using meaningful names for your controls. Label12 isn't going to mean anything to the next programmer to look at your code (even you) but lblPhoneNum will.
 

Latest posts

Back
Top