Results 1 to 8 of 8

Thread: [RESOLVED] use from variant in Form1 in Form2

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    93

    Resolved [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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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:
    1. Module GlobalModule
    2.     ' Module level
    3.     Public GlobalX As Integer ' This is visible from everywhere
    4.     Friend A As String ' This is visible only from within this application
    5.     Public SimpleX As Integer = 1 ' Also visible from everywhere
    6.  
    7.     Public Class SampleClass  ' This class is also visible from everywhere
    8.          ' Class level
    9.          Public Field As Integer = 0 ' You can refer to it using InstanceName.Field (where InstanceName
    10.                            ' is the name of the SampleClass instance
    11.          Private Shared SimpleX As Integer = 2' Within this class SimpleX will Shadow the SimpleX
    12.                           ' which is declared above (at the module level)
    13.                           ' every reference to SimpleX from this class will be evaluated to 2, not 1
    14.                           ' Also this variable is Shared so it is accessible using SimpleClass.SampleX syntax
    15.  
    16.          Private X As Integer = 5 ' A private instance member which is only accessible from this class
    17.          Protected Y As Integer = 5 ' Similar to private but can be accessible from an inherited class
    18.          
    19.          Sub Method1
    20.               ' Procedure Level
    21.               Dim X As Integer = 10  ' This X will shadow the X declared at the class level so here X = 10, not 5
    22.               IF X = 10 Then
    23.               ' Block level
    24.                  Dim Z As Integer = X + 5 ' At the block level Z = 15
    25.               End If
    26.  
    27.               ' Z declared above is not visible here already (outside the If block).
    28.  
    29.               ' The block ended and I can declare here a new Z:
    30.               Dim Z As Integer = Me.X - 5 ' Since I used Me keyword Me.X = 5 and thus Z here = 0
    31.              
    32.          End Sub
    33.     End Class
    34.  
    35. End Module

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    93

    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.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    93

    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...

  6. #6
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    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.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    93

    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

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2010
    Posts
    93

    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
  •  



Click Here to Expand Forum to Full Width