Results 1 to 7 of 7

Thread: Use Form2 To Change Form1 Control

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    10

    Use Form2 To Change Form1 Control

    form1 has a label on it.
    form2 has a button on it.

    But I can't work out how to change the label's text via the button.
    The code below doesn't work. It just says frm1 is not declared.
    Code:
    Public Class Form2
    
    	Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	dim frm1 As New Form1
    	End Sub
    
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	frm1.Label1.Text = "Text goes here"
    	End Sub
    
    End Class
    I looked on the net and it may have something to do with the scope of the variable.

    But when I did this...

    Public Class Form2
    Public Shared frm1 As New form1

    It still doesn't work. So what code am I missing? Thanks.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,130

    Re: Use Form2 To Change Form1 Control

    Again, OBJECTS! Let's say that I have a notepad. If you wanted a message to appear on my notepad, would you go and buy another notepad of the same type and write on it and then wonder why I couldn't see the message? Of course you wouldn't. That would be expecting magic, but that's exactly what you're doing here.

    You already have a Form1 object created and displayed on the screen. In your Form2 object, you create a new Form1 object and then put some text on a Label it contains. Why would you expect modifying that new Form1 object you just created to affect an existing Form1 object that is already displayed? It would be magic. If you expect to affect the existing Form1 object then you have to modify that object, not a different object of the same type.

    There are various ways you could go about this; some better than others. The proper way to go about this would be for Form2 to not even know that Form1 exists. Form2 would raise an event and Form1 would handle that event and update its own Label. It is Form1 that is creating that Form2 object so it has access to it in order to handle the event. It's pretty much the same thing as handling that Click event of that Button. The Button just raises the event and Form2 handles it and then performs its own action. It's not the Button doing something to the form; it's the Button telling the world that it was clicked and the form listening for that notification and then acting accordingly.

    That's probably a bit advanced for now though. To start out, people do tend to take the hacky option of one form acting directly on another. To do that, there are two options.

    Firstly, when the Form1 instance creates the Form2 instance, it can tell the Form2 instance about itself. Form2 then has that reference to the existing Form1 object instead of creating a new one that is of no use.
    vb.net Code:
    1. Public Class Form2
    2.  
    3.     Private frm1 As Form1
    4.  
    5.     Public Sub New(frm1 As Form1)
    6.         ' This call is required by the designer.
    7.         InitializeComponent()
    8.  
    9.         ' Add any initialization after the InitializeComponent() call.
    10.  
    11.         Me.frm1 = frm1
    12.     End Sub
    13.  
    14.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    15.         frm1.Label1.Text = "Hello World"
    16.     End Sub
    17.  
    18. End Class
    In Form1:
    vb.net Code:
    1. Dim frm2 As New Form2(Me)
    2.  
    3. frm2.Show()
    The other option is to take advantage of default instances. Default instances of forms is a bit of "magic" specific to VB, although it is just a surface layer nicety and is actually straight OOP under the hood. Each form type has a default instance, which is an instance of that type that you can always access anywhere via just the type name. Using that, you don't have to pass object references around, because the default instance is always accessible. Handily, the startup form is the default instance of its type. That means that, from Form2, you can access your startup form using that default instance. That means that, assuming Form1 is your startup form, your Form2 code can simplify to this:
    vb.net Code:
    1. Public Class Form2
    2.  
    3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    4.         Form1.Label1.Text = "Hello World"
    5.     End Sub
    6.  
    7. End Class
    Default instances do have some limitations though, and people often fall afoul of them. I suggest that you follow the Blog link in my signature below and check out my post on the subject of default instances. I suggest that you also read my three-part post on passing data among multiple forms, which goes into more detail about the possible ways to have forms interact, including using default instances, and also explains the right way.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,130

    Re: Use Form2 To Change Form1 Control

    It should also be noted that there was a scope issue in your code that would prevent it compiling but, even fixing that would not have helped. That issue arose because you declared a variable in one method and then tried to use that variable in a different method. Again, magic. A local variable only exists in the scope it was declared. In your Click event handler, there is no frm1 so that code is nonsense. Notice that, in my code, I declare the frm1 at the class level, so it is accessible from all methods in that class.

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    10

    Re: Use Form2 To Change Form1 Control

    Again, OBJECTS!
    That made me laugh (in a good way)!

    As for the rest of your reply (and I thank you for it) that's a lot to comprehend.

    I'll respond soon.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,130

    Re: Use Form2 To Change Form1 Control

    Quote Originally Posted by Dim Wit View Post
    that's a lot to comprehend.
    It is. I definitely recommend that you read those blog posts I mentioned. They're a bit more step by step and provide various examples that you can try along the way.

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2021
    Posts
    10

    Re: Use Form2 To Change Form1 Control

    I'm struggling to get my head around this. So... I just need to know if my notes below are correct or not (I'm really struggling with types and how to identify them and I need to know this info before I can move on).

    An Object Orientated Program (OOP), as the name suggests, is a program that is orientated towards objects.

    VB.Net is an OOP, therefore VB.Net is also orientated towards objects.

    In other words, VB.Net is a program that has a lot to do with, and often deals with, objects.

    For example...

    A form is an object.
    A button is an object.
    A string is an object (I don't really understand this but I'll accept it for now).

    To create an object in VB.Net you need a class.

    A class is a blueprint from which you can create various objects of a specific type.

    And the type of object is defined by a class.

    In other words, when you create an object, you need a class that defines the type of object you want to create.

    For example...

    Code:
    Public Class Form1
    This class allows you to create a type of form from the Form1 class.

    Thanks for any assistance!

    And yes, I started reading your blog JM.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,130

    Re: Use Form2 To Change Form1 Control

    That's mostly correct but it's a bit wonky early on. OOP stands for Object-Oriented Programming. It is a style of programming where you create objects that possess data and behaviour and a program works via interactions between those objects. VB.NET is an object-oriented language, in that it enables you to write programs using an object-oriented paradigm.

    As I have said before, OOP is based on the behaviour of real-world objects so you should always go back to real-world objects to see how it works. In programming, the term "class" is used in the context of classification. A class is basically a classification of objects. In VB, a class describes the data an object of that type has (properties) and the behaviour an object of that type does (methods). Each object will have different values for those properties and it will engage in different combinations of behaviour but it still has and does what its class defines. Consider a person and their pet dog. Their pet is an object - an instance of the Dog class. Dog inherits Mammal and mammal inherits Animal, so a Dog has all the common traits and behaviour of a Mammal and a Mammal has all the common traits and behaviour of an Animal. The person is an instance of the Person class and that class has a Pet property of type Animal. In code, you would create an instance of the Person class, create an instance of the Dog class and assign the Dog object to the Pet property of the Person object. There is only one Dog class and that class has properties like Height, Weight, Colour and HairLength. Every Dog object will have different values for each of those properties but they all have those properties. Similarly, there is only one Form1 class but there can be many Form1 objects, each with different values for the same properties, e.g. two Form1 objects displayed on the screen at the same time would have different values for the Location property and possible different Size and Text values.

    That stuff is pretty easy because you're dealing with tangible things in the real world. It becomes a bit harder when you're dealing with abstract things that don't have a direct real-world analogue. For instance, as you say, String is a class and each String object is an instance of that class. Just use what you already know about programming types and objects though, because it's all the same. In an OO language, everything is an object. Everything has to be stored in memory as binary code so everything gets treated the same. A String is an object that contains text, an Integer is an object that contains a number, a Date is an object that contains a date/time and so on. It can be a bit confusing because, for those fundamental types, the object pretty much IS the data, rather than an object that CONTAINS the data. Just think of it as complex objects being made up of simpler objects and those being made up of simpler objects until you get down to the simplest objects possible. Those simplest types are the fundamental types like String, Integer, Date, etc. They can still have behaviour, e.g. the Split method of a String or the ToString method of an Integer, but they aren't really made up of multiple simpler objects. A String sort of is, because each character in a String is a Char, but maybe that's too much to take in in one go.

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