Question Singing the ListBox blues...

gchq

Well-known member
Joined
Dec 14, 2007
Messages
168
Programming Experience
10+
So there is today's reason for a workstation almost taking a trip out of the window!

Scenerio - two listboxes populated by DataTables - by selecting a value in one and clicking the button it should be added to one datatable and deleted from the other. The adding bit is working fine, but the deleted... Nope It will after another item is selected - not really very handy when we have - well - users!

So here is how the Form is built

VB.NET:
Private Sub LoadCashFlowColumnDataForm(ByVal sender As Object, ByVal e As EventArgs)
        LoadCashFlowReportColumnData()
        ReportPU1 = New Form
        With ReportPU1
            .Text = "Column data"
            .Size = New Point(550, 300)
        End With
        Dim vPanel As New Panel
        With vPanel
            .Dock = DockStyle.Fill
            .AutoScroll = True
        End With
        ReportPU1.Controls.Add(vPanel)

        Dim ControlsOutLB As New ListBox
        With ControlsOutLB
            .Name = "ControlsOutLB"
            .Size = New Point(200, 100)
            .Location = New Point(10, 10)
            .BindingContext = ReportPU1.BindingContext
        End With
        vPanel.Controls.Add(ControlsOutLB)

        Dim vTT As New ToolTip

        Dim AddColumn As New Button
        With AddColumn
            .Name = "AddColumn"
            .BackgroundImage = ResizeImage(My.Resources.Arrow_Right, 20, 20)
            .Size = AddColumn.BackgroundImage.Size
            .Location = New Point(215, 20)
        End With
        AddHandler AddColumn.Click, AddressOf CashFlowAddColumn
        vTT.SetToolTip(AddColumn, "Add this column")
        vPanel.Controls.Add(AddColumn)

        Dim RemoveColumn As New Button
        With RemoveColumn
            .Name = "RemoveColumn"
            .BackgroundImage = ResizeImage(My.Resources.Arrow_Left, 20, 20)
            .Size = RemoveColumn.BackgroundImage.Size
            .Location = New Point(215, 70)
        End With
        vTT.SetToolTip(RemoveColumn, "Remove this column")
        vPanel.Controls.Add(RemoveColumn)

        Dim ControlsInLB As New ListBox
        With ControlsInLB
            .Name = "ControlsInLB"
            .Size = New Point(200, 100)
            .Location = New Point(240, 10)
            .BindingContext = ReportPU1.BindingContext
        End With
        vPanel.Controls.Add(ControlsInLB)

        Dim ControlsMoveUp As New Button
        With ControlsMoveUp
            .Name = "ControlsMoveUp"
            .BackgroundImage = ResizeImage(My.Resources.Up_Arrow, 20, 20)
            .Size = ControlsMoveUp.BackgroundImage.Size
            .Location = New Point(445, 10)
        End With
        vTT.SetToolTip(ControlsMoveUp, "Move column up a place")
        vPanel.Controls.Add(ControlsMoveUp)

        Dim ControlsMoveDown As New Button
        With ControlsMoveDown
            .Name = "ControlsMoveDown"
            .BackgroundImage = ResizeImage(My.Resources.Down_Arrow, 20, 20)
            .Size = ControlsMoveDown.BackgroundImage.Size
            .Location = New Point(445, 85)
        End With
        vTT.SetToolTip(ControlsMoveDown, "Move column down a place")
        vPanel.Controls.Add(ControlsMoveDown)
        ControlsOutLB.DataSource = StaticColumnDT
        ControlsOutLB.ValueMember = "ID"
        ControlsOutLB.DisplayMember = "Value"
      
        ControlsInLB.DataSource = ColumnDT
        ControlsInLB.ValueMember = "KeyID"
        ControlsInLB.DisplayMember = "KeyString"
       
        ReportPU1.StartPosition = FormStartPosition.CenterParent
        ReportPU1.ShowDialog()

    End Sub

And here is how it's meant to move from one DT to another

VB.NET:
Private Sub CashFlowAddColumn(ByVal sender As Object, ByVal e As EventArgs)
        Dim ColumnTB As TextBox = RFC(ReportForm, "ColumnsTB")
        Dim StaticLB As ListBox = RFC(ReportPU1, "ControlsOutLB")
        Dim ColumnsLB As ListBox = RFC(ReportPU1, "ControlsInLB")
        Dim vID As String = StaticLB.SelectedValue
        Dim vrowID As Integer = ColumnDT.Rows.Count

        Dim vIndex As Integer = Nothing
        For Each Row As DataRow In StaticColumnDT.Rows
            If Row("ID") = vID Then
                With ColumnDT.Rows
                    .Add(vrowID, FormID, vrowID, vID, StaticLB.Text)
                End With
                vIndex = StaticColumnDT.Rows.IndexOf(Row)

            End If
        Next

        If Not vIndex = Nothing Then
            StaticColumnDT.Rows(vIndex).Delete()
        End If
        StaticColumnDT.AcceptChanges()
        ColumnDT.AcceptChanges()
        ColumnTB.Text = ColumnDT.Rows.Count
        ReportPU1.Refresh()
       
    End Sub
 
Solved it

Solved it

VB.NET:
 Private Sub CashFlowAddColumn(ByVal sender As Object, ByVal e As EventArgs)
        Dim ColumnTB As TextBox = RFC(ReportForm, "ColumnsTB")
        Dim StaticLB As ListBox = RFC(ReportPU1, "ControlsOutLB")
        Dim ColumnsLB As ListBox = RFC(ReportPU1, "ControlsInLB")
        Dim vID As String = StaticLB.SelectedValue
        Dim vrowID As Integer = ColumnDT.Rows.Count
        For Each Row As DataRow In StaticColumnDT.Rows
            If Row("ID") = vID Then
                With ColumnDT.Rows
                    .Add(vrowID, FormID, vrowID, vID, StaticLB.Text)
                End With
            End If
        Next
      [B]  StaticColumnDT.Rows(StaticLB.SelectedIndex).Delete()[/B]
       StaticColumnDT.AcceptChanges()
        ColumnDT.AcceptChanges()
        ColumnTB.Text = ColumnDT.Rows.Count
        StaticLB.Refresh()
        ColumnsLB.Refresh()
        ReportPU1.Refresh()
    End Sub

Onwards and upwards.....
 
Back
Top