Results 1 to 11 of 11

Thread: Help. Reading same object on different forms.

  1. #1

    Thread Starter
    Lively Member Mugsy323's Avatar
    Join Date
    Oct 2014
    Posts
    112

    Question Help. Reading same object on different forms.

    I have two nearly identical forms (only one of which is loaded into a Panel at a time.) I want to read the value of an object that appears on both forms using a single command:

    (Sample code)
    Code:
    Dim RandomForm as new Form  ' I know this doesn't work.
    If X = 1 then RandomForm = Form1
    If X = 100 then RandomForm = Form2
    RandomForm.Label1.Text = X  ' There is a "Label1" on both forms.
    Is there a way to do this?

    TIA

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help. Reading same object on different forms.

    Without using late binding, you need a common type that contains Label1 and to use that rather than Form. There are two options for that.

    Firstly, you can create a common base class. You can create a form and add Label1, then you can have these other two forms inherit that. You would then use the base class to refer to either of the derived types and access the common member(s).

    Secondly, you can define a common interface and have both forms implement that. The two forms would each have their own member(s) but you specify that they implement the member(s) of the same name from the interface. You would then use the interface to refer to either of the concrete types and access the common member(s).

    You should do a bit of research on inheritance and interface implementation.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member Mugsy323's Avatar
    Join Date
    Oct 2014
    Posts
    112

    Re: Help. Reading same object on different forms.

    Thx for the reply.

    I'm probably too much of a Newbie to understand "Common Base Class". If I understand correctly, this would be a third "universal" form containing all the elements of Form1 & Form2. That's not really an option. "Label1" was just a simplified example. The actual situation is far more complex.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Help. Reading same object on different forms.

    Whenever I reach for a common base class, I end up re-writing to use an interface instead....eventually, though it could be quite a while later. Partially for that reason, and partially because once you have one you always find other uses for it, I would say that an interface is the right way to go. Both will work, as JMC said, but the interface tends to be more advantageous in the long run, in my experience.

    However, both could seem pretty daunting if you haven't worked with them, yet. Interfaces in particular, can be a bit intimidating for a person just starting out in them. They're very simple in practice, but the theory behind them tends to get a bit complex. Even the simplest descriptions for them tend to be kind of wordy. Fortunately, they really are simple beasts, so just fiddling around with them is fairly easy. In fact, if you are using a reasonably recent version of VS (I don't know when this feature was added, but certainly 2019, and probably earlier), then you will have the option to create an interface from any class you have already written. Forms are classes, so if you have a form, you can put the cursor on the class (Public Class YourClassNameHere), click on the screwdriver icon (or right click on the class name and select Quick Actions and Refactoring), and select Extract Interface. This will offer to create the interface in the same file or a different file. I prefer to use a different file, but you should do what you feel like, and the choice has no consequence.

    Doing that will give you an interface, so you can see what one looks like. This will do a couple things, as it will give you the interface and will alter the class so that it implements the interface. When you see what changes it made, you can see how little there is to it, and you can change that readily enough.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Lively Member Mugsy323's Avatar
    Join Date
    Oct 2014
    Posts
    112

    Re: Help. Reading same object on different forms.

    Thx. Looks like a "rewrite" is my only choice".

  6. #6
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: Help. Reading same object on different forms.

    Quote Originally Posted by Mugsy323 View Post
    Thx. Looks like a "rewrite" is my only choice".
    If your example code is as simple as it gets, there's other options:

    Code:
    If X = 1 Then
        Form1.Label1.Text = X
    ElseIf X = 100 Then
        Form2.Label1.Text = X
    End If

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Help. Reading same object on different forms.

    I created a forms app with three forms. Form2 and Form3 both have a label named 'Label1'.

    Form1 code
    Code:
    Public Class Form1
    
        Private Shared PRNG As New Random
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f As Windows.Forms.Form
            If PRNG.Next(2) = 0 Then
                f = New Form2
            Else
                f = New Form3
            End If
            Dim lblVal As String = ""
            Dim lbl As Label = DirectCast(f.Controls("Label1"), Label)
            lblVal = lbl.Text
            f.Dispose()
            Stop
        End Sub
    End Class
    Hope it helps
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    Lively Member Mugsy323's Avatar
    Join Date
    Oct 2014
    Posts
    112

    Re: Help. Reading same object on different forms.

    Quote Originally Posted by OptionBase1 View Post
    If your example code is as simple as it gets, there's other options:

    Code:
    If X = 1 Then
        Form1.Label1.Text = X
    ElseIf X = 100 Then
        Form2.Label1.Text = X
    End If
    The problem is definitely far more complex, but thanks anyway.

    Quote Originally Posted by dbasnett View Post
    I created a forms app with three forms. Form2 and Form3 both have a label named 'Label1'.

    Form1 code
    Code:
    Public Class Form1
    
        Private Shared PRNG As New Random
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim f As Windows.Forms.Form
            If PRNG.Next(2) = 0 Then
                f = New Form2
            Else
                f = New Form3
            End If
            Dim lblVal As String = ""
            Dim lbl As Label = DirectCast(f.Controls("Label1"), Label)
            lblVal = lbl.Text
            f.Dispose()
            Stop
        End Sub
    End Class
    Hope it helps
    This looks interesting. The "Windows.Forms.Form" might be the missing piece of the puzzle. I'll give it a try. Thx.

  9. #9

    Thread Starter
    Lively Member Mugsy323's Avatar
    Join Date
    Oct 2014
    Posts
    112

    Re: Help. Reading same object on different forms.

    I tried your example. It functions, but unfortunately can't be adapted to my situation (I'd need hundreds of references to every common object on each form.)

    I've already started work on a single universal form and hiding unwanted/unneeded objects until needed. Time consuming, but will simplify things in the end.

    Thx.

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Help. Reading same object on different forms.

    Quote Originally Posted by Mugsy323 View Post
    I tried your example. It functions, but unfortunately can't be adapted to my situation (I'd need hundreds of references to every common object on each form.)

    I've already started work on a single universal form and hiding unwanted/unneeded objects until needed. Time consuming, but will simplify things in the end.

    Thx.
    OK, I suspect you've got a major design flaw here ... what is it EXACTLY you're trying to do here? And I don't mean the technical or the how... but the WHAT. What is the functionality you're after here?


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11

    Thread Starter
    Lively Member Mugsy323's Avatar
    Join Date
    Oct 2014
    Posts
    112

    Re: Help. Reading same object on different forms.

    I wrote an app that I've been updating for years to manage Saved data for another app that is now in it's third generation.

    So I have separate forms laying out the data that varies based on both on the version of the software they are managing and their screen resolution.

    Newer versions of the commercial app that my software manages includes fields not found in earlier versions (and substantially more fields.)

    The size, content & layout of each form is formatted for best appearance & ease of use for each generation of the app. But this means lots of redundant code (or unnecessarily complex code.) I was hoping to simplify things by referring to items common to all forms with a single line/block of code.
    Last edited by Mugsy323; Aug 18th, 2023 at 08:26 AM.

Tags for this Thread

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