Group,

I'm declaring a variable in one form that I need to make available in a second form. My code in Form1 looks like this:

Public Sub FuncKeysModule(ByVal value As Keys)

        Select Case value
            Case Keys.F5
                Dim searchtype As Integer
                If txbPartNo.Focused = True Then
                    Popup_PartNumbers.Label1.Text = "Enter the beginning characters of the Part Number to search"
                    searchtype = 1
                End If
                If txbDesc.Focused = True Then
                    Popup_PartNumbers.Label1.Text = "Enter a word from the Part Number Description to search"
                    searchtype = 2
                End If
                Popup_PartNumbers.ShowDialog()
        End Select

    End Sub

The variable "searchtype" (1 or 2) needs to be read in Form2 to run either one of two routines. How should this be written in Form1 and then how do I read it in Form2.

In advance, thanks for your help.

Don

you're going to have to make searchtype public global so that Form2 can read it.

You will likely get several techniques for doing this. Here are two that I find preferable.

Public Class Form1

   Sub LaunchForm2()
      Dim f2 As New Form2
      f2.needVariable = 2 ' pass as property
      f2.ShowDialog()
   End Sub
   Sub LaunchForm3()
      Dim f3 As New Form3(3) ' pass in constructor
      f3.ShowDialog()
   End Sub

   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      LaunchForm2()
      LaunchForm3()
   End Sub
End Class

'Declare a property on the 2nd form

Public Class Form2
   Inherits Form

   Private _needVariable As Int32
   Public Property needVariable() As Int32
      Get
         Return _needVariable
      End Get
      Set(ByVal value As Int32)
         _needVariable = value
      End Set
   End Property 'needVariable

   Private Sub Form2_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
      MsgBox("F2: " & needVariable.ToString())
      Me.Close()
   End Sub
End Class

' pass the value in the form's constructor

Public Class Form3
   Inherits Form

   Private needVariable As Int32


   Public Sub New(ByVal needVariable As Int32)
      Me.needVariable = needVariable
   End Sub

   Private Sub Form2_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
      MsgBox("F3: " & needVariable.ToString())
      Me.Close()
   End Sub
End Class
commented: Posted my post before reading your post! +0

You can declare searchtype as Public in Popup_PartNumbers then set the value the same as the labels

For what it's worth, I'm trying this in Form1:

Public Class OrderEntry2

    Public txbSearchType = New System.Windows.Forms.TextBox()

    Public Sub FuncKeysModule(ByVal value As Keys)


        'Check what function key is in a pressed state, and then perform the corresponding action.
        Select Case value
            Case Keys.F5
                Dim searchtype As Integer
                If txbPartNo.Focused = True Then
                    Popup_PartNumbers.Label1.Text = "Enter the beginning characters of the Part Number to search"
                    searchtype = 1
                    txbSearchType.Text = searchtype
                End If
                If txbDesc.Focused = True Then
                    Popup_PartNumbers.Label1.Text = "Enter a word from the Part Number Description to search"
                    searchtype = 2
                    txbSearchType.Text = searchtype
                End If
                Popup_PartNumbers.ShowDialog()
        End Select

    End Sub

Form2 looks like this:

Public Class Popup_PartNumbers

    Dim searchtype As Integer = Convert.ToInt32(OrderEntry2.txbSearchType)
    Dim con As New SqlConnection("Data Source=Don-PC;Initial Catalog=DataDesignSolutions;Integrated Security = True;User ID=DON-PC;Password=be67011")
    Dim cmd As New SqlCommand("", con)
    Dim search1 As String
    Dim string1 As String

    Private Sub btnFindPart_Click(sender As System.Object, e As System.EventArgs) Handles btnFindPart.Click

        If searchtype = 1 Then
            string1 = txbPart.Text
            search1 = "'" & string1 & "%" & "'"
            cmd.CommandText = "SELECT PartNumber, Description FROM INVENTORY_PRODUCTS where WHERE PartNumber LIKE " & search1
        End If

        If searchtype = 2 Then
            search1 = "'" & "%" & string1 & "%" & "'"
            cmd.CommandText = "SELECT PartNumber, Description FROM INVENTORY_PRODUCTS where WHERE Description LIKE " & search1
        End If

        lvPopup.Items.Clear()
        con.Open()
        Dim rdr As SqlDataReader = cmd.ExecuteReader
        Do While rdr.Read()
            lvPopup.Items.Add(New ListViewItem({rdr(0), rdr(1)}))
        Loop
        rdr.Close()
        con.Close()

I've not been able to get these routines to work as I have 2 issues that I'm working on to fix. One of these is: Click Here. The other I will post about shortly.

Does the code above appear as though it should work? I'm not getting any errors. Further, I do have a similar routine that does work in another form(s).

Thanks for your help.

Don

just make the seatchtype variable public i.e. <code>Public searchtype As Integer</code>, read more on varaible scopes to understand why.

The safest way to do so, if you have not solved the problem, would be to use Properties.

For Example:

Public Class Form1  
    Public Property TextString As String
        Get
            Return TextBox1.Text
        End Get
        Set(value As String)
            TextBox1.Text = value
        End Set
    End Property    
End Class

Public Class Form2
    Private Sub btnSendTo_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSendTo.Click
        Form1.TextString = "This is a test."
    End Sub
End Class
commented: something along the lines of great minds think alike ;>) +8

you could use a my.settings this is how I send and receive global messages. When I first started programming I used something like this.

Public Function fncVal(stVal As String) As String
    Static stNewVal As String
    If stVal <> "q" Then
        stNewVal = stVal
    ElseIf stVal = "" Then
        stNewVal = ""
    End If
    fncVal = stNewVal
End Function

to us fncval = val

to retrieve 
val = fncval("q")

joel.hahn,

I'm going to give this one a try. I'm going to have to read some data from my sql files and use them throughout the program. Having it stored in memory would be a lot more efficient.

Don

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.