473,408 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How to pass a variable to a parameter ?

Rob
lets assume I want to remove a control from a form that was dynamically
created...

If the button was added at design time, I can do the following...

me.Controls.Remove(Button4)

How may I pass a variable in the parameter position ? i.e.,
me.Controls.Remove(strVariable)

May 12 '07 #1
7 1903
>How may I pass a variable in the parameter position ? i.e.,
>me.Controls.Remove(strVariable)
I assume from your naming convention that strVariable stores the name
of the control. If you instead store a reference to the control
itself, you could just pass it in as it is.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 12 '07 #2
Rob
Thanks Mattias,

Do you have a sample in VB.net ?

"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:ed**************@TK2MSFTNGP05.phx.gbl...
How may I pass a variable in the parameter position ? i.e.,
me.Controls.Remove(strVariable)

I assume from your naming convention that strVariable stores the name
of the control. If you instead store a reference to the control
itself, you could just pass it in as it is.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

May 12 '07 #3
Rob
I tried this...

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim delme As Object
delme = New Object()

Dim ctrl As Control
For Each ctrl In FlowLayoutPanel1.Controls

delme = (ctrl.Name)
Call wipeout(delme)

Next

End Sub
End Class
Public Sub wipeout(ByRef delme As Object)

Me.FlowLayoutPanel1.Controls.Remove(delme)
End Sub


"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:ed**************@TK2MSFTNGP05.phx.gbl...
How may I pass a variable in the parameter position ? i.e.,
me.Controls.Remove(strVariable)

I assume from your naming convention that strVariable stores the name
of the control. If you instead store a reference to the control
itself, you could just pass it in as it is.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

May 12 '07 #4
On May 12, 7:15 am, "Rob" <r...@yahoo.comwrote:
I tried this...

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim delme As Object
delme = New Object()

Dim ctrl As Control
For Each ctrl In FlowLayoutPanel1.Controls

delme = (ctrl.Name)
Call wipeout(delme)

Next

End Sub
End Class

Public Sub wipeout(ByRef delme As Object)

Me.FlowLayoutPanel1.Controls.Remove(delme)
End Sub

"Mattias Sjögren" <mattias.dont.want.s...@mvps.orgwrote in message

news:ed**************@TK2MSFTNGP05.phx.gbl...
>How may I pass a variable in the parameter position ? i.e.,
me.Controls.Remove(strVariable)
I assume from your naming convention that strVariable stores the name
of the control. If you instead store a reference to the control
itself, you could just pass it in as it is.
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/|http://www.dotnetinterop.com
Please reply only to the newsgroup.
Try something like this:

' Typed in message
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
For Each c As Control In Me.FlowLayoutPanel1.Controls
If (c.Name = "The name of the control to delete") Then
Wipeout(c)
Exit For
End If
Next
End Class

