How Can I Do This?

  • Thread starter Thread starter Henry Jones
  • Start date Start date
H

Henry Jones

Using VB.NET 2005.

I have a Function that I would like to pass the form name and the textbox
name to.

-----------------------------------------
Function DoSomeThing(byref frmOBJ as Object, byref txtOBJ as TextBox) as
integer

frmOBJ.txtOBJ.text = "Some text goes here"

DoSomeThing = 0

End Function
------------------------------------------


When I call this Function, I pass two objects, the FormName and the TextBox
name.

--------------------------------------
dim answ as integer = DoSomeThing(frmDisplay, txtDisplay)
---------------------------------------




I get an error saying that frmDisplay.txtOBJ doesn't exist.

Is there a way to pass two objects to the function and have both of them be
replaceable?

Thanks for any input
 
In this scenario, you do not have to use frmObj.
Just assign the string to txtObj.

If you want to use frmObj, you have to use the real name of the textbox
in the form, like ctype(frmObj.Controls(txtObj.Name),Textbox).text = ...
 
I condensed the code for the example here, but there are more things going
on in the routine and the result gets passed to the textbox. I would like
to call the function from many different forms and I thought I could pass
the form and the textbox to it. Just wanted to see if it could be done.

Yes, there are probably better ways of doing it, but hey, if it can be done,
I am curious now.

Thanks
 
Public Sub DoSometing(byref txt as TextBox)
txt.text = [YOUR VALUE]
End Sub

of

public function DoSomething(byref frmObj as [Form Type I.e. frmMain], byref
txtOBJ as TextBox)
'option 1
ctype(frmObj.Controls(txtOBJ.name),Textbox).text = [YOUR VALUE]

'option 2
txtObj.text = [YOUR VALUE]

return 0
end function
 
Henry Jones said:
Using VB.NET 2005.

I have a Function that I would like to pass the form name and the textbox
name to.

-----------------------------------------
Function DoSomeThing(byref frmOBJ as Object, byref txtOBJ as TextBox) as
integer

frmOBJ.txtOBJ.text = "Some text goes here"

DoSomeThing = 0

End Function
------------------------------------------


When I call this Function, I pass two objects, the FormName and the
TextBox name.

--------------------------------------
dim answ as integer = DoSomeThing(frmDisplay, txtDisplay)
---------------------------------------




I get an error saying that frmDisplay.txtOBJ doesn't exist.

Is there a way to pass two objects to the function and have both of them
be replaceable?

Thanks for any input

Something I do not understand is why you are passing both arguments by
reference? Since you are passing and object and a textbox (both are
reference types) and you aren't "replacing" the actual object nor are you
"replacing" the actual textbox instance, these do not need to be passed by
reference.

What I see you are trying to do is get the property of the form passed in.
The property name is the name of the textbox being passed in. If you have
an instance of the textbox (hence, you are passing it in via parameter), why
do you need to access the property of the form (which supposedly returns the
same instance as the textbox parameter, afaik)? Maybe your example is
incorrect and you don't mean to be passing in an object and a textbox?

HTH,
Mythran
 
No, that is not possible.

The reason is that the form doesn't have a name, so there is no way that
you can identify it that way. What I think that you mean is the name of
the class that the form was created from, but that can't be used to
identify the instance of the form, as there can be more than one form
that is created from the same class.

What you need is a reference to the instance of the form, or even
better, a reference to the textbox.
 
Herny,

If I understand you well, than you can achieve it like this (although this
goes only for owner forms)..

\\\
'This form holds two textboxes.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim frm2 As New Form2
frm2.Owner = Me
frm2.Show()
End Sub
End Class
///
\\\
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
useTextBox(DirectCast(Me.Owner, Form1).TextBox2)
End Sub
Private Sub useTextBox(ByVal txtBox As TextBox)
txtBox.Text = "Some text goes here"
End Sub
End Class
///
 
Henry said:
I have a Function that I would like to pass the form name and the textbox
name to.

More likely (and usefully), you want to pass the objects themselves,
/not/ their names.
Function DoSomeThing(byref frmOBJ as Object, byref txtOBJ as TextBox) as
integer
frmOBJ.txtOBJ.text = "Some text goes here"
DoSomeThing = 0
End Function
When I call this Function, I pass two objects, the FormName and the TextBox
name.
dim answ as integer = DoSomeThing(frmDisplay, txtDisplay)

No; you're passing a Form (Type'd as Object) and a TextBox.
I get an error saying that frmDisplay.txtOBJ doesn't exist.

That's because the Type Object doesn't have a property/method called
txtOBJ.
Is there a way to pass two objects to the function and have both of them be
replaceable?

Yes, but in this case, you don't need to.

Function DoSomeThing(ByVal txt As TextBox) _
As integer

txt.Text = "Some text goes here"

DoSomeThing = 0
End Function

then call it using

i = DoSomething( Form17.txtBox1 )

HTH,
Phill W.
 
Back
Top