Results 1 to 17 of 17

Thread: Passing a form to a function?

  1. #1

    Thread Starter
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281

    Passing a form to a function?

    I had no problems in vb6

    How can I do it in .net?
    I'd like to access and change values of properties of the objects on another form.

    ie: function T ( ByRef myForm as System.Windows.Forms.Form)

    myForm.txtName.Text = "Joe"

    end function

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

  3. #3
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840

    Re: Passing a form to a function?

    Originally posted by wordracr
    I had no problems in vb6

    How can I do it in .net?
    I'd like to access and change values of properties of the objects on another form.

    ie: function T ( ByRef myForm as System.Windows.Forms.Form)

    myForm.txtName.Text = "Joe"

    end function
    In your example above you are only passing a reference to a generic form.
    If you need to access txtName you need to reference the form that contains the txtName.

    And btw, Forms are a reference type so it doesn't matter if you use ByRef or ByVal.

    ie: Function T (ByVal myForm As Form1)
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  4. #4
    Hyperactive Member
    Join Date
    Mar 2004
    Location
    Prato - Tuscany - Italy
    Posts
    461
    This is a sub I use to set forecolor=black to all objects on a specified form, excepted some object types
    To call it, from a specified Form, for the objects of which you want to set forecolor=black, you only need to write (GIN is the project's name. The function is in a module):

    GIN.PblSubForeColorNero(Me)



    VB Code:
    1. Public Sub PblSubForeColorNero(ByVal ThisForm As System.Windows.Forms.Form)
    2.         Dim ThisControl As System.Windows.Forms.Control
    3.         For Each ThisControl In ThisForm.Controls
    4.             If Not TypeOf ThisControl Is Button Then 'Or TypeOf ThisControl Is RadioButton Or TypeOf ThisControl Is Label Or TypeOf ThisControl Is CheckBox Then
    5.                 ThisControl.ForeColor = System.Drawing.Color.Black
    6.             End If
    7.         Next ThisControl
    8.     End Sub

    As Pax said, it should be correct to use byref, but it doesn't matter in this case!

    Good job
    Live long and prosper (Mr. Spock)

  5. #5
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Re: Passing a form to a function?

    Originally posted by wordracr
    I had no problems in vb6

    How can I do it in .net?
    I'd like to access and change values of properties of the objects on another form.

    ie: function T ( ByRef myForm as System.Windows.Forms.Form)

    myForm.txtName.Text = "Joe"

    end function
    The simple answer is:
    VB Code:
    1. Public Function T(ByVal obj1 As Object) As Object
    2.       obj1.text ="Joe"
    3.   End Function
    The calling event in myForm
    VB Code:
    1. T(txt.Name)

    I have assumed the Function is in the Module. If the function is in another form, you will have to make sure that form is properly instanced and accessible.
    I have also assumed Option Strict is OFF so that any object can be passed to the function. If you want to limit the function to handling a specific type of object then you can use early binding and Option Strict ON
    Last edited by taxes; Jun 16th, 2004 at 06:25 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  6. #6

    Thread Starter
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281
    Thanks for the help, but it doesn't answer my specific question.

    I'm passing a FORM because there are repetitive form controls being modified. I'd prefer not to pass every control that I'm working with.

    How can this be done?

  7. #7
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    the answers are up posted.

  8. #8

    Thread Starter
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281
    THese dont solve my problem. I dont see how you can say that answers it.

    I have build errors right now.

  9. #9
    Fanatic Member brown monkey's Avatar
    Join Date
    Jun 2004
    Location
    Cebu
    Posts
    552
    quick sample. there's 2 form [let's say form1 and form2]. in form1, there's a button which shows the form2. like this.
    VB Code:
    1. Dim f As New Form2(Me)
    2.       f.ShowDialog()
    and in form2, we could change the constructor, like this.
    VB Code:
    1. Dim f As Form
    2.    Public Sub New(ByRef form)
    3.       MyBase.New()
    4.  
    5.       'This call is required by the Windows Form Designer.
    6.       InitializeComponent()
    7.  
    8.       f = form
    9.  
    10.       'Add any initialization after the InitializeComponent() call
    11.  
    12.    End Sub
    and on load of form2
    VB Code:
    1. Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.       f.BackColor = Color.Red
    3.    End Sub
    hope this helps. pax, taxes and alex here has this idea though. credit for them... thanx...

  10. #10
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Originally posted by wordracr
    THese dont solve my problem. I dont see how you can say that answers it.

    I have build errors right now.
    Then you haven't looked hard enough. My post clearly answers you question...
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  11. #11
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by wordracr
    THese dont solve my problem. I dont see how you can say that answers it.

    I have build errors right now.
    I would also add my assurance that the codings you have been given work. If you are getting build errors, then you have probably not created the correct instances of the forms.

    I would also emphasise the remarks in my previous post

    "I have assumed the Function is in the Module. If the function is in another form, you will have to make sure that form is properly instanced and accessible.
    I have also assumed Option Strict is OFF so that any object can be passed to the function. If you want to limit the function to handling a specific type of object then you can use early binding and Option Strict ON"

    Please answer the following:


    1. Where is the Function?

    2. What is your startup object?

    3. What are your build errors?
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  12. #12

    Thread Starter
    Hyperactive Member wordracr's Avatar
    Join Date
    Aug 2001
    Posts
    281
    The problem is probably that I didn't change my project setup yet, so the STARTUP obj is "form1". I have heard if you use multiple forms, you have to use a sub main() in a module. I will change that soon.

    IN a module, the public function I'd like to use can modify objects of form1, form2, or form3.

    function T(ByVal theForm as System.Windows.Forms.Form)
    theForm.A_Form_Obj.top = 10
    End Function

    ERROR: "A_Form_Obj is not a member of System.Windows.Forms.Form"

    Another possible problem: Option strict. It's off right now. Is there a way in tools to have it on by default?

    Thanks a lot for the continuing help!

  13. #13
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by wordracr
    Another possible problem: Option strict. It's off right now. Is there a way in tools to have it on by default?
    Yes , go to Tools menu , Options , Projects item , there you go .

  14. #14
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    You can use the form1 as the startup if you want that form to remain open throughout the project, which seems to be what you are doing but I prefer to ALWAYS use the Sub Main of a Module. What you require is the following;

    In the Module
    VB Code:
    1. Public frm1 As New Form1
    2. Public frm2 As New Form2
    3.  
    4. Public Sub Main()
    5.   Application.Run(frm1)
    6. End Sub

    When you want to open frm2: In the appropriate event of form1

    VB Code:
    1. frm2.ShowDialog()

    Now you can put the following in either form2 or the module

    VB Code:
    1. Public Sub T(ByVal obj1 As Object, strText as String) As Object
    2.       obj1.text strText  
    3.   End Sub


    Now, you can call this Sub from form1 passing the parameters of the control and the value of the string as required. Please note:

    1. You use this to alter the Text property of any appropriate object to the chosen characters. You will have to call the sub for each object one at a time.

    2. If you want to make the Text of all the objects then you can call the sub from a loop through all controls in form1. (You can set the loop to select only those controls of the same type or you can even select controls of different types omitting some of those types)

    3. You will find that you can now access any control of either form from form1 or form2

    Hope that helps.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  15. #15
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by Pirate
    Yes , go to Tools menu , Options , Projects item , there you go .

    Nope. At least not in VB.NET 2003

    Right click on project name in solution explorer.
    Choose Properties from context menu (sometimes - I don't know
    why - the wrong
    Properties page turns up.
    If so repeat)
    Click Build item
    Open the Option Strict dropdown list
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  16. #16
    Member
    Join Date
    Apr 2004
    Location
    Millbrae, CA
    Posts
    48
    If the poster is still confused...

    A form is now just another object. So you are not loading "Form2" per se... you're loading an instance of Form2 (just like any other class object).

    So, if you have a "frmMain" and "frmChild" and want to show frmChild by pressing a button, you first need to declare it as an object.

    VB Code:
    1. Dim child1 as New frmChild  'A new object from which to work with
    2.  
    3. child1.Show    'Show the instance of the form

    So, in the sub procedure in which you want to pass a frmChild as a parameter, set it up like this:

    VB Code:
    1. Private Sub DoThis (ByVal child as frmChild)
    2. 'code...
    3. End Sub

    Hope this helps!

  17. #17
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by wordracr

    IN a module, the public function I'd like to use can modify objects of form1, form2, or form3.

    function T(ByVal theForm as System.Windows.Forms.Form)
    theForm.A_Form_Obj.top = 10
    End Function

    ERROR: "A_Form_Obj is not a member of System.Windows.Forms.Form"

    HI,

    Just one point about the above section of your post.

    You can use

    function T(ByVal theForm as System.Windows.Forms.Form)
    theForm.A_Form_Obj.top = 10
    End Function

    provided you set the Modifier property of A_Form_Obj to Public.

    Sorry, I did not notice that part of your post, sooner.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

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