Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. clear textboxes

clear textboxes

Scheduled Pinned Locked Moved Visual Basic
helpcsharpquestion
7 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lisana
    wrote on last edited by
    #1

    this code is write by vb6 and it convert to vb.net, when I run it, it gets me error. is anybody knows hot to fix this or what should I code it in vb.net? Thanks! Private Sub clearTextboxes() Dim txt As System.Windows.Forms.Control 'Set each textbox's value to "" For Each txt In frmMain.DefInstance.Controls 'UPGRADE_WARNING: TypeOf has a new behavior. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1041"' If TypeOf txt Is System.Windows.Forms.TextBox Then txt.Text = "" Next txt End Sub Lisa

    D 1 Reply Last reply
    0
    • L Lisana

      this code is write by vb6 and it convert to vb.net, when I run it, it gets me error. is anybody knows hot to fix this or what should I code it in vb.net? Thanks! Private Sub clearTextboxes() Dim txt As System.Windows.Forms.Control 'Set each textbox's value to "" For Each txt In frmMain.DefInstance.Controls 'UPGRADE_WARNING: TypeOf has a new behavior. Click for more: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1041"' If TypeOf txt Is System.Windows.Forms.TextBox Then txt.Text = "" Next txt End Sub Lisa

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You need to change out the TypeOf statement to something more .NET compatible.

      Dim ctrl As Control
      Dim txtBox As TextBox
      For Each Control In frmMain.Controls
      If Control.GetType().Equals(GetType(TextBox)) Then
      txtBox = CType(Control, GetType(TextBox))
      txtBox.Text = String.Empty
      End If
      Next

      Or something very similar. I wrote this from memory so it may not compile without some tweaks. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      L 1 Reply Last reply
      0
      • D Dave Kreskowiak

        You need to change out the TypeOf statement to something more .NET compatible.

        Dim ctrl As Control
        Dim txtBox As TextBox
        For Each Control In frmMain.Controls
        If Control.GetType().Equals(GetType(TextBox)) Then
        txtBox = CType(Control, GetType(TextBox))
        txtBox.Text = String.Empty
        End If
        Next

        Or something very similar. I wrote this from memory so it may not compile without some tweaks. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        L Offline
        L Offline
        Lisana
        wrote on last edited by
        #3

        thanks..Dave. I have another question to ask you. if I created textboxes, do I need to remove them when I use it again? when I resign the iStartIndex and iEndIndex and recall the sub textboxshow(), what should I do to make the second call shows correctly? Private Sub textboxShow() setStartIndex() setEndIndex() clearTextboxes() MessageBox.Show(iStartIndex & "," & iEndIndex) 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.BackColor = Color.White 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 Private Sub clearTextboxes() Dim ctrl As Control Dim txtBox As TextBox For Each ctrl In GroupBox1.Controls If ctrl.GetType().Equals(GetType(TextBox)) Then txtBox = CType(ctrl, Control) txtBox.Text = String.Empty End If Next End Sub Lisa

        D 1 Reply Last reply
        0
        • L Lisana

          thanks..Dave. I have another question to ask you. if I created textboxes, do I need to remove them when I use it again? when I resign the iStartIndex and iEndIndex and recall the sub textboxshow(), what should I do to make the second call shows correctly? Private Sub textboxShow() setStartIndex() setEndIndex() clearTextboxes() MessageBox.Show(iStartIndex & "," & iEndIndex) 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.BackColor = Color.White 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 Private Sub clearTextboxes() Dim ctrl As Control Dim txtBox As TextBox For Each ctrl In GroupBox1.Controls If ctrl.GetType().Equals(GetType(TextBox)) Then txtBox = CType(ctrl, Control) txtBox.Text = String.Empty End If Next End Sub Lisa

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          If the code generates textboxes to display data, and you use the same code to display new data, then either, yes, you'll have to delete the old textboxes because your code is going to create a new set of them, or, your code will have to know that it already created all the necessary textboxes and reuse them. The first option is much easier to implement... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          L 1 Reply Last reply
          0
          • D Dave Kreskowiak

            If the code generates textboxes to display data, and you use the same code to display new data, then either, yes, you'll have to delete the old textboxes because your code is going to create a new set of them, or, your code will have to know that it already created all the necessary textboxes and reuse them. The first option is much easier to implement... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

            L Offline
            L Offline
            Lisana
            wrote on last edited by
            #5

            how can I remove the dynamic textboxes? I have tried to remove it use controls.remove, but it doesn't work, it just remove all the textboxes when it load, then doing nothing. can you help me see what's wrong with it? Thanks a lot. Dim tempCtrl As Control For Each tempCtrl In GroupBox1.Controls Groupbox1.Controls.Remove(tempCtrl) Next tempCtrl Lisa

            D 1 Reply Last reply
            0
            • L Lisana

              how can I remove the dynamic textboxes? I have tried to remove it use controls.remove, but it doesn't work, it just remove all the textboxes when it load, then doing nothing. can you help me see what's wrong with it? Thanks a lot. Dim tempCtrl As Control For Each tempCtrl In GroupBox1.Controls Groupbox1.Controls.Remove(tempCtrl) Next tempCtrl Lisa

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              You're not checking the type of control your removing. On top of that, you can't modify a collection while your enumerating it, or using For/Each on it. You have to use an array index to do this. But, overall, it works the same as trying to clear the text boxes. Check the type of the control first, then remove it:

              Dim ctrl As Control
              Dim txtBox As TextBox
              Dim i As Integer
              For i = 0 to frmMain.Controls.Count - 1
              If frmMain.Controls(i).GetType().Equals(GetType(TextBox)) Then
              frmMain.Controls.RemoveAt(i)
              End If
              Next

              RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              L 1 Reply Last reply
              0
              • D Dave Kreskowiak

                You're not checking the type of control your removing. On top of that, you can't modify a collection while your enumerating it, or using For/Each on it. You have to use an array index to do this. But, overall, it works the same as trying to clear the text boxes. Check the type of the control first, then remove it:

                Dim ctrl As Control
                Dim txtBox As TextBox
                Dim i As Integer
                For i = 0 to frmMain.Controls.Count - 1
                If frmMain.Controls(i).GetType().Equals(GetType(TextBox)) Then
                frmMain.Controls.RemoveAt(i)
                End If
                Next

                RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                L Offline
                L Offline
                Lisana
                wrote on last edited by
                #7

                when I run it, it shows this error: Specified argument was out of the range of valid values. parameter name: Index 32 is out of range. but the control.count shows result is 53, why it is out of range? Lisa

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups