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!
Printable View
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!
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
Either one of 2 options spring to mind initially. Perhaps the tidiest route would be the following:Quote:
Originally Posted by melvados
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.
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:
In one form, you can set the basicSalary variable:vb.net Code:
Module Module1 Public basicSalary As Double End Module
and in the other form, or forms, you can use the variable:vb.net Code:
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click basicSalary = 1300.0 Dim f As New Form2 f.Show(Me) End Sub End Class
vb.net Code:
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Text = basicSalary.ToString End Sub
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....
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!
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:
- Lookup "variable scope" on MSDN
- Review this thread describing the differences between modules and classes http://www.vbforums.com/showthread.p...odules+classes
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?
Double click your form from the designer, and scroll right to the top of the code window which appears. It will say similar to:
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.Code:Private Class Form1
In answer to your question, yes it will work ok - just try it.
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:
And this is how you can access the value from the other forms:vb Code:
Public Class Form1 Private Shared me_basicSalary As Decimal Public Shared Property BasicSalary() As Decimal Get Return Form1.me_basicSalary End Get Set(ByVal value As Decimal) Form1.me_basicSalary = value End Set End Property End Class
vb Code:
Me.Label1.Text = Form1.BasicSalary.ToString
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
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.
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.Quote:
Originally Posted by melvados
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:
Public Class Form1 Private Shared me_basicSalary As Decimal Public Shared Property BasicsSalary() As Decimal Get Return Form1.me_basicSalary End Get Set(ByVal value As Decimal) Form1.me_basicSalary = value End Set End Property Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim f As New Form2 f.Show(Me) End Sub End Class
Form2 code:
vb Code:
Public Class Form2 Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Label1.Text = Form1.BasicsSalary.ToString End Sub End Class
In the code you provided I don't see the 'basicSalary ' textbox.
Try the following code:Quote:
Originally Posted by melvados
This code will run without any problems.vb.net Code:
Option Strict Off Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim b As Double = 12.12 TextBox1.Text = b End Sub End Class
Now try this code: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.VB.NET Code:
Option Strict ON Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim b As Double = 12.12 TextBox1.Text = b End Sub End Class
Because you have not set the variable!Quote:
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?
Try this:Line 13 sets the variable and shows 1300.0 in Form2's textbox.vb.net Code:
Public Class Form1 Private Shared me_basicSalary As Double Public Shared Property BasicsSalary() As Double Get Return Form1.me_basicSalary End Get Set(ByVal value As Double) Form1.me_basicSalary = value End Set End Property Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click BasicsSalary = 1300.0 Dim f As New Form2 f.Show(Me) End Sub End Class
According to MSDN for the financial calculations a Decimal type should be used not Double.Quote:
Originally Posted by bigMeUp
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."
Hi there
I have tried putting the code in post 16 as shown below :
vb Code:
Option Strict On Public Class [U]Form1[/U] Private Sub [U]Button1_Click[/U](ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim b As Double = 12.12 TextBox1.Text = b End Sub End Class Public Class [U]Form1[/U] Private Shared me_basicSalary As Double Public Shared Property BasicsSalary() As Double Get Return Form1.me_basicSalary End Get Set(ByVal value As Double) Form1.me_basicSalary = value End Set End Property Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click BasicsSalary = 1300.0 Dim f As New Form2 f.Show(Me) End Sub 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 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.Quote:
Originally Posted by melvados
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?
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.
Right then, here's one way of doing this. Just follow the below:
- Create a new Windows application project
- Add 2 new forms, leaving their default, suggested names when creating them
- Add another form named MainForm
- Add a class named MyBaseForm
- 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
- Add 3 buttons to MainForm. Again, as with the form creation, leave the default, suggested name properites for these buttons as they are
- Copy the following code into the relevant class files (right click each form within the solution explorer window and choose the View Code option)
- Still using the solution explorer window, click the Show All Files toolbar button if this does not already appear sunken
- 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)
- 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
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
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.
I think I know where is the problem. There is a typo in here :
Should be Form1, not Form2Code: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
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?
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?
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 ?Quote:
Originally Posted by melvados
How do you post a literal in this freaking thing anyway? It was '['u']'Form1'['/u']' and '['u']' and '['/u']' when in my browser!