Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing a control name as an argument

Status
Not open for further replies.

spizotfl

MIS
Aug 17, 2005
345
US
is it possible to pass a control name as an argument to a function or procedure? if so, how do you do it?
i have looked around in the forum and on google, but i guess i am not phrasing the question correctly when searching.

"Maturity is a bitter disappointment for which no remedy exists, unless laughter can be said to remedy anything."
-Vonnegut
 
Example

sub xxx(ByVal ctl as TextBox)


NOTES
- sub can be function xxx(..) as something
- ByVal can be ByRef if you pass a reference to that control. (C programmers know this as 'pointer')
 
ByVal ctl as TextBox

could be: ByVal ctl as Control or Object. This change "extends" the functionallity and the sub (or function) can accept any control. You can find out what control was passed using:

GetType(ctl)
 
say i have some textboxes named txt1, txt2, txt3, txt4, is it possible to loop through these using something like (not real code)
ctlName = "txt" & counterVariable

"Maturity is a bitter disappointment for which no remedy exists, unless laughter can be said to remedy anything."
-Vonnegut
 
I think there is a FAQ (see the FAQ link at the top of the page) that covers that very subject.

Other possiblilities include looping through the controls in a container. Say all of the text boxes you are trying to loop through are in a frame or on a panel. You could do something like:

Code:
dim ctrl as control
For each ctrl in MyPanel.controls
  if ctrl.gettype = textbox then
    'do something
  end if
next

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Rick, 'if ctrl.gettype = textbox then' is not working :)

Another way.

Dim ctrl As Control
For Each ctrl In Me.Controls
If ctrl.GetType.Name.ToLower = "textbox" AndAlso _
ctrl.Name.ToLower Like "txt?" Then
' do something
End If
Next
 
Heh, I haven't touched the IDE in over a month, so I'm a tad rusty.

Here is the code from the IDE:
Code:
For Each ctrl As Control In Me.Controls
  If ctrl.GetType Is GetType(TextBox) Then
    MessageBox.Show(ctrl.Name & " contains: " & ctrl.Text)
  End If
Next

Ahhhhh, it's nice to have an IDE again.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I haven't touched the IDE

> I work a lot with the intellisense. I do not remember much stuff..
 
I think this would work, if you use it within your function:
Code:
Dim TextValue As String = Microsoft.VisualBasic.CallByName(Me, "txt" & Number & ".Text", CallType.Get)

'And
Microsoft.VisualBasic.CallByName(Me, "txt" & Number & ".Text", CallType.Set, TextValue)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top