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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class-Class reference-ing

Status
Not open for further replies.

medicenpringles

Programmer
Aug 7, 2005
39
US
I have an application with a running total featured on all of the forms. when certain controls are interacted with on any of the forms, i need the total to be updated. this i can do. but showing the user that the total has been changed is the problem. he's the code i'm having trouble with:

Code:
' This is in frmSecond
Sub Button1_Click(parameters, blah blah..)
   
   Dim f as New frmSecond
   Dim m as New frmMain
   
   Globals.number += 1
   f.lblTotal = Globals.number.ToString
   m.lblTotal = Globals.number.ToString  ' This does not happen

End Sub
 
Is your startup object frmMain? If so, when using global forms I use as Sub Main as my startup object - use my global frmMain variable and instantiate it (f = new frmMain) and then use f.ShowDialog.

The difference is the memory address again. Just creating the global form variable isn't enough. If your not using it to display the form, it won't update. Like having the form as the startup object, then you have two forms of frmMain in memory.
 
ok, i worked with it last night and my only problem was trying to change the contorol while showing another modal form. i moved the actions into the native forms Activate event, and it worked fine. sorry about all the fuss

it just makes me mad that i absolutely have to have the form active to make any visual changes to it.

Main Language: Visual Basic .NET
Development Enviroment: Visual Studio .NET 2003
 
You don't need the form active to make visual changes - just a valid reference to it.

I have this in Form1 - it has a label and a button:
Code:
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        f2 = New Form2
        f2.ShowDialog(Me)
    End Sub
This is in Form2 - same components:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = "Changed"
        f1.Label1.Text = "Changed"
    End Sub
Here is my module:
Code:
Module Module1
    Public Sub Main()
        f1 = New Form1
        f1.ShowDialog()
    End Sub
    Public f1 As Form1
    Public f2 As Form2
End Module
From the Project menu goto the Properties for the project and set the startup project to Sub Main().

This will cause Sub Main to run first using the global variable f1 to display the first form - thus allowing Form2 (when it is shown) to change the label on Form1 - even though it isn't active.

I just tried it and it works.
 
medicenpringles, Have a look at thread796-1095685, this shows how to set the state of checkboxes on one form based on the state of checkboxes on another.

Rick, I know, I was just trying to make senses of chip's comment

Hopee this helps.
 
What do you suggest instead of Global variables - or are you implying Public variables in a Module = bad and Public Shared members (variables) in a Public Class = good (or at least ok)?
I'm not either of us want to get into this, seeing as how you've already written a fair amount of code...

But what I would do is create an Order object. It would hold your OrderTotal dollar total as a property, plus the currency the total is in (dollars vs. pesos), as well as a strongly-typed collection of OrderItem objects. Each OrderItem object would have the name & description of the item, the price and quantity, and other relevant info.

I would create a controller class that would hold the Order object, plus anything else needed for the form to display to the user. The form itself would be very thin -- just what's needed to show the data to the user (formatting the OrderTotal property with the currency specified, converting DateTime values to the user's preferred format, etc).

The controller class would contain most of the logic needed to manage the form, and call down into lower layers to create/retrieve/update or delete Orders and OrderItems.

The advantages of doing it this way are:
1) It decouples presentation of the data from how it's stored and how business rules are enforced.
2) You're using objects, and not procedural code.
3) You gain better reuse, as code is contained with the data that it manages.
4) You can brag to other programmers that you're using the Model-View-Controller design pattern.

Hope this gets you interested in doing this. Like I said, you probably won't for this project, but maybe for the next one...

Chip H.

____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
I like 4 ;-) and i'll start practising tommorow

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top