Results 1 to 6 of 6

Thread: Trying to learn how to work with csv, listbox and textbox in vb.net

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2020
    Posts
    1

    Trying to learn how to work with csv, listbox and textbox in vb.net

    I could be going about this all wrong, but I haven't been able to figure to delete from the textbox and listbox at the same time. Any help or advice would be appreciated. Thank you
    Last edited by thePackardMill; Sep 21st, 2020 at 06:37 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Trying to learn how to work with csv, listbox and textbox in vb.net

    Quote Originally Posted by thePackardMill View Post
    I'm using a datagridview, as well, but it is hidden on the form.
    Stop that for a start. A DataGridView is a control, for displaying data to the user and for interacting with the user. If you're doing neither of those things, there's no justification for using that controls. here's what you should be doing:

    1. Create a DataTable with columns for each of the data fields in your file and one extra Boolean column.
    2. Read the file into that DataTable and set the Boolean column to a default value for each row.
    3. Bind the DataTable to two BindingSources that you added to the form in the designer.
    4. Set the Filter of one of the BindingSources to exclude rows where the Boolean column contains the default value.
    5. Bind the BindingSource without the filter to the ComboBox and the one with the Filter to the ListBox and TextBox.

    The DataTable, which is intended for data storage, replaces the DataGridView. Thanks to the data-binding, adding the selected item from the ComboBox into the ListBox is a simple case of setting the value in the Boolean column to the opposite of the default value, e.g.
    vb.net Code:
    1. DirectCast(UnfilteredBindingSource.Current, DataRowView)("Flag") = True
    Removing the selected item from the ListBox is just as simple a matter of setting the flag back again:
    vb.net Code:
    1. DirectCast(FilteredBindingSource.Current, DataRowView)("Flag") = False
    Because the filtered BindingSource is bound to both the ListBox and the TextBox, selecting an item in the ListBox will show the specified field of the same row in the TextBox. Removing an item should likewise clear the TextBox, although I haven't tested that. If it doesn't, you'd clear it just like you would any other TextBox.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Trying to learn how to work with csv, listbox and textbox in vb.net

    Wouldn't it be an issue where binding two bindingsources to the datatable, then setting the filter on one, would filter both bindingsources, resulting in no rows in both?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Trying to learn how to work with csv, listbox and textbox in vb.net

    Quote Originally Posted by .paul. View Post
    Wouldn't it be an issue where binding two bindingsources to the datatable, then setting the filter on one, would filter both bindingsources, resulting in no rows in both?
    You are correct. I confused myself because I've often pointed out that using two BindingSources with one DataTable allows you to make separate selections in two controls, but it doesn't allow you to use separate filters. You would need to create separate DataViews for that. I'll put an example together.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Trying to learn how to work with csv, listbox and textbox in vb.net

    Here is said example:
    vb.net Code:
    1. Private ReadOnly table As New DataTable
    2.  
    3. Private Sub ParentForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.     'Add standard data columns.
    5.     With table.Columns
    6.         .Add("Id", GetType(Integer))
    7.         .Add("Name", GetType(String))
    8.         .Add("Description", GetType(String))
    9.     End With
    10.  
    11.     'Add filter column.
    12.     table.Columns.Add("Filter", GetType(Boolean)).DefaultValue = True
    13.  
    14.     'Add rows.
    15.     With table.Rows
    16.         .Add(1, "One", "First")
    17.         .Add(2, "Two", "Second")
    18.         .Add(3, "Three", "Third")
    19.         .Add(4, "Four", "Fourth")
    20.     End With
    21.  
    22.     'Bind the ComboBox.
    23.     BindingSource1.DataSource = New DataView(table)
    24.  
    25.     With ComboBox1
    26.         .DisplayMember = "Name"
    27.         .ValueMember = "Id"
    28.         .DataSource = BindingSource1
    29.     End With
    30.  
    31.     'Bind the ListBox and TextBox.
    32.     BindingSource2.Filter = "Filter = False"
    33.     BindingSource2.DataSource = New DataView(table)
    34.  
    35.     With ListBox1
    36.         .DisplayMember = "Name"
    37.         .ValueMember = "Id"
    38.         .DataSource = BindingSource2
    39.     End With
    40.  
    41.     TextBox1.DataBindings.Add("Text", BindingSource2, "Description")
    42. End Sub
    43.  
    44. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    45.     'Add the selected row from the ComboBox to the ListBox.
    46.     DirectCast(BindingSource1.Current, DataRowView).Row("Filter") = False
    47. End Sub
    48.  
    49. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    50.     If ListBox1.SelectedItem IsNot Nothing Then
    51.         'Remove the selected row from the ListBox.
    52.         DirectCast(BindingSource2.Current, DataRowView).Row("Filter") = True
    53.     End If
    54. End Sub
    You'll need to add the appropriate controls and components to the form in the designer. Note that, in the Button Click event handlers, the code gets the selected DataRowView and then gets the DataRow from that to set the field. If you set it on the DataRowView instead, the UI doesn't update immediately.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Trying to learn how to work with csv, listbox and textbox in vb.net

    Quote Originally Posted by jmcilhinney View Post
    Here is said example:
    vb.net Code:
    1. Private ReadOnly table As New DataTable
    2.  
    3. Private Sub ParentForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.     'Add standard data columns.
    5.     With table.Columns
    6.         .Add("Id", GetType(Integer))
    7.         .Add("Name", GetType(String))
    8.         .Add("Description", GetType(String))
    9.     End With
    10.  
    11.     'Add filter column.
    12.     table.Columns.Add("Filter", GetType(Boolean)).DefaultValue = True
    13.  
    14.     'Add rows.
    15.     With table.Rows
    16.         .Add(1, "One", "First")
    17.         .Add(2, "Two", "Second")
    18.         .Add(3, "Three", "Third")
    19.         .Add(4, "Four", "Fourth")
    20.     End With
    21.  
    22.     'Bind the ComboBox.
    23.     BindingSource1.DataSource = New DataView(table)
    24.  
    25.     With ComboBox1
    26.         .DisplayMember = "Name"
    27.         .ValueMember = "Id"
    28.         .DataSource = BindingSource1
    29.     End With
    30.  
    31.     'Bind the ListBox and TextBox.
    32.     BindingSource2.Filter = "Filter = False"
    33.     BindingSource2.DataSource = New DataView(table)
    34.  
    35.     With ListBox1
    36.         .DisplayMember = "Name"
    37.         .ValueMember = "Id"
    38.         .DataSource = BindingSource2
    39.     End With
    40.  
    41.     TextBox1.DataBindings.Add("Text", BindingSource2, "Description")
    42. End Sub
    43.  
    44. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    45.     'Add the selected row from the ComboBox to the ListBox.
    46.     DirectCast(BindingSource1.Current, DataRowView).Row("Filter") = False
    47. End Sub
    48.  
    49. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    50.     If ListBox1.SelectedItem IsNot Nothing Then
    51.         'Remove the selected row from the ListBox.
    52.         DirectCast(BindingSource2.Current, DataRowView).Row("Filter") = True
    53.     End If
    54. End Sub
    You'll need to add the appropriate controls and components to the form in the designer. Note that, in the Button Click event handlers, the code gets the selected DataRowView and then gets the DataRow from that to set the field. If you set it on the DataRowView instead, the UI doesn't update immediately.
    That works...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width