?
Solved

Move all text in a column of textboxes up one level in vb.net

Posted on 2012-09-13
3
Medium Priority
?
890 Views
Last Modified: 2012-09-13
I have a column in my windows form that I made by lining up 12 textboxes vertically.  Next to each textbox is a delete button.  Each textbox name is numbered and the number matches name of the delete button (i.e. txtbcred1, btnDeleteB1).

If the user clicks the delete button, I want the text in every textbox below that point to shift up 1 row.

I tried extracting the number from the delete button name and then find the textbox name that contains that number, but I get an "object reference not set to an instance of an object" when I try to apply the name of the textbox to a new textbox variable.

Here is what I tried that did not work.  This routine fires every time any one of the delete buttons is clicked

        Dim h As Integer = 0
        Dim curbill As String = DirectCast(sender, Control).Name
        Dim billnumber As String = ""

        Dim curtextboxname As String
        Dim curtextbox As TextBox
        Dim nexttextboxname As String
        Dim nexttextbox As TextBox

        For Each ch As Char In curbill

            If IsNumeric(ch) = True Then
                billnumber += ch
            End If

        Next

        h = Convert.ToInt16(billnumber)

        For z As Int16 = h To 12
            curtextboxname = "txtbcred" & h
            curtextbox.Name = curtextboxname

            nexttextboxname = "txtbcred" & h + 1
            nexttextbox.Name = nexttextboxname

            If InStr(curtextboxname, 12) = 0 Then
                curtextbox.Text = nexttextbox.Text

            Else
                curtextbox.Text = ""
            End If

        Next

Open in new window

0
Comment
Question by:lostinthegame
3 Comments
 
LVL 10

Accepted Solution

by:
John Claes earned 2000 total points
ID: 38395084
It's a windows Form
So you have a control that all your textboxes are member off

Why don't you tyhe searching using Linq and do the folowing ?  ( you'll need to rewrite the code because I'm a c# writer,  but it's the Idea)


For z As Int16 = h To 12
   curtextboxname = "txtbcred" & h
   curtextbox= ParentControl.Controls.FirstOrDefault(x=>x.name == curtextboxname)

   nexttextboxname = "txtbcred" & h + 1
   nexttextbox= ParentControl.Controls.FirstOrDefault(x=>x.name == nexttextboxname)

   If (curtextbox != null AND nexttextbox!= null )
                curtextbox.Text = nexttextbox.Text
    else
        curtextbox.Text = ""
    End If
Next
0
 

Author Comment

by:lostinthegame
ID: 38395618
Got it figured out.  I actually have 4 columns that I needed to move up 1 row when a delete button was pressed.  I pulled the row number from the sending delete button, saved all the current textbox values into an array, then put the values back into the textboxes at one row up.

