Results 1 to 27 of 27

Thread: How to display a value over several forms?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    How to display a value over several forms?

    This may seems to be an easy task but need help. I have more than 2 forms in my programs.

    Example.
    User input $1300 as basic salary.

    How do I store this value that user have inputted and have it displayed in other forms?

    Thanks!

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: How to display a value over several forms?

    Someone else asked the same thing the other day, maybe you can get some help from the suggestions in their thread?
    http://www.vbforums.com/showthread.php?t=532399
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: How to display a value over several forms?

    Quote Originally Posted by melvados
    I have more than 2 forms in my programs.
    Either one of 2 options spring to mind initially. Perhaps the tidiest route would be the following:

    Create a base class inheriting from System.Windows.Forms.Form (that's from memory, you might need to check that namespace), then inherit all your forms from this. Once your money value is retreived, set a property of this base class with the value. You can they use myBase.PropertyName from any of your forms which need the value.

    However a second option if this is just being displayed statically throughout the lifetime of the other forms, from their creation, could be to overload the constructor of the forms being created, in order to allow them to accept this value as a parameter then set their label with this value. It really depends upon the lifecycle of your forms, and the value to be displayed upon them.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    UK
    Posts
    285

    Re: How to display a value over several forms?

    To share information across classes, use a module. You can create a new module file, or just declare a module in one of your existing classes:
    vb.net Code:
    1. Module Module1
    2.     Public basicSalary As Double
    3. End Module
    In one form, you can set the basicSalary variable:
    vb.net Code:
    1. Public Class Form1
    2.         Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    3.             basicSalary = 1300.0
    4.             Dim f As New Form2
    5.             f.Show(Me)
    6.         End Sub
    7. End Class
    and in the other form, or forms, you can use the variable:
    vb.net Code:
    1. Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2.         Text = basicSalary.ToString
    3. End Sub
    I've been known to be wrong ...

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    Re: How to display a value over several forms?

    Hi bigmeup, about the 'basicSalary' in the second code you wrote, do you mean the text box for it in my form? I do not quite understand from it....

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    Re: How to display a value over several forms?

    Erm, by the way, I am not sharing information across classes. I am trying to make the info to be shared over the windows form.

    Right now, I am still learning about visual studio so still had not gotten a very in-depth details over class, modules etc etc.

    Correct me if i'm wrong, thanks!

  7. #7
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: How to display a value over several forms?

    What bigMeUp has done, is to declare a public variable. This means the variable is accessible to all code files within the project.

    By placing this within in a module file, it means you can call ModuleName.PublicVariable whenever you need it from any of your forms to retreive or set this value. He's presented you with samples of both here.

    Using modules and public variables in this manner is valid, and provides you a means of accessing variables without instanciating the code module, it is similar to writing code in VB5 or VB6. You do not take advantage of the powerful Object Oriented Programming features which the Visual Basic language now has in .Net, and for this reason most professional programmers would write this in one of the ways I mention above.

    For more reading on this, I suggest you:
    1. Lookup "variable scope" on MSDN
    2. Review this thread describing the differences between modules and classes http://www.vbforums.com/showthread.p...odules+classes

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    Re: How to display a value over several forms?

    Alright. But I had like to clarify something, that is the example that bigMeUp have is for the sharing of into between classes. But will this be possible, as currently I do not have a class but windows form...

    So do i need to create one out?

  9. #9
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: How to display a value over several forms?

    Double click your form from the designer, and scroll right to the top of the code window which appears. It will say similar to:
    Code:
    Private Class Form1
    All forms are classes. Full stop. When you design a form, Visual Studio is reading the code of the class relating to controls and their properties (texterual content, position upon the form, their size and colour etc.) and making this available in a nuice graphical format for you. The form isn't that GUI representation, it's a simple class of coding.

    In answer to your question, yes it will work ok - just try it.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  10. #10
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: How to display a value over several forms?

    You can have a shared variable in the main form and a shared property through which you can access the basic salary.
    Here is an example in the main form:
    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Shared me_basicSalary As Decimal
    4.  
    5.     Public Shared Property BasicSalary() As Decimal
    6.         Get
    7.             Return Form1.me_basicSalary
    8.         End Get
    9.         Set(ByVal value As Decimal)
    10.             Form1.me_basicSalary = value
    11.         End Set
    12.     End Property
    13.  
    14. End Class
    And this is how you can access the value from the other forms:
    vb Code:
    1. Me.Label1.Text = Form1.BasicSalary.ToString

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    Re: How to display a value over several forms?

    i'm a bit confused here. The basicSalary in the above sample codes refer to the textbox where the user input right?

    Then may I ask, the code VBDT wrote, 'Public Shared Property BasicSalary() As Decimal' and 'Me.Label1.Text = Form1.BasicSalary.ToString ', why isn't BasicSalary be basicSalary?

    Is this a typo or it should be in this manner....?

    Please direct me

  12. #12
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Smile Re: How to display a value over several forms?

    I think your first port of call should be to look on MSDN and researc what a property of a class is, how it works and it's advantages. This will help you understand all the above suggestions in more detail/with a greater knowledge for them to make sense.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  13. #13
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: How to display a value over several forms?

    Quote Originally Posted by melvados
    i'm a bit confused here. The basicSalary in the above sample codes refer to the textbox where the user input right?

    Then may I ask, the code VBDT wrote, 'Public Shared Property BasicSalary() As Decimal' and 'Me.Label1.Text = Form1.BasicSalary.ToString ', why isn't BasicSalary be basicSalary?

    Is this a typo or it should be in this manner....?

    Please direct me
    You may have ‘basicSalary’ textbox that the user enters a value but you also need to convert that value to decimal so that you can do some calculations with it. Even if your app does not do any calculation with this entry, it is good programming to have a variable that holds the value that is in the textbox. There for the shard variable ‘me_basicSalary’ will hold that value. The ‘BasicSalary’ property is a good way of accessing the variable. In this case it should be shared or not shared if you want to access this value through the default main form instance (for VS. 2005 and above). I hope you understand my explanation.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    Re: How to display a value over several forms?

    I have try the code. But whatever numbers I have inputted in the basicSalary box, in my form2, the textbox/label value shown is 0. why?

    Form1 code
    vb Code:
    1. Public Class Form1
    2.     Private Shared me_basicSalary As Decimal
    3.  
    4.     Public Shared Property BasicsSalary() As Decimal
    5.         Get
    6.             Return Form1.me_basicSalary
    7.         End Get
    8.         Set(ByVal value As Decimal)
    9.             Form1.me_basicSalary = value
    10.         End Set
    11.     End Property
    12.  
    13.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    14.         Dim f As New Form2
    15.         f.Show(Me)
    16.     End Sub
    17. End Class

    Form2 code:
    vb Code:
    1. Public Class Form2
    2.  
    3.     Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Me.Label1.Text = Form1.BasicsSalary.ToString
    5.     End Sub
    6. End Class

  15. #15
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: How to display a value over several forms?

    In the code you provided I don't see the 'basicSalary ' textbox.

  16. #16
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    UK
    Posts
    285

    Re: How to display a value over several forms?

    Quote Originally Posted by melvados
    i'm a bit confused here. The basicSalary in the above sample codes refer to the textbox where the user input right?

    Then may I ask, the code VBDT wrote, 'Public Shared Property BasicSalary() As Decimal' and 'Me.Label1.Text = Form1.BasicSalary.ToString ', why isn't BasicSalary be basicSalary?

    Is this a typo or it should be in this manner....?

    Please direct me
    Try the following code:
    vb.net Code:
    1. Option Strict Off
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, _
    6.         ByVal e As System.EventArgs) Handles Button1.Click
    7.  
    8.         Dim b As Double = 12.12
    9.         TextBox1.Text = b
    10.  
    11.     End Sub
    12. End Class
    This code will run without any problems.

    Now try this code:
    VB.NET Code:
    1. Option Strict ON
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, _
    6.         ByVal e As System.EventArgs) Handles Button1.Click
    7.  
    8.         Dim b As Double = 12.12
    9.         TextBox1.Text = b
    10.  
    11.     End Sub
    12. End Class
    You will notice that this code won't run. To make sure that Option Strict is always set to `ON`, please click the TOOLS menu item in Visual Studio. Select OPTIONS at or near the bottom of the screen. In the treeview control to the left of screen, open PROJECTS AND SOLUTIONS. Choose VB DEFAULTS. On the right, you will see a set of drop-down boxes under the heading, DEFAULT PROJECT SETTINGS. Please make sure that OPTION STRICT is set to ON, then click OK. This sets the compiler so that explicit narrowing and late-binding is not allowed.

    I have try the code. But whatever numbers I have inputted in the basicSalary box, in my form2, the textbox/label value shown is 0. why?
    Because you have not set the variable!

    Try this:
    vb.net Code:
    1. Public Class Form1
    2.     Private Shared me_basicSalary As Double
    3.     Public Shared Property BasicsSalary() As Double
    4.         Get
    5.             Return Form1.me_basicSalary
    6.         End Get
    7.         Set(ByVal value As Double)
    8.             Form1.me_basicSalary = value
    9.         End Set
    10.     End Property
    11.  
    12.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    13.         BasicsSalary = 1300.0
    14.  
    15.         Dim f As New Form2
    16.         f.Show(Me)
    17.     End Sub
    18. End Class
    Line 13 sets the variable and shows 1300.0 in Form2's textbox.
    I've been known to be wrong ...

  17. #17
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: How to display a value over several forms?

    Quote Originally Posted by bigMeUp
    Try this:
    vb.net Code:
    1. Public Class Form1
    2.     Private Shared me_basicSalary As Double
    3.     Public Shared Property BasicsSalary() As Double
    4.         Get
    5.             Return Form1.me_basicSalary
    6.         End Get
    7.         Set(ByVal value As Double)
    8.             Form1.me_basicSalary = value
    9.         End Set
    10.     End Property
    11.  
    12.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    13.         BasicsSalary = 1300.0
    14.  
    15.         Dim f As New Form2
    16.         f.Show(Me)
    17.     End Sub
    18. End Class
    Line 13 sets the variable and shows 1300.0 in Form2's textbox.
    According to MSDN for the financial calculations a Decimal type should be used not Double.

    MSDN
    "The Decimal data type provides the greatest number of significant digits for a number. It supports up to 29 significant digits and can represent values in excess of 7.9228 x 10^28. It is particularly suitable for calculations, such as financial, that require a large number of digits but cannot tolerate rounding errors."

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    Re: How to display a value over several forms?

    Hi there
    I have tried putting the code in post 16 as shown below :
    vb Code:
    1. Option Strict On
    2.  
    3. Public Class [U]Form1[/U]
    4.     Private Sub [U]Button1_Click[/U](ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         Dim b As Double = 12.12
    6.         TextBox1.Text = b
    7.     End Sub
    8. End Class
    9.  
    10. Public Class [U]Form1[/U]
    11.     Private Shared me_basicSalary As Double
    12.     Public Shared Property BasicsSalary() As Double
    13.         Get
    14.             Return Form1.me_basicSalary
    15.         End Get
    16.         Set(ByVal value As Double)
    17.             Form1.me_basicSalary = value
    18.         End Set
    19.     End Property
    20.  
    21.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    22.         BasicsSalary = 1300.0
    23.         Dim f As New Form2
    24.         f.Show(Me)
    25.     End Sub
    26. End Class

    Now, the form 1 I have cannot be shown as it states that 'The type Form1 is made of several partial classes in the same file'. In the above, there are a few parts underlined as present in the codes.

    By the way, there is a textbox and a button in form 1 and a textbox in form 2.

    Just like to know, why the code BigMeUp in post 16 can put up 2 public classes in a form?

  19. #19
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: How to display a value over several forms?

    Quote Originally Posted by melvados
    Now, the form 1 I have cannot be shown as it states that 'The type Form1 is made of several partial classes in the same file'. In the above, there are a few parts underlined as present in the codes.

    Just like to know, why the code BigMeUp in post 16 can put up 2 public classes in a form?
    You can split classes to be contained within multiple files if you declare them with the "Partial" keyword. However, to use this, the multiple definitions (class...end class codings) must be contained within different files.

    Tell me, will your form1 be visible throughout the entire length of your application's runtime? If you use form1 to set your monetary figure, then open forms 2 and 3 which want to read this value, will form1 always be open at this time or might it be closed prior to forms 2 and 3 reading the value please?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    Re: How to display a value over several forms?

    Form one will not be present throughout. It will pnly be present when user wants to do something with it.
    Cause i will not only be displaying monetary figures but numbers too (as I am doing a product inventory prog)

    And so, i would say that form 1 will be closed after user have inputted the needed values before it can go to other forms.

  21. #21
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: How to display a value over several forms?

    Right then, here's one way of doing this. Just follow the below:
    1. Create a new Windows application project
    2. Add 2 new forms, leaving their default, suggested names when creating them
    3. Add another form named MainForm
    4. Add a class named MyBaseForm
    5. Right click the project from the Solution Explorer Window (hit CTRL+R if you can't see this) and choose properties. From the initially shown screen, select the startup form to be MainForm
    6. Add 3 buttons to MainForm. Again, as with the form creation, leave the default, suggested name properites for these buttons as they are
    7. Copy the following code into the relevant class files (right click each form within the solution explorer window and choose the View Code option)
    8. Still using the solution explorer window, click the Show All Files toolbar button if this does not already appear sunken
    9. Expand each form apart from MyBaseForm, and double click the associated [FormName].Designer.vb file. At the top of the file, change the Inherits statement to read Inherits MyBaseForm (rather than system.windows.forms.form)
    10. Run and test and see how this code works

    Code:
    Public Class MyBaseForm
    Inherits System.Windows.Forms.Form
    
    Protected Shared _basicSalary As Decimal
    
    Protected Property BasicSalary() As Decimal
        Get
            Return _basicSalary
        End Get
        Set(ByVal value As Decimal)
            _basicSalary = value
        End Set
    End Property
    
    End Class
    Code:
    Public Class MainForm
    
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Dim form1Instance As New Form1()
        form1Instance.Show()
    End Sub
    
    Private Sub Button2_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button2.Click
        Dim form2Instance As New Form2()
        form2Instance.Show()
    End Sub
    
    Private Sub Button3_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button3.Click
        Dim form3Instance As New Form3()
        form3Instance.Show()
    End Sub
    
    End Class
    Code:
    Public Class Form1
    
    Private Sub Form1_Shown(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.Shown
        MyBase.BasicSalary = 1300
        Me.Dispose()
    End Sub
    
    End Class
    Code:
    Public Class Form2
    
    Private Sub Form2_Shown(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.Shown
        MessageBox.Show(MyBase.BasicSalary.ToString)
        Me.Dispose()
    End Sub
    
    End Class
    Code:
    Public Class Form3
    
    Private Sub Form3_Shown(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.Shown
        MessageBox.Show(MyBase.BasicSalary.ToString)
        Me.Dispose()
    End Sub
    
    End Class
    Last edited by alex_read; Jul 29th, 2008 at 04:59 AM.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    Re: How to display a value over several forms?

    Well I have run it. Only button 1 not able to display out the value. There is like a fast window flickering and nothing pops up
    The other 2 button works fine
    Last edited by melvados; Jul 29th, 2008 at 04:50 AM.

  23. #23
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: How to display a value over several forms?

    This will either be because you are using Visual Studio 2003, or because you haven't performed the step "Still using the solution explorer window, click the Show All Files toolbar button if this does not already appear sunken".

    The error messages you are experiencing are due to this step 9.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    Re: How to display a value over several forms?

    I think I know where is the problem. There is a typo in here :
    Code:
    Public Class Form1
    
    Private Sub Form2_Shown(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles Me.Shown
        MyBase.BasicSalary = 1300
        Me.Dispose()
    End Sub
    
    End Class
    Should be Form1, not Form2

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    May 2008
    Posts
    232

    Re: How to display a value over several forms?

    Anyway, will this be similar if I used textbox to input values instead with buttons?

    Meaning to say in form1, I input a value and hit btn1 then the value will be displayed in form 2 and 3. Still possible?

  26. #26
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    Re: How to display a value over several forms?

    The typo has been ammended above. The method name is purely a textural label and has no bearing on the functionality here (think of this as a variable name which has no effect on the type). It's the handles clause of the event which is evaluated so the typo has no effect. I've tried and tested this for you and the code posted there works ok.

    This is a basic sample. Once you see and understand how it works then yes you can customise it to your hearts content, including writing the value from a textbox or other button click/event, and reading the value onto something more useful than a messagebox, such as a form label etc. - anything you like.

    Are you using VB2005 or 2008 here? can you see the "Show All Files" button at the top of the Solution Explorer window ok?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  27. #27
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    UK
    Posts
    285

    Re: How to display a value over several forms?

    Quote Originally Posted by melvados
    Hi there
    I have tried putting the code in post 16 as shown below :
    vb Code:
    1. Option Strict On
    2.  
    3. Public Class [U]Form1[/U]
    4.     Private Sub [U]Button1_Click[/U](ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         Dim b As Double = 12.12
    6.         TextBox1.Text = b
    7.     End Sub
    8. End Class
    9.  
    10. Public Class [U]Form1[/U]
    11.     Private Shared me_basicSalary As Double
    12.     Public Shared Property BasicsSalary() As Double
    13.         Get
    14.             Return Form1.me_basicSalary
    15.         End Get
    16.         Set(ByVal value As Double)
    17.             Form1.me_basicSalary = value
    18.         End Set
    19.     End Property
    20.  
    21.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    22.         BasicsSalary = 1300.0
    23.         Dim f As New Form2
    24.         f.Show(Me)
    25.     End Sub
    26. End Class

    Now, the form 1 I have cannot be shown as it states that 'The type Form1 is made of several partial classes in the same file'. In the above, there are a few parts underlined as present in the codes.

    By the way, there is a textbox and a button in form 1 and a textbox in form 2.

    Just like to know, why the code BigMeUp in post 16 can put up 2 public classes in a form?
    You can put several public classes into a form. That is not the problem. The problem is that you are defining the same class twice on line 3, and line 10. Lines 3 to 8 are completely unnecessary, and should be deleted. This is not part of the problem, but why is your form called Form1 anyway? I mean, why the and ?

    How do you post a literal in this freaking thing anyway? It was '['u']'Form1'['/u']' and '['u']' and '['/u']' when in my browser!
    I've been known to be wrong ...

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