Solved

setting property on a form from a class

Posted on 2004-04-29
9
342 Views
Last Modified: 2010-04-24
I've searched the archive here, but stil am a little confused. I uderstand the concept of OO to some degree, but isn't "text" a property of a label? I have a form that is currently open and active. I drop into a class to do some work, but want to update the status of the work in a label on the form. Shouldn't I just be able to set the property of label.text on the form to what I want without all that other code? It looks like a lot of work to simply set a property.

In short, my question is how to set the text property of a label on an active form with a value, from within another class, without 25 lines of code. For the purists of you out there, I realize the old method may not have been true OO, but darn if it wasn't simple.
0
Comment
Question by:thefunnydad
9 Comments
 
LVL 21

Expert Comment

by:tovvenki
ID: 10947744
Hi,
if you have an instance of the form in the class then you can just set the text property like

forminstance.Label1.Text = "some value"

hope that this helps you.

Regards,
Venki
0
 

Author Comment

by:thefunnydad
ID: 10947785
Well, that seems to be where I am in new territory; how to set the instance properly. I have tried the ways I am familiar with, but I always have to end up declaring it as "new" and I don't need a new instance, I need to reference the one already created. Could you throw me the bone on that then; how to set an instance to a form/class already active?

Thanks.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 10947788
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 21

Expert Comment

by:tovvenki
ID: 10947910
Hi,

try this
in class say Class1

Public Class Class1
    Dim parent As Form1
    Public Sub New(ByRef frm As Form1)
        parent = frm
        MyMethod()
    End Sub

    Public Sub MyMethod()
        parent.Label1.Text = "MyText"
    End Sub


End Class

in form1 (say in a button click event) use

        Dim cls As Class1
        cls = New Class1(Me)

I Hope that this helps you.

Regards,
Venki
0
 

Author Comment

by:thefunnydad
ID: 10948226
Color me stupid; based upon the link here is what I did:

I added the following the the start-up form load event:

MainForm = Me


Then in the top of the class, I Added:

Public MainForm as {start-up form}

In the procedure, I added:

MainForm = New {start-up form}

finally, I tried:

MainForm.Label.text = "abc"

Doesn't show on the form.

Can I make something so easy hard or what?
0
 
LVL 21

Accepted Solution

by:
tovvenki earned 500 total points
ID: 10948530
Hi,
 I assume that the procedure that you are mentioning here
"In the procedure, I added:

MainForm = New {start-up form}"

is in the class if yes change it to
Public Sub MyMethod(ByRef frm As Form1)
        MainForm = frm
    End Sub

now you can try from any method in the class and set the label text like

MainForm.Label.text = "abc"

when you say
"In the procedure, I added:

MainForm = New {start-up form} "
this will create a new instance of the start-up form and not the one that you are using i.e the Active form. If you want to use the active form then you need to pass a reference of the active form to the class assign that instance to a variable in the class and then you can manipulate the active form from within your class.

Hope that this helps you.
Regards,
Venki




0
 

Author Comment

by:thefunnydad
ID: 10948692
OK, I was able to get it to work, but blending what you gave me with what I had. What I ended up doing was this

I was calling a function in the class from within the form, so I added ByRef frm As Form1 to the list of incoming params expected in the function, then when I made the call, passed me. This did it wthout having to set any other references in the class.


Form1
Public Sub Button1_click
class1.RunReport(param,param,me)
end sub

Class1

Public Function RunReport (Byval param as string, ByVal param as string, byRef MainForm as Form1) as boolean

MainForm1.label.text = "abc"

RunReport = True

end function
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 10948732
The difference between VB and VB.NET is that VB was implicitly creating a form object for you...

You really had a "form1" Object which was an instance of the "Form1" Class... Thats what allowed you to use

Form1.lable1.caption = "Hello World"
(this was basically cheating)

Now in VB.NET you have a "scope" for every variable... This includes your forms, your classes and everything.

So basically your "class" only knows whats below it or which objects are explicitly made known.

In your example try

MainForm.Label.text = "abc"
MainForm.show

This should most likely pop up a 2nd Form which is another instance of your Startup form. In reality you made a new form which is (still) invisible and needs to be shown
0
 

Author Comment

by:thefunnydad
ID: 10948754
And to make it ALL moot; the function I wss calling from within the class was work to be done as a result of a click on the main form. SO, set the text property for the label on the form to "abc" BEFORE calling the class, then reset it to "whatever" when you return and the work is complete. {sigh} sometimes the freakin trees get in the way.


Thanks all
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question