Click to See Complete Forum and Search --> : Form Passing to Procedure
Debasis Bag
February 10th, 2010, 11:22 PM
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.
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
HanneSThEGreaT
February 11th, 2010, 04:40 AM
Please use [CODE] [/CODE] tags when posting code. It is explained here :
http://www.codeguru.com/forum/showthread.php?t=403073
HanneSThEGreaT
February 11th, 2010, 04:46 AM
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 :
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
Debasis Bag
February 11th, 2010, 07:15 AM
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
HanneSThEGreaT
February 11th, 2010, 07:37 AM
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
Cimperiali
February 15th, 2010, 04:47 AM
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 (!)
****************************************
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
ddclondon
February 20th, 2010, 01:50 AM
A better understanding of the "New" clause in:
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:
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.