|
-
Aug 2nd, 2010, 02:07 AM
#1
Thread Starter
Lively Member
[RESOLVED] use from variant in Form1 in Form2
Hello everyone,
I have a simple problem and I believe that the solution is with module using but I could not resolve it yet. Here is the scene:
In Form1 I have a variant that gets its value from the selections of a listbox. The code is:
Code:
Dim lstcoptext As String = lstCOP.GetItemText(lstCOP.SelectedItem)
I would like to use the same variant (lstCOPtext) in Form2 because this selection from Listbox (in Form 1) should also trigger some events in Form 2. I tried to use a module and declare the variant using Public. I did this and I now can use the variant in Form 2. But the value selected in Form1 does not come to Form 2.
How can I do it?
-
Aug 2nd, 2010, 02:29 AM
#2
Re: use from variant in Form1 in Form2
First up, you mean "variable", not "variant".
As for the question, if you have declared a public variable in a module then it is accessible throughout the project. If you set it in Form1 then the new value will definitely be visible in Form2, but you have to actually get it in Form2. If you've assigned the old value from the module to a variable in Form2, that variable will be unaffected. You would have to actually get the new value explicitly. The thing is, you won't actually know when you need to get a new value.
The best way to achieve your aim depends on your actual aim, which we don't actually know. What exactly are you trying to achieve in Form2? What exactly is the relationship between these two forms? Does one open the other? Does it use Show or ShowDialog?
-
Aug 2nd, 2010, 03:01 AM
#3
Re: use from variant in Form1 in Form2
Some short introduction to variable scopes.
Variables declared in a module:
1. A variable with Public modifier, declared in a module can be seen throughout the whole solution and even outside it.
2. A variable with Friend modifier is visible only within your application.
You can access these variables simply by providing their names.
Variables declared in a class (class level variables):
1. A Public variable is seen wherever this class is visible and it's accessed using InstanceName.VariableName scheme (for instance variables) or by ClassName.VariableName (for shared variables).
Shared variable means that all instances of this class share a common variable which can exist even if no instances of this class exist.
2. A Protected variable can only be accesses from the within the class and its inheritors. It cannot be accessed from outside the class.
To distinguish between the base class variable and the inherited class variable you can use MyClass or MyBase keywords, that is MyBase.VariableName will refer to the member of the base class while MyClass will refer to the member of the inheritor. Me keyword is used to refer to the member of this particular instance.
3. A Private variable is, as its access modifier name suggests, private. This means that this variable can only be accessed from within the class it's been declared and nowhere else. Private variables are used to incapsulate the inner logic variables or property values from any outside influence.
Variables declared in a procedure or a function (procedure level variables):
Such variables are declared using Dim keyword and are declared within a Sub, a Property or a Function.
They can be used to store the intermediate results and cease to exist as soon the sub or function end.
In some cases you will need to keep the value of a procedure level variable and in this case you can declare such variables using Static keyword. Such variables will keep their values between two (or more) calls to this procedure of function.
Block level variables.
Similar to the procedure level variables, but they are declared within a block (IF ... END IF, DO ... LOOP, FOR... NEXT, TRY ... CATCH, etc). Such variables are also declared with Dim (or Static) keywords and only accessible within the block they declared in.
All together:
vb Code:
Module GlobalModule
' Module level
Public GlobalX As Integer ' This is visible from everywhere
Friend A As String ' This is visible only from within this application
Public SimpleX As Integer = 1 ' Also visible from everywhere
Public Class SampleClass ' This class is also visible from everywhere
' Class level
Public Field As Integer = 0 ' You can refer to it using InstanceName.Field (where InstanceName
' is the name of the SampleClass instance
Private Shared SimpleX As Integer = 2' Within this class SimpleX will Shadow the SimpleX
' which is declared above (at the module level)
' every reference to SimpleX from this class will be evaluated to 2, not 1
' Also this variable is Shared so it is accessible using SimpleClass.SampleX syntax
Private X As Integer = 5 ' A private instance member which is only accessible from this class
Protected Y As Integer = 5 ' Similar to private but can be accessible from an inherited class
Sub Method1
' Procedure Level
Dim X As Integer = 10 ' This X will shadow the X declared at the class level so here X = 10, not 5
IF X = 10 Then
' Block level
Dim Z As Integer = X + 5 ' At the block level Z = 15
End If
' Z declared above is not visible here already (outside the If block).
' The block ended and I can declare here a new Z:
Dim Z As Integer = Me.X - 5 ' Since I used Me keyword Me.X = 5 and thus Z here = 0
End Sub
End Class
End Module
Last edited by cicatrix; Aug 2nd, 2010 at 03:05 AM.
-
Aug 2nd, 2010, 03:20 AM
#4
Thread Starter
Lively Member
Re: use from variant in Form1 in Form2
Thanks for reply and also correction. I am learning 
My actual aim is to select a value from a Listbox (lstCOP) in Form 1 and in Form 2 display a pdf file with the same name which is already available in the resources. The variable (which is lstCOPtext) should get its value from the selection in Form 1 and In Form 2 I have now the below code:
Code:
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myResourceName As String = lstCOPtext.Replace(".", "_")
Dim myTempFileName As String = IO.Path.Combine(IO.Path.GetTempPath, lstCOPtext)
My.Computer.FileSystem.WriteAllBytes(myTempFileName, CType(My.Resources.ResourceManager.GetObject(myResourceName), Byte()), False)
If IO.File.Exists(myTempFileName) Then
lstCOPtext.LoadFile(myTempFileName)
End If
End Sub
End Class
I am not sure yet if the above code will work (probably it needs revision) but first I should get the lstCOPtext value to Form 2.
Yes, one form open the other showdialog.
-
Aug 2nd, 2010, 07:14 AM
#5
Thread Starter
Lively Member
Re: use from variant in Form1 in Form2
thank you very much Cicatrix, I understand now better the defferencaes between different declerations...
But I cannot manage to get the requested data from Form 1 to Form 2. I declare it in module using "Public" and thus I can use it in Form 2. But I cannot manage to get or use its value in Form 2. For example, I put a new textbox and bind it to this variable as textbox1.text, I cannot see its value... But if I make it in form 1, I can use it as I want. jmcilhinney told me that I should actually get the new value explicitly but I could not make it so far...
-
Aug 2nd, 2010, 07:23 AM
#6
Re: use from variant in Form1 in Form2
Show your code. It's difficult to think of a million of things that you might be doing wrong. Seeing your code will help enormously to isolate a problem. Don't post the whole code, just the portion where you are trying to get a value from that textbox.
-
Aug 2nd, 2010, 07:30 AM
#7
Thread Starter
Lively Member
Re: use from variant in Form1 in Form2
I sent the hole code as it was not very long and I used the respected variable in a fwe places (not only one)...
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim lstcoptext As String = lstCOP.GetItemText(lstCOP.SelectedItem)
End Sub
Private Sub radOnSurface_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radOnSurface.CheckedChanged
COPsecim()
End Sub
Private Sub radOffSurface_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radOffSurface.CheckedChanged
COPsecim()
End Sub
Private Sub radOnFull_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radOnFull.CheckedChanged
COPsecim()
End Sub
Private Sub radOffFull_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radOffFull.CheckedChanged
COPsecim()
End Sub
Sub COPsecim()
Dim dtCOP As New AmetailorDataSet.COPDataTable
Dim adapterCOP As New AmetailorDataSetTableAdapters.COPTableAdapter
adapterCOP.Fill(dtCOP)
Dim COPdatabinding As New BindingSource
COPdatabinding.DataSource = dtCOP
COPdatabinding.Filter = String.Format("Surface_Mounted = {0} AND Full_Height = {1}", Me.radOnSurface.Checked, Me.radOnFull.Checked)
lstCOP.DataSource = COPdatabinding
lstCOP.DisplayMember = "Model"
lstCOP.ValueMember = "Model"
lstCOP.SelectedItem = -1
Button1.Enabled = True
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstCOP.SelectedIndexChanged
Dim dtCOP As New AmetailorDataSet.COPDataTable
Dim adapterCOP As New AmetailorDataSetTableAdapters.COPTableAdapter
adapterCOP.Fill(dtCOP)
If lstCOP.SelectedItem IsNot Nothing Then
Dim lstcoptext As String = lstCOP.GetItemText(lstCOP.SelectedItem)
Dim pic = CType(My.Resources.ResourceManager.GetObject(lstcoptext), Image)
PictureBox1.Image = pic
Button1.Enabled = True
End If
rtxtTechnic.DataBindings.Clear()
Dim COPdatabinding As New BindingSource
COPdatabinding.DataSource = dtCOP
COPdatabinding.DataSource = dtCOP
COPdatabinding.Filter = "Model ='" & lstCOPtext & "'"
rtxtTechnic.DataBindings.Add("Text", dtCOP, "Price")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
pnlCOP1.Visible = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
pnlCOP1.Visible = True
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim lstcoptext As String = lstCOP.GetItemText(lstCOP.SelectedItem)
Form2.ShowDialog()
End Sub
-
Aug 2nd, 2010, 07:52 AM
#8
Thread Starter
Lively Member
Re: use from variant in Form1 in Form2
I resolved the problem by checking your replies. thanks a lot!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|