Accessing the value of a control property on another page


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: Accessing the value of a control property on another page

  1. #1
    Marion Guest

    Accessing the value of a control property on another page


    I am just starting my first project in vb.net and trying to do something that
    was very simple in vb6. In VB.net I have a form named fclsVehicles and on
    the form is a listbox control named lstVehicles. I also have a standard module
    named module1. From module1 I need to access the value of the lstVehicles
    text property. With VB6 it was simple as form1.listbox1.text. I'm learning
    now that you must use a public property and the Get Accessor. Can anyone
    possible give my an example to get me started in the right direction. Anyone's
    help would be very much appreciated. Thanks!



  2. #2
    Patrick Steele [MVP] Guest

    Re: Accessing the value of a control property on another page

    In article <3c8e373b$1@10.1.10.29> (from Marion <myester1@yahoo.com>),
    > I am just starting my first project in vb.net and trying to do something that
    > was very simple in vb6. In VB.net I have a form named fclsVehicles and on
    > the form is a listbox control named lstVehicles. I also have a standard module
    > named module1. From module1 I need to access the value of the lstVehicles
    > text property. With VB6 it was simple as form1.listbox1.text. I'm learning
    > now that you must use a public property and the Get Accessor. Can anyone
    > possible give my an example to get me started in the right direction. Anyone's
    > help would be very much appreciated. Thanks!


    On fclsVehicles:

    Public ReadOnly Property VehText() As String
    Get
    Return listbox1.text
    End Get
    End Property

    Assuming, of course, your listbox is named "listbox1".

    --
    Patrick Steele
    Microsoft .NET MVP

  3. #3
    Marion Guest

    Re: Accessing the value of a control property on another page


    Patrick Steele [MVP] <patrick@mvps.org> wrote:
    >In article <3c8e373b$1@10.1.10.29> (from Marion <myester1@yahoo.com>),
    >> I am just starting my first project in vb.net and trying to do something

    that
    >> was very simple in vb6. In VB.net I have a form named fclsVehicles and

    on
    >> the form is a listbox control named lstVehicles. I also have a standard

    module
    >> named module1. From module1 I need to access the value of the lstVehicles
    >> text property. With VB6 it was simple as form1.listbox1.text. I'm learning
    >> now that you must use a public property and the Get Accessor. Can anyone
    >> possible give my an example to get me started in the right direction.

    Anyone's
    >> help would be very much appreciated. Thanks!

    >
    >On fclsVehicles:
    >
    >Public ReadOnly Property VehText() As String
    > Get
    > Return listbox1.text
    > End Get
    >End Property
    >
    >Assuming, of course, your listbox is named "listbox1".
    >
    >--
    >Patrick Steele
    >Microsoft .NET MVP


    Patrick, I see how that works, thanks a lot. One more question. I have a
    variable name strVehicleInfo in a standard module. I want to set strVehicleInfo
    to the value of VehText. I know this may sound pretty simple, but is a little
    intimidating trying to learn vb.net. Thanks!


  4. #4
    Patrick Steele [MVP] Guest

    Re: Accessing the value of a control property on another page

    In article <3c8e6015$1@10.1.10.29> (from Marion <myester1@yahoo.com>),
    > I have a
    > variable name strVehicleInfo in a standard module. I want to set strVehicleInfo
    > to the value of VehText. I know this may sound pretty simple, but is a little
    > intimidating trying to learn vb.net. Thanks!


    You'll need to use the variable that contains the instance of your form.
    For example, assuming your form was created as "TheVehForm" then:

    strVehicleInfo = TheVehForm.VehText

    --
    Patrick Steele
    Microsoft .NET MVP

  5. #5
    Marion Guest

    Re: Accessing the value of a control property on another page


    Patrick Steele [MVP] <patrick@mvps.org> wrote:
    >In article <3c8e6015$1@10.1.10.29> (from Marion <myester1@yahoo.com>),
    >> I have a
    >> variable name strVehicleInfo in a standard module. I want to set strVehicleInfo
    >> to the value of VehText. I know this may sound pretty simple, but is a

    little
    >> intimidating trying to learn vb.net. Thanks!

    >
    >You'll need to use the variable that contains the instance of your form.


    >For example, assuming your form was created as "TheVehForm" then:
    >
    >strVehicleInfo = TheVehForm.VehText
    >
    >--
    >Patrick Steele
    >Microsoft .NET MVP


    I tried that Patrick and I get a message that says "Reference to a non-shared
    member requires an object reference." I apologize for being so ingnorant
    about vb.net Can you suggest a good book to learn vb.net? Thanks!


  6. #6
    Patrick Steele [MVP] Guest

    Re: Accessing the value of a control property on another page

    In article <3c8fbd59$1@10.1.10.29> (from Marion <myester1@yahoo.com>),
    > >You'll need to use the variable that contains the instance of your form.

    >
    > >For example, assuming your form was created as "TheVehForm" then:
    > >
    > >strVehicleInfo = TheVehForm.VehText
    > >
    > >--
    > >Patrick Steele
    > >Microsoft .NET MVP

    >
    > I tried that Patrick and I get a message that says "Reference to a non-shared
    > member requires an object reference." I apologize for being so ingnorant
    > about vb.net Can you suggest a good book to learn vb.net? Thanks!


    Try Dan Appleman's "Moving to VB.NET". It's pretty good.

    Your problem is probably because you're accessing the form's name (it's
    class name) instead of a created instance of that form (class). Here's
    a quick example:

    Public Class MyForm
    Inherits System.Windows.Forms.Form

    ...
    End Class

    This defines a form (a class that inherits from the
    System.Windows.Forms.Form class). To actually use this, you need to
    create an instance of it. So, in a module:

    Module Module1
    Sub Main()
    ' define a variable of type 'MyForm'
    Dim VehForm As MyForm

    ' create an instance of our form
    VehForm = New MyForm()

    ' start a message processing loop
    Application.Run(VehForm)
    End Sub
    End Module

    If you simply try to access a member of the Class with:

    MyForm.xxx

    You'll get the error you described since MyForm is really a "template".
    By creating an instance of it in 'VehForm', we have a usable object.

    There's one exception to this rule of member access. A class can have
    shared members. A shared member exists all the time -- you don't need
    to create an instance of it to access the method. Here's an example:

    Class MyUtilities
    Public Shared Function AddAPeriod(ByVal str As String) As String
    Return str & "."
    End Function
    End Class

    This function can now be accessed simply by doing:

    MyUtilities.AddAPeriod("hello")

    Hope this gets you going!

    --
    Patrick Steele
    Microsoft .NET MVP

  7. #7
    Marion Guest

    Re: Accessing the value of a control property on another page


    >Thanks for the adice on the book Patrick. I will check it out at BooksAMillion

    today. I am still having a problem so I will include the exact code I am
    using. Within the GetVehicleInfo procedure in Module1 I am trying to do this,
    strId = VehForm.VehText. Again, I really thank you for your time and expertise.
    From a standard module:
    Module Module1
    Public p_conn As New ADODB.Connection()
    Public strVehInfo As String
    Public strVehYear As String

    Sub Main()
    Dim VehForm As fclsAutoTracker
    VehForm = New fclsAutoTracker()
    Application.Run(VehForm)
    End Sub

    Public Sub GetVehicleInfo()
    Dim rstInfo As New ADODB.Recordset()
    Dim strQuery As String
    Dim strId As String
    Dim VehForm As fclsAutoTracker
    VehForm = New fclsAutoTracker()
    strId = VehForm.VehText 'Patrick, This is where I am having the problem.


    ConnectToDatabase()
    'Mid(strVehInfo, 1, 1)
    'strQuery = "Select * From VehicleInfo Where VehicleId = '" & strId
    & "'"
    'rstInfo.Open(strQuery, p_conn, ADODB.CursorTypeEnum.adOpenKeyset,
    ADODB.LockTypeEnum.adLockReadOnly)
    'strVehYear = rstInfo.Fields("Year").Value
    'rstInfo.Close()
    'p_conn.Close()
    End Sub
    Public Sub ConnectToDatabase()
    p_conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=f:\Program
    Files\AutoMaint\AutoMaint.mdb")
    End Sub
    End Module

    And this is my form:
    Public Class fclsAutoTracker
    Inherits System.Windows.Forms.Form
    Public strText As String


    Public ReadOnly Property VehText() As String
    Get
    Return lstVehicles.Text
    End Get
    End Property

    Private Sub lstVehicles_SelectedIndexChanged(ByVal sender As System.Object,
    ByVal e As System.EventArgs) Handles lstVehicles.SelectedIndexChanged
    Select Case tabAutoTracker.SelectedIndex
    Case 0 : GetVehicleInfo()
    Case 1
    Case 2
    End Select
    End Sub
    End Class

  8. #8
    Patrick Steele [MVP] Guest

    Re: Accessing the value of a control property on another page


    1. Change your GetVehicleInfo to accept a fclsAutoTracker object
    (instead of defining one in the code):

    Public Sub GetVehicleInfo(vForm as fclsAutoTracker)

    2. Change the call to GetVehicleInfo to:

    GetVehicleInfo(Me)

    3. Finally, change the line of code with the error to:

    vForm.VehText

    The first change is to give GetVehicleInfo a variable which is the
    instance of the form.

    The next step is a modification to your form code that calls
    GetVehicleInfo. You can use the keyword "Me" to pass the instance of
    the object you're running inside (the VehForm).

    Finally, the GetVehicleInfo gets the string from the form passed in.

    However, to better separate the logic in this app, GetVehicleInfo really
    just needs the ID. Instead of passing it a form from which you pull the
    ID, you could simply pass the ID itself:

    Public Sub GetVehicleInfo(strId as String)

    Now the GetVehicleInfo is more resusable since it's not tied to
    requiring a fclsAutoTracker class.

    --
    Patrick Steele
    Microsoft .NET MVP

  9. #9
    Marion Guest

    Re: Accessing the value of a control property on another page


    Patrick Steele [MVP] <patrick@mvps.org> wrote:
    >
    >1. Change your GetVehicleInfo to accept a fclsAutoTracker object
    >(instead of defining one in the code):
    >
    > Public Sub GetVehicleInfo(vForm as fclsAutoTracker)
    >
    >2. Change the call to GetVehicleInfo to:
    >
    > GetVehicleInfo(Me)
    >
    >3. Finally, change the line of code with the error to:
    >
    > vForm.VehText
    >
    >The first change is to give GetVehicleInfo a variable which is the
    >instance of the form.
    >
    >The next step is a modification to your form code that calls
    >GetVehicleInfo. You can use the keyword "Me" to pass the instance of
    >the object you're running inside (the VehForm).
    >
    >Finally, the GetVehicleInfo gets the string from the form passed in.
    >
    >However, to better separate the logic in this app, GetVehicleInfo really


    >just needs the ID. Instead of passing it a form from which you pull the


    >ID, you could simply pass the ID itself:
    >
    >Public Sub GetVehicleInfo(strId as String)
    >
    >Now the GetVehicleInfo is more resusable since it's not tied to
    >requiring a fclsAutoTracker class.
    >
    >--
    >Patrick Steele
    >Microsoft .NET MVP


    Patrick, I made the changes you suggested and now I am getting:

    Argument not specified for parameter 'vForm' of 'Public Sub GetVehicleInfo(vForm
    As fclsAutoTracker)'.

    I will continue to look at this and try to figure it out. It's like learning
    to walk all over again! Thanks for all of your help.


  10. #10
    Patrick Steele [MVP] Guest

    Re: Accessing the value of a control property on another page

    In article <3c911ae0$1@10.1.10.29> (from Marion <myester1@yahoo.com>),
    > Patrick, I made the changes you suggested and now I am getting:
    >
    > Argument not specified for parameter 'vForm' of 'Public Sub GetVehicleInfo(vForm
    > As fclsAutoTracker)'.


    Are you sure you're changing the call to GetVehicleInfo to include the
    'Me' reference? I.e.:

    GetVehicleInfo(Me)

    --
    Patrick Steele
    Microsoft .NET MVP

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center