Results 1 to 12 of 12

Thread: calling a function from another form?!!!!!

  1. #1

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Exclamation calling a function from another form?!!!!!

    aaah, I'm confused!
    How can I access a function of another form?

    I have a form with a listView control on it. A form called frmAdd is called to add items to the listview control. When frmAdd is closing, I want to add the new items to my listview control. But I dont know how to access the listview control from "frmAdd". I added a function to my main form, but I cant access the function
    how can I do this? is there a better way to do this anyways?
    Last edited by MrPolite; Jun 14th, 2002 at 09:50 PM.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  2. #2
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    The best way to do this imho is to send a reference to the listview to your frmAdd form. Do this by changing the Public Sub New part of the frmAdd form:-

    Public Sub New(ByRef lv As ListView)

    This sub is the first procedure in the Windows Form Designer Generated code.

    Now from your form with the list view you do the following in the procedure to open the frmAdd form:-

    dim frm as new frmAdd
    frm.Show(ListView1)

    This assumes your listview is called ListView1 so you would change that to the correct name.
    Then when the user clicks the o.k. button on the frmAdd form you just use the passed reference:-

    lv.Items.Add "New Item"
    ....
    ..
    .
    etc.

    Should work but I haven't tried it - thats up to you

  3. #3
    hellswraith
    Guest
    I agree, I have been passing references as arguments to the constructor of the class for a while now. Works great. Plus, if you use the same form for different things, you can overload the constructor and allow yourself to pass it what you want depending on what you want the form/class to do.

  4. #4

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Wow! never thought of editing the default constructor!!! tnx guys

    Edit:
    Doesnt work
    VB Code:
    1. Private newItems() As DB.Item
    2.  
    3. Public Sub New(ByRef newItems() As DB.Item)
    4.     ' ...
    5.     Me.newItems = newItems
    6. End Sub

    when the form is closed, the newItems array that was sent as ByRef isnt changed. the newItems() is changed before closing this form, but when it is closed it will be set to Nothing
    I'm passing it as ByRef!!! why doesnt it work?

    NOTE: DB is a class and DB.Item is a Structure inside it
    Last edited by MrPolite; Jun 13th, 2002 at 10:01 PM.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    you are using the same variable name as a decalred private and a parameter. 20 bucks thats the problem.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    Originally posted by Cander
    you are using the same variable name as a decalred private and a parameter. 20 bucks thats the problem.
    give me 20 bucks that's not a problem at all becuase I'm useing the ME keyword to access the private variable thingie. Just to make sure, I changed the variable name, but that didnt change anything.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  7. #7
    Addicted Member wolfofthenorth's Avatar
    Join Date
    Jan 2001
    Location
    Tatooine
    Posts
    169
    You could try setting the objects through a public property instead of a constructor, but that shouldn't make a difference.

    It also shouldn't make a difference if you pass it ByVal or ByRef.

    Post more code. I don't think the problem is with anything you have posted so far.
    That which does not kill us, only makes us stronger.

  8. #8

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    My fault, but still..

    OH WELL!!! Maybe I'm still confusing C++ and VB together

    When the form starts, the object IS equal to nothing. Let's change the example this way:


    VB Code:
    1. Private newItems() As DB.Item
    2.  
    3. Public Sub New(ByRef SaveValues() As DB.Item)
    4.     ' I wish this could work like a reference
    5.     saveValues = newItems
    6. End Sub
    7.  
    8. Private doStuff()
    9.      ' Suppose that newItems() is chaning to another array
    10.      newItems = .......
    11. End Sub
    I am changing newItems(), so it wont be equal to NOTHING. What I want is that whenever newItems changes, the saveValues array would change to the same value as newItems. And when the form is closing, the saveValues array would be keep alive (because it's declared on the main form)Exactly like setting a reference to a variable in C++.
    kinda confusing
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  9. #9
    hellswraith
    Guest
    Ok, if I understand you correctly, I think I have your answer. Follow these steps.

    Create two forms: form1 and form2

    On form1, add a button and a label with default names. In the buttons click event, add this code:
    Code:
            Dim frmTemp As Form2
            frmTemp = New Form2(Me)
            frmTemp.ShowDialog()
    Also add a user created sub to that form1 class like this:
    Code:
        Public Sub MySub()
            Label1.BackColor = System.Drawing.Color.AliceBlue
        End Sub

    Now, on form2, add just one button, keeping the default name.
    Here is where things get a little tricky, so follow closely...
    First, edit the constructor to take a object variable into it and assign it to a variable we will add in next:
    Code:
        Public Sub New(ByVal sender As System.Object)
            MyBase.New()
    
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call
    
            original = sender
        End Sub
    Now, add a private "CLASS" variable to the form2 class like so (I am showing you two statements, the first is put there by VS, the second is mine. I just wanted to show you where I put it)
    Code:
        Private components As System.ComponentModel.IContainer
        'This one is the one I added:
        Private original As System.Object
    Now, in the buttons click event, put this code:
    Code:
            ' This will call the MySub on form 1, which created form2.
            Dim frmTemp As Form1
            frmTemp = DirectCast(original, Form1)
            frmTemp.MySub()
    Now, when you start and run the program, form one will show up. You push the button to bring up form2. When you push form2's button, it will call a public sub from form1. If you watch, you will see the labels color change to prove it works.

  10. #10
    hellswraith
    Guest
    And, just in case I wasn't clear on anything, here is all the code for both forms, starting with form1.

    Code:
    'Form1's code:
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
    #Region " Windows Form Designer generated code "
    
        Public Sub New()
            MyBase.New()
    
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call
    
        End Sub
    
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
    
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
    
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents Button1 As System.Windows.Forms.Button
        Friend WithEvents Label1 As System.Windows.Forms.Label
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.Button1 = New System.Windows.Forms.Button()
            Me.Label1 = New System.Windows.Forms.Label()
            Me.SuspendLayout()
            '
            'Button1
            '
            Me.Button1.Location = New System.Drawing.Point(72, 128)
            Me.Button1.Name = "Button1"
            Me.Button1.TabIndex = 0
            Me.Button1.Text = "Button1"
            '
            'Label1
            '
            Me.Label1.Location = New System.Drawing.Point(32, 32)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(160, 23)
            Me.Label1.TabIndex = 1
            Me.Label1.Text = "Label1"
            '
            'Form1
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(292, 266)
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1, Me.Button1})
            Me.Name = "Form1"
            Me.Text = "Form1"
            Me.ResumeLayout(False)
    
        End Sub
    
    #End Region
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim frmTemp As Form2
            frmTemp = New Form2(Me)
            frmTemp.ShowDialog()
        End Sub
    
        Public Sub MySub()
            Label1.BackColor = System.Drawing.Color.AliceBlue
        End Sub
    End Class
    Code:
    'Form2's code:
    Public Class Form2
        Inherits System.Windows.Forms.Form
    
    #Region " Windows Form Designer generated code "
    
    
        Public Sub New(ByVal sender As System.Object)
            MyBase.New()
    
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call
    
            original = sender
        End Sub
    
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
    
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
        Private original As System.Object
    
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents Button1 As System.Windows.Forms.Button
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.Button1 = New System.Windows.Forms.Button()
            Me.SuspendLayout()
            '
            'Button1
            '
            Me.Button1.Location = New System.Drawing.Point(88, 104)
            Me.Button1.Name = "Button1"
            Me.Button1.TabIndex = 0
            Me.Button1.Text = "Button1"
            '
            'Form2
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(292, 266)
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1})
            Me.Name = "Form2"
            Me.Text = "Form2"
            Me.ResumeLayout(False)
    
        End Sub
    
    #End Region
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' This will call the MySub on form 1, which created form2.
            Dim frmTemp As Form1
            frmTemp = DirectCast(original, Form1)
            frmTemp.MySub()
        End Sub
    End Class

  11. #11

    Thread Starter
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    tried your fist post, workz perfect!!! tnx


    EDITED:
    I'm just wondering why do you send the form as system.Object? I tried the same thing and I also tried to send the form like this
    VB Code:
    1. Private Original As Form1
    2.     Public Sub New(ByVal frmOriginal As Form1)
    3.         MyBase.New()
    4.  
    5.         'This call is required by the Windows Form Designer.
    6.         InitializeComponent()
    7.  
    8.         'Add any initialization after the InitializeComponent() call
    9.  
    10.         original = frmOriginal
    11.     End Sub

    this works too, but why would you use system.object and then cast from it? is that any better?
    Last edited by MrPolite; Jun 14th, 2002 at 09:44 PM.
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  12. #12
    hellswraith
    Guest
    The reason I did the cast is only a matter of preference thing. I was needing to pass in different object types to the constructor, so I ended up using System.Object. Your way will work, as long as form1 is the only one creating that form2.

    Glad it worked for you.

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