Results 1 to 14 of 14

Thread: Changing a label from a module

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2001
    Posts
    52

    Question Changing a label from a module

    I have been trying out vb.net and I cannot work out how to change a label from a module. In VB6 from my module, I would say: form1.label.caption = "test". Does anyone know how to do it now?

  2. #2
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    How about
    form1.Conrols("label").Caption = "test"
    Baaaaaaaaah

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2001
    Posts
    52
    That doesn't work. Any other ideas?

  4. #4
    Tygur
    Guest
    You need to declare a new instance of your form. After that, you should be able to do something like this:
    FormVariableName.LabelName.Text = "Stuff"

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2001
    Posts
    52
    Does this mean that I need to create a new instance for every form that I want to use? This is going to take a lot of extra time to do and also use extra memory. Is that the only way to do it?

  6. #6
    Tygur
    Guest
    Originally posted by mbjunior99
    Does this mean that I need to create a new instance for every form that I want to use? This is going to take a lot of extra time to do and also use extra memory. Is that the only way to do it?
    I don't see why it's tedious. If you want, you can just make public variables for each form in a module and use them:
    Public FormVar1 = New Form1
    Public FormVar2 = New Form2

    That's the least you'll have to do. It's not very difficult. One line for each form. That's what VB6 did in the background to allow you to use forms without declaring them.

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2001
    Posts
    52
    To get the label property to appear I need to do the following:

    Global Frm1 as Form1 = New Form1

    Then I can go to my module and say:

    Frm1.Label1.Text = "Test"

    But when running the project it does not change the label. To get the label to show, I need to then say:

    Frm1.Show

    This means that I now have two forms showing.

  8. #8
    Tygur
    Guest
    Originally posted by mbjunior99
    This means that I now have two forms showing.
    Are you sure? I can't see how that would be. It doesn't look like you tried it, because I don't believe Global exists as a keyword in VB.NET.

    First, you'd do something like this in a module:
    Dim Frm1 As New Form1()

    ..Then you can access that variable with the form from other places. Like I said before, it's the same thing as what VB6 did in the background so you didn't have to declare forms. In VB6, every time you did something like Form1.Show or Form1.Label1.Caption, you were actually accessing a variable that VB6 automatically declared for you.

  9. #9
    New Member
    Join Date
    Apr 2002
    Location
    Brisbane
    Posts
    3

    Question

    I got a Form1 with a Label1 on it.
    I call the Module CALLModule and the code is as below.

    Imports System.Windows.Forms
    Module Module1
    Dim Frm1 As New Form1()
    Public Sub CallModule()
    Frm1.Label1.Text = "This is a test"
    End Sub
    End Module

    How ever when I look at Form1 there is no updated label with the text "This is a Test"

    But if I have

    Module Module1
    public Frm1 As New Form1()
    Public Sub CallModule()
    Frm1.Label1.Text = "This is a test"
    frm1.show
    End Sub
    End Module

    Then in addition to the the form1 i get a Frm1 based on the FORM1 but with the Label1 having the new text.

    How do I get rid of the FORM1 being recreated???

  10. #10
    Lively Member ac11965's Avatar
    Join Date
    May 2000
    Location
    East London, South Africa
    Posts
    69
    i don't want to be funny, but i think you must stick to assigning values to your controls in your form class.

    use your modules to create functions and procedures that will assign values to variables or function names (depending on which you use) and then assign these to your controls on your form...

    when you create a new instance of your form, there will be another form shown if you use the instance...

    but this is quite an interesting case...

  11. #11
    Tygur
    Guest
    Okay, could somebody make a project that is mysteriously creating a new form when it should be using one that is already there and post it? I'm not experiencing this, and I want to know what I'm missing, here.

  12. #12
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    pass the label as a parameter of the modules routine you want to call. Remember that this is a full OOP language now and objects dont persist the same way they did in VB6 and below. Every namespace/class is an object and occupies its own memory space. You need to pass the reference of an object to another object to use the same instance of it. Hope this helps.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  13. #13
    New Member
    Join Date
    Apr 2002
    Location
    Brisbane
    Posts
    3

    Figured it out.

    Thats ok guys. Im sure you all tried. Found a way to do it.

    Wasnt anything that was posted as a reply though.

  14. #14
    New Member
    Join Date
    May 2002
    Posts
    6

    Re: Figured it out.

    Originally posted by rdk
    Thats ok guys. Im sure you all tried. Found a way to do it.

    Wasnt anything that was posted as a reply though.
    That's cool and everything, we are all glad that you figured it out, but seeing as it was something that no one posted as a reply, perhaps you could share the knowledge with us?

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