Thanks for the help.

    Private Sub btndeleteB1_Click(sender As System.Object, e As System.EventArgs) Handles btndeleteB01.Click, btndeleteb02.Click, btndeleteB03.Click, _
        btndeleteB04.Click, btndeleteB05.Click, btndeleteB06.Click, btndeleteB07.Click, btndeleteB08.Click, btndeleteB09.Click, btndeleteB10.Click, _
        btndeleteB11.Click, btndeleteB12.Click

        'save variable for the row sending the delete call
        Dim sendingrow As String = ""

        'add value to the sending row by taking the numbers off of the end of the sending delete button's name
        For Each ch As Char In DirectCast(sender, Control).Name
            If IsNumeric(ch) = True Then
                sendingrow += ch
                sendingrow = Convert.ToInt16(sendingrow)
            End If
        Next

        'create variables to save the current textbox values
        Dim cred(12) As String
        Dim bal(12) As String
        Dim pay(12) As String
        Dim rate(12) As String

        'save a variable for the current row of the textbox being reviewed
        Dim rownum As String = ""

        'loop through controls in the groupbox
        For Each Cntrl As Control In GroupCBills.Controls

            If TypeOf Cntrl Is TextBox Then

                'for each control, check the column name to assign the value to the correct variable
                For w As Integer = 1 To 12

                    'add value to the arrays
                    If InStr(Cntrl.Name, w.ToString("00")) > 0 Then

                        If InStr(LCase(Cntrl.Name), "cred") > 0 Then
                            cred(w) = Cntrl.Text
                        ElseIf InStr(LCase(Cntrl.Name), "bal") > 0 Then
                            bal(w) = Cntrl.Text
                        ElseIf InStr(LCase(Cntrl.Name), "pay") > 0 Then
                            pay(w) = Cntrl.Text
                        ElseIf InStr(LCase(Cntrl.Name), "rate") > 0 Then
                            rate(w) = Cntrl.Text
                        End If

                    End If

                Next w

            End If

        Next Cntrl



        'for each control in the group, check to see if it needs to be moved up a level, and check which column it is currently in by searching the name
        For Each cntrl As Control In GroupCBills.Controls

            If TypeOf cntrl Is TextBox Then

                'find the current row number
                For Each ch As Char In cntrl.Name
                    If IsNumeric(ch) = True Then
                        rownum += ch
                    End If
                Next

                'check to see if the current row number needs to be moved up
                If Convert.ToDouble(rownum) >= sendingrow Then

                    rownum = Convert.ToInt16(rownum)

                    If Convert.ToDouble(rownum) <> 12 Then

                        If InStr(LCase(cntrl.Name), "cred") > 0 Then
                            cntrl.Text = cred(rownum + 1)
                        ElseIf InStr(LCase(cntrl.Name), "bal") > 0 Then
                            cntrl.Text = bal(rownum + 1)
                        ElseIf InStr(LCase(cntrl.Name), "pay") > 0 Then
                            cntrl.Text = pay(rownum + 1)
                        ElseIf InStr(LCase(cntrl.Name), "rate") > 0 Then
                            cntrl.Text = rate(rownum + 1)
                        End If

                        'if it's column 12, and the delete row command came from column 12, just delete the text
                    ElseIf Convert.ToDouble(rownum) = 12 Then

                        cntrl.Text = ""
                    End If

                End If

            'reset the rownumber
            rownum = ""

            End If

        Next

        'indicate that file needs to be saved after changes
        needsave = 1

    End Sub

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38396162
I would seriously look at using a DataGridView...

If your TextBox names all begin with "txtb", followed by {"cred", "bal", "pay", "rate"}, and then the two digit number:

    txtbcredXX
    txtbbalXX
    txtbpayXX
    txtbrateXX

Then here is another approach:
Public Class Form1

    Private Del As New List(Of Button)
    Private Cred As New List(Of TextBox)
    Private Bal As New List(Of TextBox)
    Private Pay As New List(Of TextBox)
    Private Rate As New List(Of TextBox)
    Private Cols() As List(Of TextBox) = {Cred, Bal, Pay, Rate}

    Private Sub Form1_Load(sender As Object, e As System.EventArgs)
        Dim names() As String = {"cred", "bal", "pay", "rate"}

        Dim ctlName As String
        Dim ctl As Control
        For i As Integer = 1 To 12
            ctlName = "btndeleteB" & i.ToString("00")
            ctl = FindControlByName(ctlName)
            If Not IsNothing(ctl) Then
                Del.Add(ctl)
            Else
                MessageBox.Show(ctlName, "Control Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

            For j As Integer = 0 To 3
                ctlName = "txtb" & names(j) & i.ToString("00")
                ctl = FindControlByName(ctlName)
                If Not IsNothing(ctl) Then
                    Cols(j).Add(ctl)
                Else
                    MessageBox.Show(ctlName, "Control Not Found", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            Next
        Next
    End Sub

    Private Function FindControlByName(ByVal name As String) As Control
        Dim matches() As Control = Me.Controls.Find(name, True)
        If matches.Length > 0 Then
            Return matches(0)
        End If
        Return Nothing
    End Function

    Private Sub btndeleteB1_Click(sender As System.Object, e As System.EventArgs) Handles btndeleteB01.Click, btndeleteb02.Click, btndeleteB03.Click, _
        btndeleteB04.Click, btndeleteB05.Click, btndeleteB06.Click, btndeleteB07.Click, btndeleteB08.Click, btndeleteB09.Click, btndeleteB10.Click, _
        btndeleteB11.Click, btndeleteB12.Click

        Dim index As Integer = Del.IndexOf(sender)
        If index <> -1 Then
            For i As Integer = index To 12
                For Each col As List(Of TextBox) In Cols
                    If i < 12 Then
                        col(i).Text = col(i + 1).Text
                    Else
                        col(i).Text = ""
                    End If
                Next
            Next
        End If

        'indicate that file needs to be saved after changes
        needsave = 1
    End Sub

End Class

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses
Course of the Month16 days, 3 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question