Public Sub Wipeout(ByVal c as Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Or if you're using .Net 2.0 (VB 2005) you should be able to do this:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Wipeout(Me.FlowLayoutPanel1.FindControl("The name of the control
to delete"))
End Class

Public Sub Wipeout(ByVal c as Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Thanks,

Seth Rowe

May 12 '07 #5
Rob
Thanks so much for your help Seth !

I placed the code below into the project... odd thing though...
It appears to delete only half the controls... (FYI - using 2.0 VS2005)

I have a button that adds user controls to the FlowLayoutPanel... so I added
8 of them... ran the code and 4 were deleted... ran again... 2 more were
deleted...

Public Sub Wipeout(ByVal c As Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim strControlName As String
For Each c As Control In Me.FlowLayoutPanel1.Controls
strControlName = c.Name
If (c.Name = strControlName) Then
Wipeout(c)
'Exit For
End If
Next
End Sub

- Rob
"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...
On May 12, 7:15 am, "Rob" <r...@yahoo.comwrote:
I tried this...

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim delme As Object
delme = New Object()

Dim ctrl As Control
For Each ctrl In FlowLayoutPanel1.Controls

delme = (ctrl.Name)
Call wipeout(delme)

Next

End Sub
End Class

Public Sub wipeout(ByRef delme As Object)

Me.FlowLayoutPanel1.Controls.Remove(delme)
End Sub

"Mattias Sjögren" <mattias.dont.want.s...@mvps.orgwrote in message

news:ed**************@TK2MSFTNGP05.phx.gbl...
>How may I pass a variable in the parameter position ? i.e.,
me.Controls.Remove(strVariable)
I assume from your naming convention that strVariable stores the name
of the control. If you instead store a reference to the control
itself, you could just pass it in as it is.
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/|http://www.dotnetinterop.com
Please reply only to the newsgroup.
Try something like this:

' Typed in message
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
For Each c As Control In Me.FlowLayoutPanel1.Controls
If (c.Name = "The name of the control to delete") Then
Wipeout(c)
Exit For
End If
Next
End Class

Public Sub Wipeout(ByVal c as Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Or if you're using .Net 2.0 (VB 2005) you should be able to do this:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Wipeout(Me.FlowLayoutPanel1.FindControl("The name of the control
to delete"))
End Class

Public Sub Wipeout(ByVal c as Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Thanks,

Seth Rowe
May 12 '07 #6
On May 12, 8:28 am, "Rob" <r...@yahoo.comwrote:
Thanks so much for your help Seth !

I placed the code below into the project... odd thing though...
It appears to delete only half the controls... (FYI - using 2.0 VS2005)

I have a button that adds user controls to the FlowLayoutPanel... so I added
8 of them... ran the code and 4 were deleted... ran again... 2 more were
deleted...

Public Sub Wipeout(ByVal c As Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim strControlName As String
For Each c As Control In Me.FlowLayoutPanel1.Controls
strControlName = c.Name
If (c.Name = strControlName) Then
Wipeout(c)
'Exit For
End If
Next
End Sub

- Rob

"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11**********************@y80g2000hsf.googlegr oups.com...
On May 12, 7:15 am, "Rob" <r...@yahoo.comwrote:
I tried this...
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim delme As Object
delme = New Object()
Dim ctrl As Control
For Each ctrl In FlowLayoutPanel1.Controls
delme = (ctrl.Name)
Call wipeout(delme)
Next
End Sub
End Class
Public Sub wipeout(ByRef delme As Object)
Me.FlowLayoutPanel1.Controls.Remove(delme)
End Sub
"Mattias Sjögren" <mattias.dont.want.s...@mvps.orgwrote in message
news:ed**************@TK2MSFTNGP05.phx.gbl...
How may I pass a variable in the parameter position ? i.e.,
>>me.Controls.Remove(strVariable)
I assume from your naming convention that strVariable stores the name
of the control. If you instead store a reference to the control
itself, you could just pass it in as it is.
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.org
>http://www.msjogren.net/dotnet/|http://www.dotnetinterop.com
Please reply only to the newsgroup.

Try something like this:

' Typed in message
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
For Each c As Control In Me.FlowLayoutPanel1.Controls
If (c.Name = "The name of the control to delete") Then
Wipeout(c)
Exit For
End If
Next
End Class

Public Sub Wipeout(ByVal c as Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Or if you're using .Net 2.0 (VB 2005) you should be able to do this:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Wipeout(Me.FlowLayoutPanel1.FindControl("The name of the control
to delete"))
End Class

Public Sub Wipeout(ByVal c as Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Thanks,

Seth Rowe
I placed the code below into the project... odd thing though...
It appears to delete only half the controls... (FYI - using 2.0 VS2005)
That's because as we remove a control it throws off the for each
loop's counter causing it to skip every other control. Now that I
realized my stupid mistake (hey it is early here) try this instead:

Private Sub Button3_Click2(ByVal sender As System.Object, ByVal e
As System.EventArgs) 'Handles Button3.Click
Dim i As Integer = 0
While i < Me.FlowLayoutPanel1.Controls.Count
Dim c As Control = Me.FlowLayoutPanel1.Controls(i)
If (c.Name = "ControlToDelete") Then
Wipeout(c)
Else
i += 1
End If
End While
End Sub

That way we only increment the counter when we don't remove a control.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim strControlName As String
For Each c As Control In Me.FlowLayoutPanel1.Controls
strControlName = c.Name
If (c.Name = strControlName) Then
Wipeout(c)
'Exit For
End If
Next
End Sub
I see you are just deleting all the FlowLayoutPanels controls right?
If that's all you need to do you can just replace that whole method
with:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e
As
Me.FlowLayoutPanel1.Controls.Clear()
End Sub

Thanks,

Seth Rowe

May 12 '07 #7
Rob
Seth,

That worked perfectly ! Thank You very much !

Thanks for this as well...
> Me.FlowLayoutPanel1.Controls.Clear()
However, eventually I will be adding some criteria as to which controls get
removed...

Rob
"rowe_newsgroups" <ro********@yahoo.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
On May 12, 8:28 am, "Rob" <r...@yahoo.comwrote:
Thanks so much for your help Seth !

I placed the code below into the project... odd thing though...
It appears to delete only half the controls... (FYI - using 2.0 VS2005)

I have a button that adds user controls to the FlowLayoutPanel... so I
added
8 of them... ran the code and 4 were deleted... ran again... 2 more were
deleted...

Public Sub Wipeout(ByVal c As Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim strControlName As String
For Each c As Control In Me.FlowLayoutPanel1.Controls
strControlName = c.Name
If (c.Name = strControlName) Then
Wipeout(c)
'Exit For
End If
Next
End Sub

- Rob

"rowe_newsgroups" <rowe_em...@yahoo.comwrote in message

news:11**********************@y80g2000hsf.googlegr oups.com...
On May 12, 7:15 am, "Rob" <r...@yahoo.comwrote:
I tried this...
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim delme As Object
delme = New Object()
Dim ctrl As Control
For Each ctrl In FlowLayoutPanel1.Controls
delme = (ctrl.Name)
Call wipeout(delme)
Next
End Sub
End Class
Public Sub wipeout(ByRef delme As Object)
Me.FlowLayoutPanel1.Controls.Remove(delme)
End Sub
"Mattias Sjögren" <mattias.dont.want.s...@mvps.orgwrote in message
news:ed**************@TK2MSFTNGP05.phx.gbl...
How may I pass a variable in the parameter position ? i.e.,
>>me.Controls.Remove(strVariable)
I assume from your naming convention that strVariable stores the name
of the control. If you instead store a reference to the control
itself, you could just pass it in as it is.
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.org
>http://www.msjogren.net/dotnet/|http://www.dotnetinterop.com
Please reply only to the newsgroup.

Try something like this:

' Typed in message
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
For Each c As Control In Me.FlowLayoutPanel1.Controls
If (c.Name = "The name of the control to delete") Then
Wipeout(c)
Exit For
End If
Next
End Class

Public Sub Wipeout(ByVal c as Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Or if you're using .Net 2.0 (VB 2005) you should be able to do this:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Wipeout(Me.FlowLayoutPanel1.FindControl("The name of the control
to delete"))
End Class

Public Sub Wipeout(ByVal c as Control)
If (Me.FlowLayoutPanel1.Controls.Contains(c)) Then
Me.FlowLayoutPanel1.Controls.Remove(c)
c.Dispose()
End If
End Sub

Thanks,

Seth Rowe
I placed the code below into the project... odd thing though...
It appears to delete only half the controls... (FYI - using 2.0 VS2005)
That's because as we remove a control it throws off the for each
loop's counter causing it to skip every other control. Now that I
realized my stupid mistake (hey it is early here) try this instead:

Private Sub Button3_Click2(ByVal sender As System.Object, ByVal e
As System.EventArgs) 'Handles Button3.Click
Dim i As Integer = 0
While i < Me.FlowLayoutPanel1.Controls.Count
Dim c As Control = Me.FlowLayoutPanel1.Controls(i)
If (c.Name = "ControlToDelete") Then
Wipeout(c)
Else
i += 1
End If
End While
End Sub

That way we only increment the counter when we don't remove a control.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim strControlName As String
For Each c As Control In Me.FlowLayoutPanel1.Controls
strControlName = c.Name
If (c.Name = strControlName) Then
Wipeout(c)
'Exit For
End If
Next
End Sub
I see you are just deleting all the FlowLayoutPanels controls right?
If that's all you need to do you can just replace that whole method
with:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e
As
Me.FlowLayoutPanel1.Controls.Clear()
End Sub

Thanks,

Seth Rowe
May 12 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
2
by: Yarik | last post by:
Hello there! I am working with MS SQL Server 2000. I have a table function that takes an integer parameter and returns a table, and I can successfully use it like this (passing a literal as a...
5
by: wilson | last post by:
Dear all, In this time, I want to pass array to function. What should I declare the parameter in the function?i int array or int array? Which one is correct? ...
1
by: DC Gringo | last post by:
I am trying to pass an input parater to SQL Server. The parameter is a URL querystring variable. I can't seem to get it to pass a variable, only a literal value... Help! In the following...
4
by: _Mario.lat | last post by:
Hallo, I have a little question: In the function session_set_save_handler I can pass the name of function which deal with session. In Xoops code I see the use of this function like that: ...
2
by: phil | last post by:
Hi, I want to put a recordset in a gridview but i don't know how to pass the value of the variable in the 'where' statement. The value of the variable is set in the code-behind. i added a tag...
5
by: David++ | last post by:
Hi folks, I would be interested to hear peoples views on whether or not 'pass by reference' is allowed when using a Web Service method. The thing that troubles me about pass-by-reference into...
10
arunmib
by: arunmib | last post by:
hi all, I just got this freaky kind of doubt....I have the following piece of code, int main() { int Val= 10, *ptr; ptr = &Val; TestFn(&Val);
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.