Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change Label Text 1

Status
Not open for further replies.

astrodestino

IS-IT--Management
Feb 19, 2005
179
AR
Hi!
I want to change a label text in form1 from a module.
What I got

Dim lblusuarios As Form1
lblusuarios.Label9.Text = "News : " & masterrs_total

It wont return any error but it wont change labels text either.
How can I fix that?
Thank you!
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Label9.Text = "News : " & masterrs_total

End Sub

Or use the Form Load event.
 
astrodestino i use the exact same code as you to change a label.text in an app im writing. Only difference is that for me it works, and im setting the value from a form but the difference between a module and form in that example shouldnt be a problem ;)
Are you sure your code gets executed?





- - - - - - - - - - - - - - - - - -
Im three apples high, Im blue, and i most certainly like that cold beer that should be every mans right after a hard days work!
 
the problem is that the form object you declare is never instantiated.

Assuming you display the form in another method of your module, I would sugegst something like this:

Out side of the subs/functions:

private frmWhatever as new Form1

In the sub main:
frmWhatever.showmodal

in your other method:
frmWhatever.Label9.Text = "News : " & masterrs_total



When ever you want to work with an object you need to either Instantiate it (IE: dim [variable name] as NEW [type]) OR set it equal to a reference of another object (IE: dim frm2 as Form = frmWhatever)

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
cjinsocal581: I use it from a module so I can have a public sub for all my forms.

Tnx 4 the replies, what i've done is:

In the declararions of my module I added:

Public frm As New Form1

And in Public Sub totaldata() I added:

frm.Label9.Text = "sdfgd"


From a button in form1 I call totaldata I know that the line is read by the editor but label9 remains unchanged...

THe module can be the problem?
Tnx!
 
is the frm variable that you are referencing in the totaldata sub the same one that you use to display the form?

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Form1 is Class not a Module.
Try
Dim frm As New Form1
frm.Label9.Text = "1st Copy"
frm.Show
Dim frmAnother As New Form1
frmAnother.Label9.Text = "2nd Copy"
frmAnother.Show

and you will find that you have two instances of the Form1 class.

This is VB.Net. You are thinking VB6 (non OO) which allowed you treat Form1 as a single instance.


- free online Compare/Diff of snippets
 
Rick, the module looks like this:

Public frm As New Form1

Public Sub totaldata()

frm.Label9.Text = "sdfgd"

end sub

The thing John is that the form is already on the screen and I call the sub in the module from within form1.
In VB6 was simple as this:

In form1:
Private Command1_Click()
totaldata()
end sub

In the module:
Public Sub totaldata()
form1.label9.caption="My text"
end sub

If I put frm.show() in the module, wont it show an instance of that form and show me the original form1 and the instanced one at the same time?
I want to do like in vb6 change the label caption from the active form, in this case form1.
Thank you for the help!!
 
Try this:
Code:
Public Sub totaldata(frmToChange as Form1)
  frmToChange.Label9.Text = "sdfgd"
end sub

in your Form1:
Code:
Private Command1_Click()
  totaldata(Me)
end sub

This passes a reference to the frm that is calling the method to the method. That way, the method knows which object to change.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Ok John you are right, vb net changed a lot, but having form1 active and on screen if I use
Public frm As New Form1
frm.label9.text="mytext"
frm.show()

this will show 2 forms, the one that I had and the new one, what I want is to change the label text from the form that is currently on screen that I selected to be the startup form in the Project Properties Window.
Tnx!
 
Rick, it worked, thank you very much!
Now, can anyone explain me how it works this form thing?
I understand that is a class but I thought that the startup form was defined by default.
If I use form1.vb as startup form (the form that automaticaly load when you start your app) how do I change a form1 label from form2?
Thank you!
 
There's a few ways you can do it.

1. Control the flow of forms from a module. create a sub Main() in your module and set it to the startup object. control everything from there. that way, you always have a global variable you can acces.

2. Alter the constructor of the form. Add a parameter to the sub New(). use that parameter to set any values on the form when it gets insantiated.

3. Open the Form2 modally (.showModal) Use the .ParentForm to access Form1 (remeber to cast it to Form1 type if you need to access specific object on it. .ParentForm will return a type form)

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Tnx for your advice, do you know any site where I can read about vb net sintax changes from vb6 so I can practice?
I always use vb6 but for this job I must use net because nobody knows if vb6 apps are going to run in the next windows and this app must last long.
Programming is the same in both visuals but my difficulties are while I tray to use those small thing like what I was asking here, those small things drives me mad! :D
Tnx!
 
The syntax between VB6 and VB.Net is very similar, the difference is a fundamental change in the orientation of the language. VB.Net is fully object oriented, like Java or C. As far as where to get some practice stuff, just dream something up, and try to make it, use google to find samples, and ask specifics/guidance here.

As far as the future of .Net, from everything I've read, MS is going to drop support of it. That means, no more patches or updates. But Longhorn should support GDI, COM, and the Win 32 API. Although they'll be secondary to Avalon and .Net. I wouldn't panic and try to convery everything to .Net (alla 2k date issue), but I wouldn't recommend continuing to develop in VB6.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
If dropping NET what are they going to come with? :S
Tnx again for the help!
 
woops, sorry, that what I get for posting while distracted. That should read: "As far as the future of VB6, from everything I've read, MS is going to drop support of it."

VB6 support is ending, .Net will be arround atleast through Longhorn.

-Rick

----------------------

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top