Hi I am using this code to clear the contents of my panel

For Each c As Control In PanelEdit.Controls
            If TypeOf c Is RadTextBox Then
                DirectCast(c, TextBox).Text = String.Empty
            End If
            If TypeOf c Is MaskedEditBox Then
                DirectCast(c, MaskedEditBox).Text = String.Empty
            End If
            If TypeOf c Is ListBox Then
                DirectCast(c, ListBox).Text = String.Empty
            End If
        Next

All the other controls are cleared this line does nothing

If TypeOf c Is ListBox Then
                DirectCast(c, ListBox).Text = String.Empty
            End If

the idea is if a user opens a different panel and then comes back to the panel they were in all the data is cleared.

any ideas

thanks

M

Couldn't you just use the following code to do what you need to do? I added this on Button_Click assuming you are hiding the Panel using Panel1.Visible = False:

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Panel1.Visible = False
        If Panel1.Visible = False Then
            ListBox1.Items.Clear()
        Else
            'Do nothing or what you want it to do
        End If
    End Sub
End Class

I am somewhat new at VB and still want to learn all I can so any other members have any input that would be great! I am currently going to college for computer software engineering so I will hopefully be proficient in coding in a few years.

Thanks for your reply I have tried the listBox.items.clear() as I am filling the listbox with data it gives the following error

Items collection cannot be modified when the DataSource property is set.

I am using this code to populate the ListBox on button click

Dim connString As String = My.Settings.strConn
        Dim conn As New SqlConnection(connString)
        Dim reader As IDataReader
        Dim cmd As New SqlCommand
        Dim tempList As New BindingList(Of Tenant)(tenants)
        Dim bsource As New BindingSource
        bsource.DataSource = tempList
        ListControl.DataSource = bsource.DataSource
        ListControl.DisplayMember = "Name"
        ListControl.ValueMember = "ID"

        cmd.CommandText = "SELECT * FROM tenant WHERE existing = 1"
        cmd.Connection = conn

I have to click the button twice to get it to fill the ListBox I assume this is because the first click initiates the connection / reader etc then the second click populates the ListBox with the new data.

this is my code for the fill button

Private Sub ButtonFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonFill.Click
        Dim connString As String = My.Settings.strConn
        Dim conn As New SqlConnection(connString)
        Dim reader As IDataReader
        Dim cmd As New SqlCommand
        Dim tempList As New BindingList(Of Tenant)(tenants)
        Dim bsource As New BindingSource
        bsource.DataSource = tempList
        ListControl.DataSource = bsource.DataSource
        ListControl.DisplayMember = "Name"
        ListControl.ValueMember = "ID"

        cmd.CommandText = "SELECT * FROM tenant WHERE existing = 1"
        cmd.Connection = conn

        Try
            conn.Open()
            reader = cmd.ExecuteReader()

            Do While reader.Read()
                Dim person As New Tenant()
                With person
                    .ID = reader(0)
                    .FirstName = reader(1)
                    .LastName = reader(2)
                    .Email = reader(3)
                    .phone = reader(4)
                    .Dob = reader(5)
                    .PropertyC = reader(12)
                    .Notes = reader(13)
                    .Existing = reader(14)

                End With
                tenants.Add(person)
            Loop
            reader.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            
            conn.Close()
            conn.Dispose()
            cmd.Dispose()


        End Try

and this is the tenants class I am using

Public Class Tenant
        Private _t_id As String
        Private _firstName As String
        Private _lastName As String
        Private _email As String
        Private _phone As Decimal
        Private _Dob As String
        Private _property As String
        Private _notes As String
        Private _existing As Boolean

        Public Property ID() As String
            Get
                Return _t_id
            End Get
            Set(ByVal value As String)
                _t_id = value
            End Set
        End Property

        Public Property FirstName() As String
            Get
                Return _firstName
            End Get
            Set(ByVal value As String)
                _firstName = value
            End Set
        End Property

        Public Property LastName() As String
            Get
                Return _lastName
            End Get
            Set(ByVal value As String)
                _lastName = value
            End Set
        End Property

        Public Property Email() As String
            Get
                Return _email
            End Get
            Set(ByVal value As String)
                _email = value
            End Set
        End Property

        Public Property phone() As Long
            Get
                Return _phone
            End Get
            Set(ByVal value As Long)
                _phone = value
            End Set
        End Property

        Public Property PropertyC() As String
            Get
                Return _property
            End Get
            Set(ByVal value As String)
                _property = value
            End Set
        End Property

        Public Property Notes() As String
            Get
                Return _notes
            End Get
            Set(ByVal value As String)
                _notes = value
            End Set
        End Property

        Public Property Dob() As String
            Get
                Return _Dob
            End Get
            Set(ByVal value As String)
                _Dob = value
            End Set
        End Property

        Public ReadOnly Property Name() As String
            Get
                Return LastName & ", " & FirstName
            End Get
        End Property

        Public Property Existing() As Boolean
            Get
                Return _existing
            End Get
            Set(ByVal value As Boolean)
                _existing = value
            End Set
        End Property

    End Class
    Private tenants As New List(Of Tenant)

is there a way I can achieve this without databinding or is there a way to clear the databinding

thanks
M

Then try to set the datasource as nothing.
Make the datasoruce of Listbox=nothing

Well, I don't see why it wouldn't clear the items even if they are loaded in. I will try to add your code to buttons and see what I can do.

I got it to clear using

If ListControl.Items.Count > 0 Then
ListControl.DataSource.Clear()
End If

on panel.visable event

thanks

M

That is why my code didn't work. You were databinding the items so the code below didn't work:

ListView1.Items.Clear()

Well I hope my post gave you some guidance into getting your issue solved.

Yes I know that's why I used ListControl.DataSource.Clear()

This question has already been answered. Start a new discussion instead.