PDA

Click to See Complete Forum and Search --> : Changing a label from a module


mbjunior99
Apr 29th, 2002, 06:27 PM
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?

abdul
Apr 29th, 2002, 06:56 PM
How about
form1.Conrols("label").Caption = "test"

mbjunior99
Apr 29th, 2002, 07:33 PM
That doesn't work. Any other ideas?

Tygur
Apr 29th, 2002, 08:00 PM
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"

mbjunior99
Apr 29th, 2002, 08:25 PM
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?

Tygur
Apr 29th, 2002, 08:43 PM
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.

mbjunior99
Apr 30th, 2002, 12:02 AM
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.

Tygur
Apr 30th, 2002, 02:20 AM
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.

rdk
Apr 30th, 2002, 03:17 AM
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???

ac11965
Apr 30th, 2002, 06:28 AM
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...

Tygur
Apr 30th, 2002, 09:54 AM
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.

Cander
Apr 30th, 2002, 10:17 AM
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.

rdk
May 1st, 2002, 03:48 AM
Thats ok guys. Im sure you all tried. Found a way to do it.

Wasnt anything that was posted as a reply though.

ioslipstream
May 1st, 2002, 08:49 PM
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? :D