CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2007
    Posts
    115

    Question Form Passing to Procedure

    Dear Sir,

    I am in great trouble. I have a form which contain Two textbox control. When I passing the Form to Procedure the textbox controls of the form not getting say I am giving Exaple.

    Code:
    Public Class Myform
    
    'This Form Contains Two Controls TextBox1 And TextBox2
    
    'Calling the Procedure
    Call GetReferanceForm(Me)
    
    End Class
    
    Public Class Module1
    
    
    Public Sub GetReferanceForm(frm as Form)
         
        dim oForm as New Form
        oForm =frm 
        'Now Here oFrom is Defined by Frm which MyForm Reference
        'Here I am not getting Controls Like TextBox1,TextBox2
         oForm.TextBox1 'not Getting
    
    End Sub
    Please elp me.

    Thankin You

    Debasis Bag
    Last edited by HanneSThEGreaT; February 11th, 2010 at 05:48 AM. Reason: Added [CODE] tags!

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Form Passing to Procedure

    Please use [CODE] [/CODE] tags when posting code. It is explained here :

    http://www.codeguru.com/forum/showthread.php?t=403073

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Form Passing to Procedure

    TextBox1 will not exist in oForm. Why, because oForm is only just created the only thing it is is an empty form. It doesn't matter if you said oForm = frm because frm is still empty, so you will not get any controls for it.

    If I were to do this :
    Code:
    Dim oForm As New Form1
    Voila, the TextBox can then be used.


    I'd loop through all the controls on the form, in this case frm and determine if there is a control named TextBox1. Based on that, do whatever

  4. #4
    Join Date
    Jul 2007
    Posts
    115

    Question Kind Attention To Mr. HanneSThEGreaT

    Code:
    Public Class Myform 
        'This Form Contains Two Controls TextBox1 And TextBox2 
        'Calling the Procedure 
        Call GetReferanceForm(Me) 
    End Class 
    Public Class Module1 
      Public Sub GetReferanceForm(frm as Form) 
      dim oForm as New Form 
      oForm =frm 
      'Now Here oFrom is Defined by Frm which MyForm Reference 
      'Here I am not getting Controls Like TextBox1,TextBox2 
      oForm.TextBox1 'not Getting 
    End Sub
    I am passing the Myform through frm which doesn't contain controls. But to get all textbox control from oForm in Class Module1. What I have to do.

    Please help me.

    Thanking You

    Debasis Bag
    Last edited by HanneSThEGreaT; February 11th, 2010 at 08:38 AM. Reason: [CODE] Tags

  5. #5
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Form Passing to Procedure

    I have merged Both your threads.

    Debasis Bag, I have noticed that you do not post replies to your threads, you make new threads. That is wrong, please, if you have a current thread with a question, post replies only to that thread. If you have a brand new question which is totally different than your other question, post a new thread.

    Please, read these 2 threads on how to post properly - I keep on sending you the same link, with the hope that you will eventually follow that procedure, so hopefully it is the last time I have to send this link to you

    How to post in the VB.NET Forum

    http://www.codeguru.com/forum/showthread.php?t=403073

    How to post a proper question on CodeGuru

    http://www.codeguru.com/forum/announcement.php?f=12

    Read these threads, then come back to this question

    Thanx for understanding

    Hannes
    Last edited by HanneSThEGreaT; February 11th, 2010 at 08:45 AM.

  6. #6
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Form Passing to Procedure

    I do not exactly understand why you want that. In any case, if reflection is too hard, you can try a couple of tricks (look at all procedures and see if one of the last two is fine for you, if you do not like first two)
    *****edit********************************
    I am blind! did not saw Hannes already show you
    you needed to pass the form as YourForm (!)
    ****************************************
    Code:
    Module ModIsAPublicSharedNotHineritableClass
        
    
        Public Sub GetReferanceForm(ByVal frm As Form)
    'you do not really need oForm. see next procedure...
            Dim oForm As New Form
            oForm = frm
            'Now Here oFrom is Defined by Frm which MyForm Reference 
            'Here I am not getting Controls Like TextBox1,TextBox2 
            For Each ctl As Control In oForm.Controls
                If TypeOf ctl Is TextBox Then
                    Debug.Print("got textbox " & ctl.Name)
     
                    'once you get a control and discover its type,
                    'you can cast it to that type to use it as that...
    
                    Dim txt As TextBox = DirectCast(ctl, TextBox)
                    Debug.Print(txt.Name & " has been used like a textbox")
                    txt.text=string.Empty
    
                End If
            Next
    
        End Sub
    
        Public Sub GetReferanceFormSmaller(ByVal frm As Form)
    
    'without oForm
            
            For Each ctl As Control In frm.Controls
                If TypeOf ctl Is TextBox Then
                    Debug.Print("got textbox " & ctl.Name)
    
                    'once you get a control and discover its type,
                    'you can cast it to that type to use it as that...
    
                    Dim txt As TextBox = DirectCast(ctl, TextBox)
                    Debug.Print(txt.Name & " has been used like a textbox")
                    txt.Text="I am " & txt.Name
                End If
            Next
    
        End Sub
    
        Public Sub GetReferanceFormSpecific(ByVal frm As Form1)
    
    'to get formname.ControlName
    
            frm.TextBox1.Text = "Oh, yes!"
            frm.TextBox2.Text = "I got you!"
            'and you can always do:
            For Each ctl As Control In frm.Controls
                If TypeOf ctl Is TextBox Then
    
                    'look at immediate or output window...
                    Debug.Print("got textbox " & ctl.Name)
                End If
            Next
    
        End Sub
        Public Sub GetReferanceFormSpecificButGeneric(ByVal frm As Form)
    'to be able to do the above trick with more than one form...
    'but you need to know how each form is done.
    
            If TypeOf frm Is Form1 Then
    
    
                DirectCast(frm, Form1).TextBox1.Text = "You see?"
                DirectCast(frm, Form1).TextBox2.Text = "I got you"
                DirectCast(frm, Form1).TextBox3.Text = "again!"
            ElseIf TypeOf frm Is Form2 Then
                'what would you like an instance of  Form2 does for you?
            End If
    
            'and you can always do:
            For Each ctl As Control In frm.Controls
                If TypeOf ctl Is TextBox Then
                    Debug.Print("got textbox " & ctl.Name)
                End If
            Next
    
    
    
        End Sub
    
    End Module
    Last edited by Cimperiali; February 15th, 2010 at 05:16 PM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  7. #7
    Join Date
    Jan 2010
    Posts
    38

    Re: Form Passing to Procedure

    A better understanding of the "New" clause in:

    Code:
         Dim MyNewForm As New Form
    The "As New Form" is asking the system to spawn a new INSTANCE (copy) of Form

    Form is a generic BLANK form and that is why you do not get any contols on it. This is what you get when you add a Windows Form to your project = a blank, generic form.

    BUT let us suppose that you have already created MySpecialForm.vb with lots of Textboxes on it and saved it within your project and then executed the following:

    Code:
        Dim MyNewForm as New MySpecialForm
    Then MyNewForm form (as a copy of MySpecialForm) will deliver what you are seeking.

    You are INHERITING all the controls (and methods) of MySpecialForm

    And Cimperiali has kindly given you the code to loop through all the controls on the form to let you manipulate the Texboxes in whatever way you wish.

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

Featured