Results 1 to 8 of 8

Thread: Problem Accessing Controls from inside my custom class

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36

    Problem Accessing Controls from inside my custom class

    I am pretty new to VB.Net and I am playing around with classes. I understand the basics but I am having a problem accessing a control on my form from within my new class.

    Example:
    I have a label on my main form.
    I have created a new class (car)
    When I call a Method in my car class to name the car I would like it to change the text property of my label.
    I am not sure how to refrence the label control from my class ( I have tried Mylable.Text = "Ford") But I get an error.

    I am sure I need to put in some sort of refrence back to the original control but I am not sure what to put.

    Also:
    I have 2 picture boxes on my Form
    I am trying to store a picturebox (MyPictureBox1) as a member variable in my class.
    The result I am looking for is to be able to say MyClass.Picture.Visible = True and change the state of the associated picturebox change from hidden to visible.

    Thanks,
    -Patrick

  2. #2
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    A class is encapsulated, which means it can only see what is inside itself. What you need to do is pass in a reference to your form in your class's Constructor and declare a private variable:

    Private m_myForm as MyForm

    Public Sub New(ByVal myForm as MyForm)

    ' And then in your constructor, set them equal

    m_myForm = myForm

    The you will be able to access your controls like:

    m_myForm.label1.text = "New text"
    Last edited by SeanGrebey; Jun 10th, 2004 at 09:20 AM.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36

    A bit more help

    Ok, I have been playing around with this and I understand the idea, but I am having a bit of trouble with the implementation.

    When I try to pass a reference of the form to the class I am getting an error when I create the Object. It tells me that Form1 *the name of my form in which the label resides* is a type and cannot be used as an expression.

    Its my understanding if I use perameters in my classs's constructor I need to pass in the correct datatype when I create a new instance of the object.

    I have tried passing the following to no avail:
    Code:
    Dim NewCar as Car
    
    NewCar = New Car()
    NewCar = New Car(Form1)
    NewCar = New Car(Me.ParentForm)
    Thanks for the help,

  4. #4
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    try:

    NewCar = new Car(me)

    Edit: Form1 is a Type, not a variable....that is to say, you could open up 3 different copies of the form you laid out. Me is the reference to the object you are working out of (i.e. the active form)

    Also you make sure you are using ByVal in the Constructor, not ByRef as objects are references types, not value types (i.e. objects are always by reference)

    Public Class Car

    private m_MyForm as Form1

    Public Sub New(ByVal myForm as Form1)

    m_MyForm = MyForm

    m_MyForm.Label1.text = "Some text"
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    I cant thank you enough. I have been struggling with this all night. This soultion works great and I have learned a bunch in the process.

    Once again THANKS!

    -Patrick

  6. #6
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618
    No problem.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    Ok Sean one more question, the previous solution worked great, but now I have run into a bit of another snag. I am trying to do the same thing (access a control on the original form), but this time I am trying to access an ImageList control. I assume I need to create a refrence to the ImageList in my constructor, but I cant seem to figure out how to refrence it. I have tried the following

    Code:
    Public Sub New(ByVal _PForm As Form1, ByVal _PImgList As ImageList)
    
    'also tried
    'Public Sub New(ByVal _PForm As Form1, ByVal _PImgList As ImlCars)
    
            _myForm = _PForm
            _myImgList = _PImgList
    
        End Sub
    and to create the class
    Code:
    Dim Car1 As Car
    Car1= New Car(Me, Me.ImlCars)
    'Car1 = New Car(Me, ImlCars)
    The class works great in the form you gave me just refrencing the form, I am assuming my ImageList (ImlCars) dosent reside on the form so I cannot use Me to reference it.

    Thanks again you have been a ton of help

    -Patrick

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    I figured it out, I was accessing the ImageList incorrectly. Simply refrencing the original Form allows you to access all ImageList controls.

    Thanks,
    -Patrick

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width