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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to remove dynamic textboxes in VB.NET?

Status
Not open for further replies.

lydro

Programmer
Mar 7, 2005
36
CA
I created an array textboxes, the frist load is good, but when I reload it again, it won't work, and I know it has to be removed before load again, but I don't know how to remove it in dynamic textboxes, they don't have textbox.name. here is my create textbox code.

Private Sub textboxShow()
setStartIndex()
setEndIndex()
clearTextboxes()'????here is the remove textboxes, but I don't know how to code this function. I have used controls.remove(), but it doesn't work either.PLS HELP!!

Dim I, J As Integer
Dim sData As Integer = 1
While sData <= iEndIndex
For I = 1 To 6
For J = 1 To 7
Call AddDataShow(sData, I, J)
sData += 1
Next
Next
End While
End Sub

Public Sub AddDataShow(ByVal sText As String, ByVal I As Integer, ByVal J As Integer)
Dim txtDataShow As New TextBox
Dim UserLft, UserTop As Integer
Dim X, Y As Integer
Dim a As Integer
a = sText - iStartIndex
UserLft = 20
UserTop = 80
txtDataShow.Height = 80
txtDataShow.Width = 80
txtDataShow.TextAlign = HorizontalAlignment.Left
txtDataShow.BorderStyle = BorderStyle.FixedSingle
txtDataShow.BringToFront()
If a > 0 And a <= iEndIndex Then
txtDataShow.Text = a
'MessageBox.Show(a)
Else
txtDataShow.Text = ""
End If
If txtDataShow.Text = "" Then
txtDataShow.BorderStyle = BorderStyle.None
Else
txtDataShow.BorderStyle = BorderStyle.FixedSingle
txtDataShow.BackColor = Color.White
End If
txtDataShow.Multiline = True
txtDataShow.ReadOnly = True
X = UserLft + (J - 1) * txtDataShow.Width
Y = UserTop + (I - 1) * txtDataShow.Height
txtDataShow.Location = New Point(X, Y)
'MessageBox.Show(X & "," & Y)
Me.GroupBox1.Controls.Add(txtDataShow)
End Sub
 
Why dont you give them a name, say text1...text2...text3 etc, and then use system.reflection to find the control(s) by name before deleting the control(s)?

Code:
 Public Shared Function GetControlByName(ByVal oForm As Form, ByVal Name As String) As Control

Dim info As System.Reflection.FieldInfo = oForm.GetType().GetField("_" & Name, _
   System.Reflection.BindingFlags.NonPublic Or _
   System.Reflection.BindingFlags.Instance Or _
   System.Reflection.BindingFlags.Public Or _
   System.Reflection.BindingFlags.IgnoreCase)

If info Is Nothing Then Return Nothing

Dim o As Object = info.GetValue(oForm)
Return o

End Function


Sweep
...if it works dont mess with it
 
I have 42 txtboxes, is it means I need to have 42 names for it? is there any way more easy?
 
I used this

Private Sub clearTextboxes()
Dim ctrl As Control
Dim txtBox As TextBox
Dim i As Integer = 1
For Each ctrl In GroupBox1.Controls
If ctrl.GetType().Equals(GetType(TextBox)) Then
txtBox = CType(ctrl, Control)
GroupBox1.Controls.Remove(txtBox)
End If
Next
End Sub

when I run it, it gave me this error. for line "For Each ctrl In GroupBox1.Controls" "object reference not set to an instance of an object".
 
all my textboxes are created in the groupbox1
 
Removing of controls is odd, as the controls collection reshuffles itself as one is deleted. The only releiable way to make this work (I have found, and wait to be corrected), is to make 2 passes, one to find the controls, and the second to delete. You must also delete the highest index controls first or you run into the same problem

Here is one way I did it, Im sure there are better ways


Code:
Dim oTab As New DataTable
oTab.Columns.Add("iIndex", Type.GetType("System.Int32"))

'Find the Controls
For Each x As Control In xBox.Controls
    If TypeOf x Is TextBox Then
         Dim r As DataRow = oTab.NewRow
         r("iIndex") = xBox.Controls.GetChildIndex(x)
         oTab.Rows.Add(r)
    End If
Next

'Now remove them in descending control order
For Each r As DataRow In oTab.Select("", "iIndex desc")
    xBox.Controls.RemoveAt(CInt(r("iIndex")))
Next

xBox.Refresh()



Sweep
...if it works dont mess with it
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top